Cod sursa(job #715316)

Utilizator andreipasalauPasalau Andrei andreipasalau Data 16 martie 2012 23:58:03
Problema Evaluarea unei expresii Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
//#include "stdafx.h"
#include<fstream>
#include<string>
using namespace std;



ifstream f("evaluare.in");
ofstream g("evaluare.out");

char c;

long numar();
long prod();

long expr(){
	long rez = prod();
	while((c == '+'|| c == '-')&&!f.eof()){
		if(c == '+'){
			f>>c;
			rez += prod();
		}
		if(c == '-'){
			f >> c;
			rez -= prod();
		}
	}
	return rez;
}


long prod(){
	long rez = numar();
	while((c == '*'|| c == '/')&&!f.eof()){
		if(c == '*'){
			f >> c;
			rez *= numar();
		}
		if(c == '/'){
			f >> c;
			rez /= numar();
		}
	}
	return rez;
}


long numar(){
	long nr = 0;
	if(c == '('){
		f>>c;
		nr = expr();
		f>>c;
	}else{
		while(c>'0'&&c<'9'&&!f.eof()){
			nr = nr*10+c-'0'; 
			f>>c;
		}
	}
	return nr;
}


int main()
{	
	f>>c;
	g<<expr();
	f.close();
	g.close();
	return 0;
}