Pagini recente » Cod sursa (job #1405108) | Cod sursa (job #2762004) | Cod sursa (job #2774753) | Cod sursa (job #2167525) | Cod sursa (job #3196994)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
string s;
int i = 0;
int expresie();
int termen();
int factor();
int expresie()
{
int ans = termen();
while (s[i] == '+' || s[i] == '-')
{
if (s[i] == '+')
{
i++;
ans += termen();
}
else
{
i++;
ans -= termen();
}
}
return ans;
}
int termen()
{
int ans = factor();
while (s[i] == '*' || s[i] == '/')
{
if (s[i] == '*')
{
i++;
ans *= factor();
}
else
{
i++;
ans /= factor();
}
}
return ans;
}
int factor()
{
if (s[i] == '(')
{
i++;
int ans = expresie();
i++;
return ans;
}
else
{
int nr = 0;
while (isdigit(s[i]) == 1)
{
nr = nr * 10 + s[i] -'0';
i++;
}
return nr;
}
}
int main()
{
getline(fin, s);
s = ' ' + s;
i = 1;
fout << expresie();
return 0;
}