Pagini recente » Cod sursa (job #12577) | Cod sursa (job #2839050) | Cod sursa (job #2127739) | Cod sursa (job #309867) | Cod sursa (job #3291526)
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
using namespace std;
#define ll long long
#define hmax 2
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
string s;
long long pos = 0;
char operand[4][4] = { "+-", "*/", "^", "" };
long operatie(long x, long y, char c) {
switch (c) {
case '+': return x + y;
case '-': return x - y;
case '*': return x * y;
case '/': return x / y;
}
return 0;
}
long element();
long eval(long h) {
long r = (h == hmax) ? element() : eval(h + 1);
while (pos < s.length() && strchr(operand[h], s[pos]))
r = operatie(r, eval(h + 1), s[pos++]);
return r;
}
long element() {
ll r = 0;
if (s[pos] == '(') {
++pos;
r = eval(0);
++pos;
} else {
while (pos < s.length() && isdigit(s[pos]))
r = r * 10 + (s[pos++] - '0');
}
return r;
}
int main() {
getline(fin, s);
fout << eval(0) << '\n';
return 0;
}