题目(难度***)
Desc
Calculate expression.
Input
The input contains several test cases, one test case per line.
Each case have nintegers(0<=n<2
31
) and operators(“+” or “-”), separated by blank spaces.The number of integers is small than 10.
Output
One case per line, output the answer
输入样例
123456789 + 8 - 765
5 - 987456 + 2
输出样例
123456032
-987449
收获
1:学会了多组string输入,参考链接
代码
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
while(getline(cin,s)){
int num=0;
queue<char >op;
queue<int >number;
for(int i=0;i<s.length();i++){
if(s[i]=='+'||s[i]=='-')
op.push(s[i]);
else if(s[i]==' ')
continue;
else{
while(s[i]!=' '&&i<s.length())
{
num=num*10+s[i]-'0';
i++;
}
//cout<<num<<endl;
number.push(num);
num=0;
}
}
long long sum=number.front();
number.pop();
while(!number.empty()){
int temp=number.front();
number.pop();
int oper=op.front();
op.pop();
if(oper=='+')
sum+=temp;
else{
sum-=temp;
}
}
cout<<sum<<endl;
}
return 0;
}