Cod sursa(job #1131042)

Utilizator cont_de_testeCont Teste cont_de_teste Data 28 februarie 2014 17:26:02
Problema Evaluarea unei expresii Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.73 kb
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

ifstream fin("evaluare.in");
ofstream fout("evaluare.out");

int n, p = 1;
char a[100005];
int g1(), g2(), g3();

int g1() {
	int t = g2();
	while (a[p] == '+' || a[p] == '-') {
		++p;
		if (a[p - 1] == '+')
			t += g2();
		else
			t -= g2();
	}
	return t;
}

int g2() {
	int t = g3();
	while (a[p] == '*' || a[p] == '/') {
		++p;
		if (a[p - 1] == '*')
			t *= g3();
		else
			t /= g3();
	}
	return t;
}

int g3() {
	int t = 0;
	if (a[p] == '(') {
		++p;
		t = g1();
		++p;
	} else {
		while ('0' <= a[p] && a[p] <= '9') {
			t = t * 10 + a[p] - '0';
			++p;
		}
	}
	return t;
}

int main() {
	fin.getline(a + 1, 100002);
	fout << g1();
	fin.close();
	fout.close();
}