看错题目了,看成多个数字了
两位正整数的简单算术运算(只考虑整数运算),算术运算为:
+,加法运算;
-,减法运算;
*,乘法运算;
/,整除运算;
%,取余运算。
算术表达式的格式为(运算符前后可能有空格):
运算数运算符运算数。
请输出相应的结果。
输入格式
一行算术表达式。
输出格式
整型算数运算的结果(结果值不一定为 2 位数,可能多于 2 位或少于 2 位)。
输入输出样例
输入 #1
32+64
输出 #1
96
#include <iostream>
#include <bits/stdc++.h>
#include <stack>
using namespace std;
int main()
{
string a;
getline(cin,a);
string s;
stack<int> s1;
stack<char>s2;
for(int i=0;i<a.length();i++){
if(a[i]=='+'){
int x=stoi(s);
s="";
s1.push(x);
s2.push('+');
}
else if(a[i]=='-'){
int x=stoi(s);
s="";
s1.push(x);
s2.push('-');
}
else if(a[i]=='*'){
int x=stoi(s);
s="";
s1.push(x);
s2.push('*');
}
else if(a[i]=='%'){
int x=stoi(s);
s="";
s1.push(x);
s2.push('%');
}
else if(a[i]=='/'){
int x=stoi(s);
s="";
s1.push(x);
s2.push('/');
}
else s+=a[i];
}
int x=stoi(s);
s1.push(x);
// while(!s1.empty()){
// cout<<s1.top();
// s1.pop();
// }
while(!s2.empty()){
int x=s1.top();//64
s1.pop();
int y=s1.top();//32
s1.pop();
char c=s2.top();
s2.pop();
int s=0;
if(c=='+')s=y+x;
if(c=='-')s=y-x;
if(c=='*')s=y*x;
if(c=='/')s=y/x;
if(c=='%')s=y%x;
s1.push(s);
}
cout<<s1.top();
return 0;
}

浙公网安备 33010602011771号