Pagini recente » Cod sursa (job #449884) | Cod sursa (job #2504454) | Cod sursa (job #614257) | Cod sursa (job #2257806) | Cod sursa (job #3284268)
#include <bits/stdc++.h>
using namespace std;
string s;
int pos = 0;
int calcpm();
// ( n
int calcbn()
{
if(s[pos] == '(')
{
pos++;
int val = calcpm();
pos++;
return val;
}
else
{
int nr = 0;
while(s[pos] >= '0' && s[pos] <= '9')
{
nr = nr * 10 + (s[pos] - '0');
pos++;
}
return nr;
}
}
// * /
int calcmd()
{
int val = calcbn();
while(s[pos] == '*' or s[pos] == '/')
{
if(s[pos] == '*')
{
pos++;
int nvl = calcbn();
val *= nvl;
}
else
{
pos++;
int nvl = calcbn();
val /= nvl;
}
}
return val;
}
// + -
int calcpm()
{
int val = calcmd();
while(s[pos] == '+' or s[pos] == '-')
{
if(s[pos] == '+')
{
pos++;
int nvl = calcmd();
val += nvl;
}
else
{
pos++;
int nvl = calcmd();
val -= nvl;
}
}
return val;
}
int main()
{
freopen("evaluare.in", "r", stdin);
freopen("evaluare.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> s;
cout << calcpm();
}