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;
}

原文地址:https://www.cnblogs.com/yj1989/p/3519525.html