#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int mod=1e9+7;
const int N=1e5+10;
int priori(char c)//判断一个字符的优先级,返回的是它的优先级
{
if(c=='*'||c=='/')
return 2;
if(c=='+'||c=='-')
return 1;
}
int iswhatandcompute(char c,int n1, int n2)
{
/*
c:操作符
n1,n2:操作数
返回的是n2 c n1 的值
*/
if (c=='*')
return n1*n2;
if(c=='/')
return n2/n1;
if(c=='+')
return n2+n1;
if(c=='-')
return n2-n1;
}
int chartoint(char c)
{
//字符转数字
return c-48;
}
int isoperate(char c)
{
/*
判断字符是数字还是操作符
操作符:返回1
数字:返回0
*/
if(c=='+'||c=='-'||c=='*'||c=='/')
return 1;
else
return 0;
}
void compute(string s)
{
stack<char> op;
stack<int> num;
int n=0;
//计算表达式的值
int i;
int size_s=s.size();
for(i=0; i<size_s;)
{
if (isoperate(s[i])) //判断s[i]是操作符
{
num.push(n);
n=0;
if(!op.empty()) //操作符栈非空
{
char temp=op.top();
while(!op.empty()&&priori(op.top())>=priori(s[i]))
{
temp=op.top();
int n1=num.top();
num.pop();
int n2=num.top();
num.pop();
int n3=iswhatandcompute(temp,n1,n2);
num.push(n3);
op.pop();
}
op.push(s[i]);
i++;
}
else
{
op.push(s[i]);
i++;
}
}
else //是数字
{
n=n*10+chartoint(s[i]);
i++;
}
}
num.push(n);
while(!op.empty())
{
char t=op.top();
op.pop();
int t1=num.top();
num.pop();
int t2=num.top();
num.pop();
int t3=iswhatandcompute(t,t1,t2);
num.push(t3);
}
int k=num.top();
cout<<k<<endl;
}
int main()
{
string str;
while(cin>>str)
{
compute(str);
}
return 0;
}
pipi 1362(表达式求值)
最新推荐文章于 2024-09-07 14:59:50 发布