Pagini recente » Cod sursa (job #2791866) | Cod sursa (job #2173316) | Cod sursa (job #3240732) | Cod sursa (job #2240185) | Cod sursa (job #2209797)
#include <fstream>
#include <string>
using namespace std;
char const in [] = "evaluare.in";
char const out [] = "evaluare.out";
int const NM = 1e5 + 7;
char st [NM];
int termen ();
int factor ();
int point;
inline bool check1 (char c)
{
return c == '+' || c == '-';
}
inline bool check2 (char c)
{
return c == '*' || c == '/';
}
inline int eval ()
{
int a = termen ();
while(check1 (st [point]))
{
if (st [point] == '+')
{
++ point;
a += termen ();
break;
}
else
{
++ point;
a -= termen ();
break;
}
}
return a;
}
inline int termen ()
{
int a = factor ();
while( check2 (st [point]))
{
if (st [point] == '*')
{
++ point;
a = a * factor () ;
break;
}
else
{
++ point;
a = a / factor () ;
break;
}
}
return a;
}
inline int factor ()
{
int a = 0;
if(st [point] == '(')
{
++ point;
a = eval ();
++ point;
}
else
{
while(isdigit (st [point]))
{
a = a * 10 + (st [point] - '0');
++ point;
}
}
return a;
}
int main()
{
fstream f;
fstream g;
f . open (in , ios :: in);
g . open (out , ios :: out);
f >> st;
g << eval () << ' ';
return 0;
}