Pagini recente » Cod sursa (job #957410) | Cod sursa (job #1866071) | Cod sursa (job #2482156) | Cod sursa (job #2469454) | Cod sursa (job #2483998)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
int eval(),termen(),factor();
char *p;
int eval()
{
int res=termen();
while(*p!='\00' && *p!=')')
{
int op=(*p=='+')? 1:0;
++p;
int t=termen();
if(op)
res+=t;
else
res-=t;
}
++p;
return res;
}
int termen()
{
int t=0;
if(*p=='(')
{
++p;
t=eval();
while(*p=='*' || *p=='/')
{
int op=(*p=='*')? 1:0,x=0;
++p;
while(*p>='0' && *p<='9')
x=x*10+(*p-'0'),++p;
if(op)
t*=x;
else
t/=x;
}
return t;
}
while(*p>='0' && *p<='9')
t=t*10+(*p-'0'),++p;
while(*p=='*' || *p=='/')
{
int op=(*p=='*')? 1:0,x=0;
++p;
while(*p>='0' && *p<='9')
x=x*10+(*p-'0'),++p;
if(op)
t*=x;
else
t/=x;
}
return t;
}
int main()
{
char s[100000];
p=s;
f>>s;
g<<eval();
return 0;
}