hdu1082

本文介绍了一个使用C++实现的表达式解析与计算程序。该程序通过栈结构处理括号配对,并计算含有矩阵乘法的表达式的值。程序首先读取一定数量的矩阵定义,包括矩阵名称、行数和列数,然后读取并处理一系列包含这些矩阵的算术表达式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<stack>
#include<string>
#include<cctype>
using namespace std;

#define N 30
struct node
{
char m;
int r,c;
} a[N];
string s;

bool process(int& ans)
{
int len,i;
stack<node> sta;
node x,y,t;

len=s.length();
if(len==1)
{
ans=0;
return true;
}
ans=0;
for(i=0;i<len;i++)
if(isupper(s[i]))
sta.push(a[s[i]-'A']);
else if(s[i]==')')
{
x=sta.top();
sta.pop();
y=sta.top();
sta.pop();
if(x.r!=y.c) return false;
t.r=y.r;
t.c=x.c;
ans+=y.r*y.c*x.c;
sta.push(t);
}
return true;
}

int main()
{
int n,i,ans;
char c;

cin>>n;
for(i=0;i<n;i++)
{
getchar();
cin>>c;
a[c-'A'].m=c;
cin>>a[c-'A'].r>>a[c-'A'].c;
}
while(cin>>s)
{
if(!process(ans))
cout<<"error"<<endl;
else
cout<<ans<<endl;
}
return 0;
}

转载于:https://www.cnblogs.com/wangkun1993/p/6341653.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值