Cod sursa(job #2146670)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 28 februarie 2018 09:36:36
Problema Ciurul lui Eratosthenes Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>
#include <iostream>
#define LL long long

using namespace std;

ifstream f("evaluare.in");
ofstream g("evaluare.out");

char s[100005], *k = s;

LL sum();
LL fact();
LL eval();

LL fact() {
    LL sol = 0;
    if (*k == '(') {
        k++;
        sol = eval();
        k++;
    } else {
        while (*k >= '0' && *k <= '9')
            sol = sol*10+*k-'0', k++;
    }
    return sol;
}

LL sum() {
    LL sol = fact();
    while (*k == '*' || *k == '/')
        if (*k == '*') k++, sol *= fact();
        else k++, sol /= fact();
    return sol;
}

LL eval() {
    LL sol = sum();
    while (*k == '+' || *k == '-')
        if (*k == '+') k++, sol += sum();
        else k++, sol -= sum();
    return sol;
}

int main() {
    f >> s;
    g << eval();
}