Cod sursa(job #1671459)

Utilizator TudorVersoiuVersoiu Tudor Sorin TudorVersoiu Data 1 aprilie 2016 19:11:50
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <iostream>
#include <fstream>

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

char sir[100002];
char* act;

int total;

int expresie() {
    int result = 1; char sign = '+';

    if ( *act == '-' ) { act++; sign = '-'; }
    if ( *act == '+' ) { act++; }

    do {
        int x = 0; char op = '*';

        if ( *act == '*' ) { act++; op = '*'; }
        if ( *act == '/' ) { act++; op = '/'; }

        while ( '0' <= *act && *act <= '9' ) {
            x = x*10 + *act - '0';
            act++;
        }


        if ( *act == '(' ) {
            act++;
            while ( *act != ')' )
                x += expresie(); act++;
        }


        if ( op == '*' ) result *= x;
        if ( op == '/' ) result /= x;
    } while ( *act == '*' or *act == '/' );

    if ( sign == '-' )
        return -result;
    else return result;
}

int main() {
    f.getline(sir, 100000);


    act = sir;
    while ( *act ) {
        total += expresie();
    }
    g << total;
}