zmabc @ 2021-01-11 00:37:04
#include <iostream>
#include<queue>
#include <sstream>
#include <queue>
using namespace std;
int width(int x);
struct ys{
char c;
int number1;
int number2;
};
int main()
{
int n;
cin>>n;
cin.sync();/*这里不放这个输后面入时会有问题,输不进去*/
queue<ys> arr;
char c;
int num1;
int num2;
for(int i=0;i<n;++i){
string s;
getline(cin,s,'\n');
stringstream os;
os<<s;
if(s[0]=='a'||s[0]=='b'||s[0]=='c'){
os>>c>>num1>>num2;
}
else{
c=arr.back().c;
os>>num1>>num2;
}
ys temp={c,num1,num2};
arr.push(temp);
}
while(!arr.empty()){
int temp;
if(arr.front().c=='a'){
temp=arr.front().number1+arr.front().number2;
cout<<arr.front().number1<<"+"<<arr.front().number2<<"="<<temp<<endl;
cout<<width(arr.front().number1)+width(arr.front().number2)+width(temp)+2<<endl;
}
else if(arr.front().c=='b'){
temp=arr.front().number1-arr.front().number2;
cout<<arr.front().number1<<"-"<<arr.front().number2<<"="<<temp<<endl;
cout<<width(arr.front().number1)+width(arr.front().number2)+width(temp)+2<<endl;
}
else if(arr.front().c=='c'){
temp=arr.front().number1*arr.front().number2;
cout<<arr.front().number1<<"*"<<arr.front().number2<<"="<<temp<<endl;
cout<<width(arr.front().number1)+width(arr.front().number2)+width(temp)+2<<endl;
}
arr.pop();
}
return 0;
}
int width(int x )
{
int width=0;
if(x<0) width++;
while(x!=0){
width++;
x/=10;
}
return width;
}
by FCB_1899 @ 2021-01-11 09:47:13
我很蒻,遇到这种问题时一般考虑优化
by _caiji_ @ 2021-01-11 17:15:59
getline读进了\r,把他去掉试一下