Pagini recente » Cod sursa (job #1806994) | Cod sursa (job #2840255) | Cod sursa (job #1166148) | Cod sursa (job #1024312) | Cod sursa (job #1138270)
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string.h>
#define maxn 100000
using namespace std;
char oper[4][4]={"+-","/*","",""};
struct nod
{
char c;
int val;
nod* s;
nod* d;
nod(char a=0, int b=0, nod *q=0, nod *v=0) :
c(a), val(b), s(q), d(v){}
}*r;
char buff[maxn];
char *p;
int numerizare()
{
int num;
for(num=0;(*p<='9')&&(*p>='0'); p++)
{
num = num*10 + *p - '0';
}
return num;
}
void citire()
{
fgets(buff, maxn,stdin);
p=buff;
}
nod *construire(int niv)
{
nod *fin, *aux;
if(niv==2)
if(*p=='(')
{
p++;
fin=construire(0);
p++;
}
else
{
fin=new nod(0,numerizare(),0,0);
// g<<fin->val;
}
else
{
fin=construire(niv+1);
while(strchr( oper[niv], *p ))
{
aux=new nod(*p++, 0, fin, construire(niv+1));
fin=aux;
}
}
return fin;
}
int explore(nod* n)
{
switch(n->c)
{
case '+':return explore(n->d) + explore(n->s);
case '*':return explore(n->d) * explore(n->s);
case '-':return explore(n->s) - explore(n->d);
case '/':return explore(n->s) / explore(n->d);
default:return n->val;
}
}
int main()
{
freopen( "evaluare.in", "r", stdin );
freopen( "evaluare.out", "w", stdout );
citire();
r=construire(0);
printf("%d \n",explore(r));
return 0;
}