Cod sursa(job #3341592)

Utilizator mihaigeorgescuGeorgescu Mihai mihaigeorgescu Data 20 februarie 2026 11:48:28
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <stdio.h>
#include <cstring>
using namespace std;
FILE* fcin=fopen("evaluare.in", "r");
FILE* fout=fopen("evaluare.out", "w");
char s[100005], *p=s;
char op[2][3]=
{
    "+-",
    "*/",
};
inline int calc(int x, int y, char ch)
{
    if(ch=='+') return x+y;
    if(ch=='-') return x-y;
    if(ch=='*') return x*y;
    if(ch=='/') return x/y;
    return 0;
}
inline int eval(int k)
{
    int x,y;
    if(k==2)
        if(*p=='(')
            ++p, x=eval(0), ++p;
        else
            for(x=0; *p>='0' && *p<='9'; ++p)
               x=x*10+*p-'0';
    else
        for(x=eval(k+1); strchr(op[k],*p); x=y)
            y=calc(x,eval(k+1), *p++);
    return x;
}
int main()
{
    fgets(s,100005,fcin);
    fprintf(fout,"%d", eval(0));
    return 0;
}