Pagini recente » Cod sursa (job #2974701) | Cod sursa (job #3249307) | Cod sursa (job #3249284) | Cod sursa (job #475427) | Cod sursa (job #2348250)
#include <fstream>
#include <stack>
#include <map>
using namespace std;
ifstream fi("evaluare.in");
ofstream fo("evaluare.out");
stack <int> nr;
stack <char> op;
map <char ,int> pr;
string s;
int a,b,x,ok;
int main()
{
fi>>s;
s="("+s+")";
pr['+']=pr['-']=2; pr['*']=pr['/']=3; pr[')']=1; pr['(']=0;
op.push('(');
for(int i=1;i<s.size();i++)
{
x=0; ok=0;
while(s[i]>='0' && s[i]<='9')
{
x=x*10+(s[i]-'0');
i++; ok=1;
}
if(ok)
{
nr.push(x); i--;
continue;
}
while(s[i]!='(' && pr[s[i]]<=pr[op.top()])
{
a=nr.top(); nr.pop();
b=nr.top(); nr.pop();
if(op.top()=='+') nr.push(a+b);
if(op.top()=='-') nr.push(b-a);
if(op.top()=='*') nr.push(a*b);
if(op.top()=='/') nr.push(b/a);
op.pop();
}
if(s[i]==')') op.pop();
else op.push(s[i]);
}
fo<<nr.top();
fi.close();
fo.close();
return 0;
}