Pagini recente » Cod sursa (job #1224523) | Cod sursa (job #183103) | Cod sursa (job #2895287) | Cod sursa (job #873715) | Cod sursa (job #2375377)
#include <bits/stdc++.h>
#define NMAX 100005
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
char C[NMAX];
int idx;
int expresie();
int factor()
{
if (C[idx] == '(')
return expresie();
int ret = 0;
while ('0' <= C[idx] && C[idx] <= '9')
ret = ret * 10 + C[idx++] - '0';
return ret;
}
int termen()
{
int ret = factor();
while (C[idx] == '*' || C[idx] == '/')
{
if (C[idx] == '*')
{
idx++;
ret *= factor();
}
else
{
idx++;
ret /= factor();
}
}
return ret;
}
int expresie()
{
idx++;
int ret = termen();
while (C[idx] != ')')
{
if (C[idx] == '+')
{
idx++;
ret += termen();
}
else
{
idx++;
ret -= termen();
}
}
idx++;
return ret;
}
int main()
{
fin >> C + 1;
C[0] = '(';
C[strlen(C)] = ')';
fout << expresie() << "\n";
return 0;
}