Pagini recente » Cod sursa (job #2809842) | Cod sursa (job #2763019) | Cod sursa (job #2979476) | Cod sursa (job #2671171)
#include <fstream>
#include <string>
using namespace std;
string sir;
int poz;
int eval();
int termen();
int factor();
int expr()
{
int val = termen();
while (poz < sir.size())
{
if (sir[poz] == '+')
{
++poz;
val += termen();
}
else // if (sir[poz] == '-')
{
++poz;
val -= termen();
}
}
return val;
}
int termen()
{
int val = factor();
while (poz < sir.size())
{
if (sir[poz] == '*')
{
++poz;
val *= factor();
}
else // if (sir[poz] == '/')
{
++poz;
val *= factor();
}
}
return val;
}
int factor()
{
int val = 0;
if (sir[poz] == '(')
{
++poz; // '('
val = expr();
++poz; // ')'
}
else
{
while ('0' <= sir[poz] && sir[poz] <= '9')
{
val = val * 10 + (sir[poz] - '0');
++poz;
}
}
return val;
}
int main()
{
ifstream cin("evaluare.in");
ofstream cout("evaluare.out");
cin >> sir;
cout << expr() << '\n';
return 0;
}