Pagini recente » Cod sursa (job #2911413) | Cod sursa (job #2043298) | Cod sursa (job #3147626) | Cod sursa (job #1000800) | Cod sursa (job #1341500)
#include<stdio.h>
#include<stack>
#include<queue>
using namespace std;
queue<long> q;
stack<long> init;
stack<long> fin;
bool prioritate(long x,char c)
{
switch(x)
{
case -3:
{
return 0;
break;
}
case -4:
{
if(c!='+')
return 0;
else
return 1;
break;
}
case -5:
{
if(c=='*' || c=='/')
return 0;
else
return 1;
break;
}
case -6:
{
if(c!='/')
return 1;
else
return 0;
break;
}
}
return 0;
}
int main()
{
freopen("evaluare.in","r",stdin);
freopen("evaluare.out","w",stdout);
char c;
long 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);
}
q.push(nr);
}
if(c=='(')
init.push(-1);
if(c==')')
{
while(init.top()!=-1)
{
q.push(init.top());
init.pop();
}
init.pop();
}
while(!init.empty() && prioritate(init.top(),c))
{
q.push(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())
{
q.push(init.top());
init.pop();
}
while(!q.empty())
{
if(q.front()>=0)
{
fin.push(q.front());
q.pop();
}
else
{
a=fin.top();
fin.pop();
b=fin.top();
fin.pop();
switch(q.front())
{
case -3:{a=b+a;break;}
case -4:{a=b-a;break;}
case -5:{a=b*a;break;}
case -6:{a=b/a;break;}
}
q.pop();
fin.push(a);
}
}
printf("%ld\n",fin.top());
return 0;
}