Pagini recente » Cod sursa (job #2988422) | Cod sursa (job #1070417) | Cod sursa (job #1872827) | Cod sursa (job #3241419) | Cod sursa (job #1989616)
#include <fstream>
#include <string>
#include <stack>
using namespace std;
ifstream in("evaluare.in");
ofstream out("evaluare.out");
char ch,lch;
int polo[100005],pr[300],op[100005];
int top,tpolo,nr;
void eval()
{
switch(op[top])
{
case '+':
polo[tpolo-1]+=polo[tpolo];
break;
case '-':
polo[tpolo-1]-=polo[tpolo];
break;
case '*':
polo[tpolo-1]*=polo[tpolo];
break;
case '/':
polo[tpolo-1]/=polo[tpolo];
break;
}
tpolo--;
top--;
}
void poloneza()
{
pr['+']=pr['-']=1;
pr['*']=pr['/']=2;
pr['(']=top=tpolo=nr=0;
lch=0;
while(in>>ch)
{
if('0'<=lch && lch<='9' && !('0'<=ch && ch<='9'))
polo[++tpolo]=nr,nr=0;
if('0'<=ch && ch<='9') nr=nr*10+ch-'0';
else if(ch=='(') op[++top]=ch;
else if(ch==')')
{
while(op[top]!='(') eval();
top--;
}
else
{
while(top>0 && pr[ch]<=pr[op[top]]) eval();
op[++top]=ch;
}
lch=ch;
}
if(nr>0) polo[++tpolo]=nr;
while(top) eval();
out<<polo[1]<<'\n';
}
int main()
{
poloneza();
return 0;
}