Pagini recente » Cod sursa (job #423606) | Cod sursa (job #2330927) | Cod sursa (job #2372756) | Cod sursa (job #2714670) | Cod sursa (job #2362006)
#include <fstream>
#include <cstring>
using namespace std;
char s[100001];
int nr, k1, k2, st2[100001], rez, i, pr[300];
char st1[100001];
int operatie (char c, int a, int b)
{
if (c=='+')
return a+b;
else if (c=='-')
return a-b;
else if (c=='*')
return a*b;
else if (c=='/')
return a/b;
}
int main () {
ifstream fin ("evaluare.in");
ofstream fout ("evaluare.out");
fin>>s;
pr['+']=1;
pr['-']=1;
pr['(']=0;
pr[')']=0;
pr['*']=2;
pr['/']=2;
for (i=0;s[i]!=0;i++) {
if (s[i]>='0' && s[i]<='9') {
nr=0;
while (s[i]>='0' && s[i]<='9') {
nr=nr*10+s[i]-'0';
i++;
}
i--;
st2[++k2]=nr;
}else
if (s[i]=='(')
st1[++k1]='(';
else
if (s[i]==')') {
while (st1[k1]!='(') {
rez=operatie(st1[k1], st2[k2-1], st2[k2]);
k1--; k2--;
st2[k2]=rez;
}
k1--;
}
else {
while (k1>0 && pr[st1[k1]]>=pr[s[i]]) {
rez=operatie(st1[k1], st2[k2-1], st2[k2]);
k1--; k2--;
st2[k2]=rez;
}
st1[++k1]=s[i];
}
}
while (k1>0 && pr[st1[k1]]>=pr[s[i]]) {
rez=operatie(st1[k1], st2[k2-1], st2[k2]);
k1--;k2--;
st2[k2]=rez;
}
fout<<st2[1];
return 0;
}