Cod sursa(job #2943851)

Utilizator BuzdiBuzdugan Rares Andrei Buzdi Data 21 noiembrie 2022 18:36:20
Problema Evaluarea unei expresii Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>

using namespace std;

ifstream cin("evaluare.in");
ofstream cout("evaluare.out");

char s[100001];
int i;

int Expresie();
int Termen();
int Factor();
int Constanta();

int main()
{
	cin >> s;
	i = 0;
	cout << Expresie();

	return 0;
}

int Expresie()
{
	int rez = Termen();
	while (s[i] == '+' || s[i] == '-')
	{
		if (s[i] == '+')
			i++, rez += Termen();
		else
			i++, rez -= Termen();
	}
	
	return rez;
}

int Termen()
{
	int rez = Factor();
	while (s[i] == '*' || s[i] == '/')
	{
		if (s[i] == '*')
			i++, rez *= Factor();
		else
			i++, rez /= Factor();
	}

	return rez;
}

int Factor()
{
	int rez;
	if (s[i] == '(')
	{
		i++; // Sar peste rotunda deschisa
		rez = Expresie();
		i++; // Sar peste rotunda inchisa
	}
	else
		rez = Constanta();

	return rez;
}

int Constanta()
{
	int rez = 0;
	while (s[i] >= '0' && s[i] <= '9')
		rez = rez * 10 + (s[i] - '0'), i++;
	return rez;
}