这道题去我照着老师的打了一遍,不知道有没有打错 回家运行后发现有问题,还有就是pri函数不知道发哪个地方?
求各位大佬帮我调一下
邀请码是 dzkw
#include <cstdio>
#include <stack>
#include <cmath>
#include <cstring>
using namespace std;
const int N = 1000005;
int a = 0;
char s[N];
int flag = 0;
stack<int> q;
stack<int> p;
int pri(char ch)
{
if(ch == ' ') return 4;
if(ch == '*' || ch == '/') return 3;
if(ch == '+' || ch == '-') return 2;
if(ch == '(' || ch == ')') return 1;
}
int main()
{
scanf("%s", s + 1);
int n = strlen(s + 1);
for(int i = 1; i <= n; i ++)
{
char ch = s[i];
int j = i;
if(ch >= '0' && ch <= '9')
{
while(s[j] >= '0' && s[j] <= '9') a = a * 10 + s[j] - '0', j ++;
i = j - 1;
q.push(a);
a = 0;
}
if(ch == '(') p.push('(');
else if(ch == '(')
{
while(p.top() != '(')
{
int x = q.top();q.pop();
int y = q.top();q.pop();
char chi = p.top();
if(chi == '+') q.push(y + x);
if(chi == '-') q.push(y - x);
if(chi == '*') q.push(y * x);
if(chi == '/') q.push(y / x);
if(chi == '^') q.push(pow(y, x));
p.pop();
}
p.push(ch);
}
}
while(!p.empty())
{
int x = q.top();q.pop();
int y = q.top();q.pop();
char chi = p.top();
if(chi == '+') q.push(y + x);
if(chi == '-') q.push(y - x);
if(chi == '*') q.push(y * x);
if(chi == '/') q.push(y / x);
if(chi == '^') q.push(pow(y, x));
p.pop();
}
printf("%d", q.top());
return 0;
}