以下为代码! 希望各位大大帮忙
#include <iostream>
#include <string>
#include <strstream>
using namespace std;
int main(){
string input;
while(getline(cin,input))
{
string checknow,snum[200];
long int ysz=0,snum1=0;
snum[0]="(";
for(int i =0;i<input.length();i++)
{
checknow = input[i];
if(checknow >="0"&&checknow <="9")
{
if(ysz==1)
{
string heqilai = snum[snum1] + checknow;
snum[snum1] = heqilai;
}
else
{
snum1++;
snum[snum1]=checknow;
ysz=1;
}
}
else if(checknow =="+"||checknow =="-"||checknow =="*"||checknow =="/"||checknow =="("||checknow ==")"||checknow =="%")
{
snum1++;
snum[snum1]=checknow;
}
else{
ysz=0;
}
}
snum1++;
snum[snum1]=")";
///////////////////////////
/////////////////////
string snumtemp[200],right[200],checknow2;
long int snumtemp1=0,right1=0;
long int start[10],start1=-1;
for(int i=0;i<=snum1;i++)
{
checknow = snum[i];
if(checknow=="(")
{
right[right1]= "(";
right1++;
start1++;
start[start1]=i;
}
else if(checknow=="+")
{
if(right[right1-1]!="("&&(right[right1-1]=="+"||right[right1-1]=="-"||right[right1-1]=="*"||right[right1-1]=="/"))
{
snumtemp[snumtemp1]= right[right1-1];
snumtemp1++;
right[right1-1]= "+";
}
else
{right[right1]= "+";
right1++;}
}
else if(checknow=="-")
{
if(right[right1-1]!="("&&(right[right1-1]=="+"||right[right1-1]=="-"||right[right1-1]=="*"||right[right1-1]=="/"))
{
snumtemp[snumtemp1]= right[right1-1];
snumtemp1++;
right[right1-1]= "-";
}
else
{right[right1]= "-";
right1++;}
}
else if(checknow=="*")
{
if(right[right1-1]!="("&&(right[right1-1]=="*"||right[right1-1]=="/"))
{
snumtemp[snumtemp1]= right[right1-1];
snumtemp1++;
right[right1-1]= "*";
}
else
{right[right1]= "*";
right1++;}
}
else if(checknow=="/")
{
if(right[right1-1]!="("&&(right[right1-1]=="*"||right[right1-1]=="/"))
{
snumtemp[snumtemp1]= right[right1-1];
snumtemp1++;
right[right1-1]= "/";
}
else
{right[right1]= "/";
right1++;}
}
else if(checknow=="%")
{
if(right[right1-1]!="("&&(right[right1-1]=="*"||right[right1-1]=="%"))
{
snumtemp[snumtemp1]= right[right1-1];
snumtemp1++;
right[right1-1]= "%";
}
else
{right[right1]= "%";
right1++;}
}
else if(checknow==")")
{
for(int i2=right1;i2>=start[start1];i2--)
{checknow2 = right[i2];
if(checknow2=="+")
{
snumtemp[snumtemp1]= "+";
snumtemp1++;
right[i2]="";
}
else if(checknow2=="-")
{
snumtemp[snumtemp1]= "-";
snumtemp1++;
right[i2]="";
}
else if(checknow2=="*")
{
snumtemp[snumtemp1]= "*";
snumtemp1++;
right[i2]="";
}
else if(checknow2=="/")
{
snumtemp[snumtemp1]= "/";
snumtemp1++;
right[i2]="";
}
else if(checknow2=="%")
{
snumtemp[snumtemp1]= "%";
snumtemp1++;
right[i2]="";
}
}
start1--;
}
else
{
snumtemp[snumtemp1]= checknow;
snumtemp1++;
}
}
/////////////////////////
//////////////////////////////////
long int anw[200];
long int anw1=-1,stianw2=0;
for(int i=0;i<=snumtemp1;i++)
{ if (snumtemp[i]=="+")
{
anw[anw1-1] =anw[anw1-1]+anw[anw1];
anw1--;
}
else if (snumtemp[i]=="-")
{
anw[anw1-1] =anw[anw1-1]-anw[anw1];
anw1--;
}
else if (snumtemp[i]=="*")
{
anw[anw1-1] =anw[anw1-1]*anw[anw1];
anw1--;
}
else if (snumtemp[i]=="/")
{
anw[anw1-1] =anw[anw1-1]/anw[anw1];
anw1--;
}
else if (snumtemp[i]=="%")
{
anw[anw1-1] =anw[anw1-1]%anw[anw1];
anw1--;
}
else{
strstream ss; //共用值SS
stianw2=0;
ss<< snumtemp[i]; //Total导入ss //形态转换 int to string
ss>> stianw2;
anw1++;
anw[anw1]= stianw2;
}
}
cout << anw [0]<<endl;
}
return 0;
}