Pagini recente » Cod sursa (job #3233156) | Cod sursa (job #640267) | Cod sursa (job #3032704) | Cod sursa (job #2193688) | Cod sursa (job #2809381)
#include <bits/stdc++.h>
using namespace std;
ifstream in("evaluare.in");
ofstream out("evaluare.out");
string s;
int pos = 0;
int eval();
int fact()
{
if (s[pos] == '(')
{
pos++;
int x = eval();
pos++;
return x;
}
else
{
int nr = 0;
while (s[pos] >= '0' and s[pos] <= '9')
{
nr *= 10;
nr += s[pos] - '0';
pos++;
}
return nr;
}
}
int term()
{
int x = fact();
while (s[pos] == '*' or s[pos] == '/')
{
if (s[pos] == '*')
{
pos++;
x *= fact();
}
else
{
pos++;
x /= fact();
}
}
return x;
}
int eval()
{
int x = term();
while (s[pos] == '+' or s[pos] == '-')
{
if (s[pos] == '+')
{
pos++;
x += term();
}
else
{
pos++;
x -= term();
}
}
return x;
}
int main()
{
in >> s;
s += "#";
out << eval();
return 0;
}