简单的栈操作 对给定的输入串进行出栈如栈操作 每一次出栈后进行计算 得到的结果再次如栈 一直进行到输入串结束!!
#include<cstdio>
#include<iostream>
#include<cstring>
#include<stack>
#include<vector>
#include<map>
using namespace std;
struct Node
{
char name[10];
int row,col;
};
int main()
{
int n;
char exp[100];
Node maxtrix[30];
scanf("%d",&n);
for(int i = 0; i < n; i++)
{
scanf("%s",maxtrix[i].name);
scanf("%d%d",&maxtrix[i].row,&maxtrix[i].col);
}
memset(exp, '\0', sizeof(exp));
while(scanf("%s",exp) != EOF)
{
int i;
int cou = 0;
stack<Node> arr;
for(i = 0; i < strlen(exp); i++)
{
if(exp[i] == '(')
//不需要进行操作 直接进行下一个字符的判断
continue;
if (exp[i] == ')')
{
//如果遇到')' 出现则要进行计算
//把最末尾的两个矩阵提取出来
Node b = arr.top();
arr.pop();
Node a = arr.top();
arr.pop();
//两个矩阵不能进行相乘的情况
if (a.col != b.row)
{
printf("error\n");
break;
}
cou += a.row * b.row * b.col;
//进行一次出栈后的计算 结果再次如栈
Node tmp ;
//tmp.name = a.name;
tmp.row = a.row;
tmp.col = b.col;
arr.push(tmp);
}
else
{
//进行如栈操作
for(int j=0; j<n; j++)
{
if(maxtrix[j].name[0]==exp[i])
{
arr.push(maxtrix[j]);
break;
}
}
}
}
if(i == strlen(exp))
printf("%d\n",cou);
memset(exp, '\0', sizeof(exp));
}
return 0;
}