Pagini recente » Cod sursa (job #2108177) | Cod sursa (job #3197997) | Cod sursa (job #379904) | Cod sursa (job #2259048) | Cod sursa (job #1814770)
#include <fstream>
#include <cstring>
using namespace std;
#define nmax 100010
char op[3][3] = { "+-", "*/", "^"};
char s[nmax], *p = s;
int eval( int a, int b, char o )
{
if (o=='+') return a+b;
if (o=='-') return a-b;
if (o=='/') return a/b;
if (o=='*') return a*b;
}
int expr(int ordin)
{
int x,y;char o;
if (ordin==2)
{
if (*p=='(')
++p,x=expr(0),++p;
else
for (x=0; '0'<=*p && *p<='9'; p++)
x=10*x+*p-'0';
}
else
{
x=expr(ordin+1);
for ( ; strchr(op[ordin],*p) && *p!=0; x=y )
y=eval( x, expr(ordin+1), *p++ );
}
return x;
}
int main()
{
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
fin.get(s,nmax);
fin.close();
fout<<expr(0)<<'\n';
fout.close();
return 0;
}