Pagini recente » Cod sursa (job #2368679) | Cod sursa (job #140708) | Cod sursa (job #2440806) | Cod sursa (job #216638) | Cod sursa (job #772858)
Cod sursa(job #772858)
/*
Evaluarea unei expresii.
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#define LEN 100010
#define NR 4
#define NIV 2
using namespace std;
char expresie[LEN];
char *p = expresie;
char op[NIV][NR] = {"+-", "*/"};
int calculeaza(int operand_1, int operand_2, char tip_op) {
int rez = 0;
switch (tip_op) {
case '+': rez = operand_1 + operand_2; break;
case '-': rez = operand_1 - operand_2; break;
case '*': rez = operand_1 * operand_2; break;
case '/': rez = operand_1 / operand_2; break;
}
return rez;
}
int evalueaza (int nivel) {
int numar, aux;
if (nivel == NIV) {
if (*p == '(') {
++p;
numar = evalueaza(0);
++p;
}
else {
numar = 0;
while ('0' <= *p && *p <= '9') {
numar = numar * 10 + *p - '0';
++p;
}
}
}
else
for(numar = evalueaza(nivel + 1); strchr(op[nivel], *p); numar = aux)
aux = calculeaza(numar, evalueaza(nivel + 1), *p++);
return numar;
}
int main () {
FILE *f_in = fopen("evaluare.in", "r");
FILE *f_out = fopen("evaluare.out", "w");
fgets(expresie, LEN, f_in);
fprintf(f_out, "%d\n", evalueaza(0));
return 0;
}