Cod sursa(job #462689)

Utilizator voikybodea voichita voiky Data 12 iunie 2010 19:13:12
Problema Evaluarea unei expresii Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include<fstream>
using namespace std;
ifstream f("evaluare.in");ofstream g("evaluare.out");

char s[100001];
int i=0;

int calculez(int x,int y,char operatie)
{
	if(operatie=='+')return x+y;
	if(operatie=='-')return x-y;
	if(operatie=='*')return x*y;
	if(operatie=='/')return x/y;
	return -1;
}

int expresie(int k)
{
	int x;
	if(k==2)
		if(s[i]=='(')
		{
			i++;
			x=expresie(k);
		}	
		else {x=0; while(s[i]>='0' && s[i]<='9'){x=x*10+s[i]-'0';i++;}}
	else
	{	
		x=expresie(k+1);
		while(s[i] && strchr("+-*/",s[i]))
		{
			char c=s[i];
			i++;
			x=calculez(x,expresie(k+1),c);
		}
	}	
	return x;
}

int main()
{
	f.get(s,100001);
	g<<expresie(0);
	f.close();g.close();
	return 0;
}