Pagini recente » Cod sursa (job #1431094) | Cod sursa (job #2422784) | Cod sursa (job #3280724)
#include <iostream>
#include <fstream>
#include <cassert>
#include <cinttypes>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <climits>
using namespace std;
typedef int64_t i64;
typedef uint64_t u64;
typedef int8_t i8;
typedef int64_t i64;
ifstream fin("evaluare.in");
ofstream fout("evaluare.out");
string expr;
i64 do_multiply(int* idx);
i64 do_term(int* idx);
i64 do_add(int* idx) {
i64 res = do_multiply(idx);
while (*idx < expr.size() && (expr[*idx] == '+' || expr[*idx] == '-')) {
if (expr[*idx] == '+') {
(*idx)++;
res += do_multiply(idx);
}
else {
(*idx)++;
res -= do_multiply(idx);
}
}
return res;
}
i64 do_multiply(int* idx) {
i64 res = do_term(idx);
while (*idx < expr.size() && (expr[*idx] == '*' || expr[*idx] == '/')) {
if (expr[*idx] == '*') {
(*idx)++;
res *= do_term(idx);
}
else {
(*idx)++;
res /= do_term(idx);
}
}
return res;
}
i64 do_term(int* idx) {
i64 res = 0;
if (expr[*idx] == '(') {
(*idx)++;
res = do_add(idx);
(*idx)++;
}
else {
while (*idx < expr.size() && expr[*idx] >= '0' && expr[*idx] <= '9') {
res = res * 10 + (expr[*idx] - '0');
(*idx)++;
}
}
return res;
}
int main()
{
fin >> expr;
int idx = 0;
fout << do_add(&idx);
return 0;
}