Pagini recente » Cod sursa (job #2174152) | Cod sursa (job #2401290) | Cod sursa (job #368144) | Cod sursa (job #2294348) | Cod sursa (job #2982826)
#include <bits/stdc++.h>
#define nivelmaxim 2
using namespace std;
ifstream f("evaluare.in");
ofstream g("evaluare.out");
char op[4][4]={ "+-", "*/", "^", "" };
char s[100010], *p =s;
int eval(int a,int b,char c)
{
if(c=='+')
return a+b;
if(c=='-')
return a-b;
if(c=='*')
return a*b;
if(c=='/')
return a/b;
}
int expresie(int nivel)
{
int x, y;
if(nivel==nivelmaxim)
{
if(*p=='(' )
{
p++;
x=expresie(0);
p++;
}
else
{
x=0;
while(isdigit(*p))
{
x=x*10+(*p-'0');
p++;
}
}
}
else
{
x=expresie(nivel+1);
while(strchr(op[nivel],*p))
{
y=eval(x,expresie(nivel+1),*p++);
x=y;
}
}
return x;
}
int main()
{
f>>s;
s[strlen(s)]='\n';
g<<expresie(0);
return 0;
}