Cod sursa(job #1814933)

Utilizator ShutterflyFilip A Shutterfly Data 24 noiembrie 2016 17:56:43
Problema Evaluarea unei expresii Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
#include <iostream>
#include <stdio.h>

using namespace std;

//GLOBALS DIVISION//
char Storage[100001];
int Level;

//PROTOTYPE DIVISION//
int Express();
int Chunk();
int Particle();

//PROCEDURES DIVISION//
int Express() {
    int Sum = Chunk();
    while (Storage[Level] == '+' || Storage[Level] == '-') {
        if (Storage[Level] == '+') {
            Level++;
            Sum += Chunk();
        }
        if (Storage[Level] == '-') {
            Level++;
            Sum -= Chunk();
        }
    }
    return Sum;
}

int Chunk() {
    int Prod = Particle();
    while (Storage[Level] == '*' || Storage[Level] == '/') {
        if (Storage[Level] == '*') {
            Level++;
            Prod *= Chunk();
        }
        if (Storage[Level] == '/') {
            Level++;
            Prod /= Chunk();
        }
    }
    return Prod;
}

int Particle() {
    int Number = 0, Sign = 1;
    while (Storage[Level] == '-') {
        Level++;
        Sign = -Sign;
    }

    if (Storage[Level] == '(') {
        Level++;
        Number = Express();
        Level++;
        return Sign * Number;
    }

    while (Storage[Level] >= '0' && Storage[Level] <= '9') {
        Number = Number * 10 + Storage[Level] - '0';
        Level++;
    }
    return Sign * Number;
}

int main() {
    freopen("evaluare.in", "r", stdin);
    freopen("evaluare.out", "w", stdout);
    char T,lvl = 0;
    while(cin>>T) {
        Storage[lvl] = T;
        lvl++;
    }
    cout << Express();
}