Cod sursa(job #1868704)

Utilizator mihai.alphamihai craciun mihai.alpha Data 5 februarie 2017 11:30:03
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define SIZE 100000

FILE *fin = fopen("evaluare.in", "r"), *fout = fopen("evaluare.out", "w");

char *s = new char[SIZE + 1];

int F(int x)  {
    int R;
    char ch;
    if(x == 0)  {///Pt adunare / scadere exclusiv
        R = F(1);
        for(ch = *s;ch == '+' || ch == '-';ch = *s)
            s++, R = ch == '+' ? R + F(1) : R - F(1);
    }
    else if (x == 1)  {///Pt inmultire / impartire exclusiv
        R = F(2);
        for(ch = *s;ch == '*' || ch == '/';ch = *s)
            s++, R = ch == '*' ? R * F(2) : R / F(2);
    }
    else if(*s == '(')  {///Cand incepe o parantezare si trb sa o evaluam
        s++;
        R = F(0);
        s++;
    }
    else  {///Cand avem de parsat un numar exclusiv
        for(R = 0;isdigit(*s);s++)
            R = 10 * R + *s - '0';
    }

    return R;
}

int main()  {
    fgets(s, SIZE + 1, fin);
    fprintf(fout, "%d", F(0));

    fclose(fin);
    fclose(fout);
    return 0;
}