Pagini recente » Cod sursa (job #1905596) | Cod sursa (job #1356556) | Cod sursa (job #2282754) | Cod sursa (job #192939) | Cod sursa (job #208286)
Cod sursa(job #208286)
#include<stdio.h>
#define N 100100
char *p,s[N];
inline bool cifra(char c){
return c>='0' && c<='9';
}
int eval();
int factor(){
int x=0;
if(*p=='('){
++p;
x=eval();
++p;
}
while(cifra(*p))
x=x*10+((*p++)-'0');
return x;
}
int termen(){
int x=factor();
while(*p=='*' || *p=='/'){
if(*p=='*'){
++p;
x*=factor();
}
if(*p=='/'){
++p;
x/=factor();
}
}
return x;
}
int eval(){
int x=termen();
while(*p=='+' || *p=='-'){
if(*p=='+'){
++p;
x+=termen();
}
if(*p=='-'){
++p;
x-=termen();
}
}
return x;
}
int main(){
freopen("evaluare.in","r",stdin);
freopen("evaluare.out","w",stdout);
fgets(s,N,stdin);
p=s;
printf("%d\n",eval());
return 0;
}