Cod sursa(job #599837)

Utilizator andrianAndrian andrian Data 29 iunie 2011 18:18:30
Problema Evaluarea unei expresii Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

char exp[100001];

void citire(){
    ifstream in("evaluare.in");
    in >> exp;
    exp[strlen(exp)] = '\0';
    in.close();
}

long expresie(int & k);
long termen(int & k);

long factor(int & k){
    long t=0;
    if(exp[k] == '('){
        t=expresie(++k);
        +k;
    }else{
        for(;isdigit(exp[k]);++k)
            t=t*10+exp[k]-'0';
        return t;
    };
}

long termen(int & k){
    long produs=factor(k);
    while(exp[k] == '*' || exp[k] == '/')
    switch(exp[k]){
        case '*': produs*=factor(++k);
        break;
        case '/': produs/=factor(++k);
        break;
    }
    return produs;
}

long expresie(int & k){
    long suma=termen(k);
    char c='+';
    while(exp[k] == '+' || exp[k] == '-')
    switch(exp[k]){
        case '+':suma+=termen(++k);
        break;
        case '-':suma-=termen(++k);
        break;
    }
    ++k;
    return suma;
}

int main()
{
    citire();
    ofstream out("evaluare.out");
    int l=0,s=expresie(l);
    cout << s;
    out.close();
    return 0;
}