Cod sursa(job #677482)

Utilizator thebest001Neagu Rares Florian thebest001 Data 10 februarie 2012 11:45:39
Problema Evaluarea unei expresii Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.06 kb
//marco
#include <string.h>
#include <stack>
#include <queue>
#include <string>
#include <stdio.h>
using namespace std;
int a[27];

queue<char>polo;
stack<char>rofl;

bool inline isnum(char x){
	return x>='0' && x<='9';
}
int inline op(char x){
	switch (x){
	case '-':return 1;break;
	case '+':return 1;break;
	case '/':return 2;break;
	case '*':return 2;break;
	case '(':return 3;break;
	}
	return -1;
}

bool inline canpush(char x){
	if (x==')') return false;
	if (rofl.empty())
		return true;
	if (rofl.top()=='(') 
		return true;
	if (op(rofl.top())<op(x))
		return true;
	return false;
}

int inline operatie(int a,int b,char op){
	switch (op){
	case '-':return a-b;break;
	case '+':return a+b;break;
	case '/':return a/b;break;
	case '*':return a*b;break;
	}
}


int main(){
	freopen("polo.in","r",stdin);
	freopen("polo.out","w",stdout);
	char x[100001]={0};
	gets(x);
	char *p=&x[0];
	while (*p!=0){
		if (isnum(p[0]))
			polo.push(p[0]);
		else{
			polo.push('|');
			if (canpush(p[0])){
				rofl.push(p[0]);
			} else{
				if (p[0]==')'){
					while (rofl.top()!='('){
						polo.push(rofl.top());
						rofl.pop();
					}
					rofl.pop();
				} else{
					polo.push(rofl.top());
					rofl.pop();
					rofl.push(p[0]);
				}
			}
		}
		p++;
	}
	while (!rofl.empty()){
		if (rofl.top()!='('){
			polo.push(rofl.top());
			
		}
		rofl.pop();
	}
	/*while (!polo.empty()){
		printf("%c",polo.front());
		polo.pop();
	}
	return 0;*/
	stack<int> elemente;
	int nr;
	bool added1=false;
	long solutie=0;
	nr=0;
	while (!polo.empty()){
		char x=polo.front();
		if (x=='|'){
			if (added1){
				added1=false;
				elemente.push(nr);
				nr=0;
			}
		}
		if (x!='|' && !isnum(x)){

			int a1,a2;
			a1=elemente.top();elemente.pop();
			a2=elemente.top();elemente.pop();
			elemente.push(operatie(a2,a1,x));
		}
		if (x!='|' && isnum(x)){
			nr=nr*10+x-'0';
			added1=true;
		}
		polo.pop();
	}
	if (!elemente.empty())
		printf("%d",elemente.top());	
	else
		printf("DAFUQ");
	return 0;
}