Pagini recente » Borderou de evaluare (job #3012081) | Borderou de evaluare (job #3338821) | Borderou de evaluare (job #3018194) | Borderou de evaluare (job #1418615) | Cod sursa (job #3341592)
#include <stdio.h>
#include <cstring>
using namespace std;
FILE* fcin=fopen("evaluare.in", "r");
FILE* fout=fopen("evaluare.out", "w");
char s[100005], *p=s;
char op[2][3]=
{
"+-",
"*/",
};
inline int calc(int x, int y, char ch)
{
if(ch=='+') return x+y;
if(ch=='-') return x-y;
if(ch=='*') return x*y;
if(ch=='/') return x/y;
return 0;
}
inline int eval(int k)
{
int x,y;
if(k==2)
if(*p=='(')
++p, x=eval(0), ++p;
else
for(x=0; *p>='0' && *p<='9'; ++p)
x=x*10+*p-'0';
else
for(x=eval(k+1); strchr(op[k],*p); x=y)
y=calc(x,eval(k+1), *p++);
return x;
}
int main()
{
fgets(s,100005,fcin);
fprintf(fout,"%d", eval(0));
return 0;
}