zoukankan      html  css  js  c++  java
  • STL模板之_map,stack(计算矩阵相乘的次数)

    #include <map>
    #include <stack>
    #include <iostream>
    using namespace std;

    struct Node { int row, col; };

    int main()
    {
    int n;
    char name;
    map<char, Node> matrix;
    cout<<"please input the number of the zimu:"<<endl;
    cin >> n;
    cout<<"please input the information of the matrix:"<<endl;
    for(int i = 0; i < n; i++)
    {
    cin >> name;
    cin >> matrix[name].row >> matrix[name].col;
    }
    cout<<"please input the experssion for compute:"<<endl;
    string exp;
    while(cin >> exp)
    {
    int i,p;
    int count = 0;
    stack<Node> array;
    for(i = 0; i < exp.size(); i++)
    {
    if(exp[i] == '(') continue; //continue 是跳出本次循环,而break 是跳出本层循环
    if(exp[i] == ')')
    {
    Node b = array.top();
    array.pop();
    Node a = array.top();
    array.pop();
    if(a.col != b.row)
    {
    cout<<"error"<<endl;
    break;
    }
    count += a.row * b.row * b.col;
    Node tmp = {a.row, b.col};
    array.push(tmp);
    }
    else array.push(matrix[exp[i]]);
    }
    if(i == exp.size())
    {
    cout<<"the total number of the experssion is:"<<endl;
    cout << count << endl;
    };
    cout<<"continue or not?"<<endl;
    cout<<"jixu(1)"<<" "<<"tingzhi(0)"<<endl;
    cin>>p;
    if (p==0) break;
    else cout<<"please input the experssion for compute:"<<endl;
    }
    system("pause");
    return 0;
    }

  • 相关阅读:
    小程序 ----- 使用less框架
    小程序 ------ 选择器(十)
    小程序 ------ 样式(九)
    小程序 --- 事件绑定(八)
    记一次接口数据获取最大最小值
    flutter——命名路由跳转传值
    Dart——库
    Dart——基础
    人口普查小结
    人口普查-运行截图
  • 原文地址:https://www.cnblogs.com/yj1989/p/3519525.html
Copyright © 2011-2022 走看看