Pagini recente » Cod sursa (job #879600) | Cod sursa (job #2461697) | Cod sursa (job #2506841) | Cod sursa (job #3225450)
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;
#define pb push_back
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
int p;
string s;
int factor(), termen(), suma();
int main()
{
fin.tie(0); fout.tie(0);
ios_base::sync_with_stdio(0);
fin >> s;
fout << suma();
return 0;
}
int factor(){
int ret = 0;
if (s[p] == '('){
p++; ret = suma(); p++;
return ret;
}
for (; '0' <= s[p] && s[p] <= '9'; p++) ret = ret * 10 + s[p] - '0';
return ret;
}
int termen(){
int ret = factor();
while (s[p] == '*' || s[p] == '/'){
if (s[p] == '*') { p++; ret *= factor(); }
else { p++; ret /= factor(); }
}
return ret;
}
int suma(){
int ret = termen();
while (s[p] == '+' || s[p] == '-'){
if (s[p] == '+') { p++; ret += termen(); }
else { p++; ret -= termen(); }
}
return ret;
return 0;
}