Cod sursa(job #198749)

Utilizator alex_aurelia_31Neamtu Alexandra alex_aurelia_31 Data 14 iulie 2008 13:36:28
Problema Evaluarea unei expresii Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.66 kb
#include<stdio.h>
#include<string.h>
FILE *f,*g;

char s[100010],*p=s;
char op[4][4]={"+-","*/","^",""};
const long hmax=2;


long operatie(long x,long y,char c)
{
if(c=='+')
return x+y;
if(c=='-')
return x-y;
if(c=='*')
return x*y;
if(c=='/')
return x/y;

}

long expr(int h)
{
int x,y;
if(h==hmax)
if(*p=='(')
{
++p;
x=expr(0);
++p;
}
else
for(x=0;*p>='0' && *p<='9';++p)
x=x*10+*p-'0';
else
for(x=expr(h+1);strchr(op[h],*p);x=y)
y=operatie(x,expr(h+1),*p++);
return x;
}



int main()
{
f=fopen("evaluare.in","r");
g=fopen("evaluare.out","w");
fscanf(f,"%s",s);
//s[strlen(s)]='\0';
fprintf(g,"%ld\n",expr(0));
return 0;
}