Pagini recente » Cod sursa (job #2857344) | Cod sursa (job #26289) | Cod sursa (job #959630) | Cod sursa (job #1201952) | Cod sursa (job #719230)
Cod sursa(job #719230)
#include <fstream>
#include<string.h>
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
int prior(char c){if(c=='+'||c=='-')return 1;
if(c=='*'||c=='/')return 2;
if(c=='('||c==')')return 0;
else return -1;
}
int main()
{ char s[100002],expr[100002];
fin.getline(s,100002);
int i,nr;
i=strlen(s);
s[i]=')';
s[i+1]=0;
int k=0,l=-1;
int st[100002],vf=0,stnr[100002];
char stc[100002];
stc[0]='(';
for(i=0;i<strlen(s);i++)
{nr=0;
if(prior(s[i])<0)
{
while(prior(s[i])<0&&i<strlen(s))nr=nr*10+s[i++]-'0';
i--;
st[++k]=nr;
expr[++l]='a';
}
else{if(s[i]=='(')stc[++vf]='(';
if(s[i]==')'){while(stc[vf]!='(')expr[++l]=stc[vf--];
vf--;
}
if(prior(s[i])>0){while(prior(stc[vf])>=prior(s[i]))expr[++l]=stc[vf--];
stc[++vf]=s[i];
}
}
}
expr[++l]=0;
vf=0;
nr=0;
for(i=0;i<l;i++)
if(expr[i]=='a'){++nr;stnr[++vf]=st[nr];}
else{if(expr[i]=='+'){stnr[vf-1]=stnr[vf-1]+stnr[vf];vf--;}
if(expr[i]=='-'){stnr[vf-1]=stnr[vf-1]-stnr[vf];vf--;}
if(expr[i]=='*'){stnr[vf-1]=stnr[vf-1]*stnr[vf];vf--;}
if(expr[i]=='/'){stnr[vf-1]=stnr[vf-1]/stnr[vf];vf--;}
}
fout<<stnr[1];
return 0;
}