Pagini recente » Cod sursa (job #1883080) | Cod sursa (job #2063945) | Cod sursa (job #2686757) | Monitorul de evaluare | Cod sursa (job #1201708)
#include<stdio.h>
#include<stack>
#include<vector>
using namespace std;
vector<int> v;
stack<int> init;
stack<int> fin;
int main()
{
freopen("evaluare.in","r",stdin);
freopen("evaluare.out","w",stdout);
char c;
int a,b,nr;
while(scanf("%c",&c)!='\n')
{
if(c=='\n')
break;
if(c>47 && c<58)
{
nr=0;
while(c>47 && c<58)
{
nr=nr*10+(int(c)-'0');
scanf("%c",&c);
}
v.push_back(nr);
}
if(c=='(')
init.push(-1);
if(c==')')
{
while(init.top()!=-1)
{
v.push_back(init.top());
init.pop();
}
init.pop();
}
while(!init.empty() && (init.top()==-5 || init.top()==-6))
{
v.push_back(init.top());
init.pop();
}
if(c=='+')
init.push(-3);
if(c=='-')
init.push(-4);
if(c=='*')
init.push(-5);
if(c=='/')
init.push(-6);
}
while(!init.empty())
{
v.push_back(init.top());
init.pop();
}
while(!v.empty())
{
if(v.front()>=0)
{
fin.push(v.front());
v.erase(v.begin());
}
else
{
a=fin.top();
fin.pop();
b=fin.top();
fin.pop();
switch(v.front())
{
case -3:{a=a+b;break;}
case -4:{a=b-a;break;}
case -5:{a=a*b;break;}
case -6:{a=b/a;break;}
}
v.erase(v.begin());
fin.push(a);
}
}
printf("%d\n",fin.top());
return 0;
}