Pagini recente » Cod sursa (job #3288193) | Cod sursa (job #2438796) | Cod sursa (job #2607920) | Cod sursa (job #588973) | Cod sursa (job #2320240)
#include <fstream>
using namespace std;
ifstream f ("evaluare.in");
ofstream g ("evaluare.out");
int const NM = 1 + 1e5;
char s [NM];
int p;
int nextterm ();
int nextfact ();
int exp ();
int exp (){
int sum = nextterm ();
while (s [p] == '+' || s [p] == '-'){
if (s [p] == '+')
++ p , sum += nextterm ();
else
++ p , sum -= nextterm ();
}
return sum;
}
int nextterm (){
int prod = nextfact ();
while (s [p] == '*' || s [p] == '/'){
if (s [p] == '*')
++ p , prod *= nextfact ();
else
++ p , prod /= nextfact ();
}
return prod;
}
int nextfact (){
int semn = 1 , val = 0;
while (s [p] == '-')
semn = -semn , ++ p;
if (s [p] == '('){
++ p;
val = exp ();
++ p;
return semn * val;
}
while (isdigit (s [p]))
val = val * 10 + (s [p] - '0') , ++ p;
return semn * val;
}
int main()
{
f >> s;
f . close ();
g << exp ();
return 0;
}