Cod sursa(job #770519)

Utilizator NicuCJNicu B. NicuCJ Data 23 iulie 2012 12:48:24
Problema Evaluarea unei expresii Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 2.75 kb
#include <fstream>
#include <stack>
#include <cstring>
using namespace std;
stack<char> stiva;
stack<int> stiva2;
stack<int> stiva3, stiva4;
char a[100005];
int i, nr;
bool nrluat;
int main()
{
	ifstream f("evaluare.in");
	ofstream g("evaluare.out");
	f.getline(a, 100005);
	int x=strlen(a);
	for(i=0; i<x; i++)
	{
		bool ok=false;
		while(isdigit(a[i]))
		{
			ok=true;
			nr=nr*10+(a[i]-48);
			i++;
		}
		if(ok)
		stiva2.push(nr);
		nr=0;
		if(!isdigit(a[i]))
		{
		if(a[i]=='+' || a[i]=='-')
		{
			if(!stiva.empty() && ( stiva.top()=='+' || stiva.top()=='-' || stiva.top()=='*' || stiva.top()=='/'  ) )
			{
				if(stiva.top()=='+')
				{
					stiva2.push(1000000001);
				}
				else if(stiva.top()=='-')
				{
					int a1=0, b1=0;
					stiva2.push(1000000002);
				}
				else if(stiva.top()=='*')
				{
					int a1=0, b1=0;
					stiva2.push(1000000003);
				}
				else if(stiva.top()=='/')
				{
					int a1=0, b1=0;
					stiva2.push(1000000004);
				}
				stiva.pop();
			}
			stiva.push(a[i]);
		}
		else if(a[i]=='*' || a[i]=='/')
		{
			if(!stiva.empty() && ( stiva.top()=='*' || stiva.top()=='/' ) )
			{
				if(stiva.top()=='*')
				{
					int a1=0, b1=0;
					stiva2.push(1000000003);
				}
				else
				{
					int a1=0, b1=0;
					stiva2.push(1000000004);
				}
				stiva.pop();
			}
			stiva.push(a[i]);
		}
		else if(a[i]=='(')
		{
			stiva.push(a[i]);
		}
		else if(a[i]==')')
		{
			while(!stiva.empty() && stiva.top()!='(')
			{
				if(stiva.top()=='+')
				{
					int a1=0, b1=0;
					stiva2.push(1000000001);
				}
				else if(stiva.top()=='-')
				{
					stiva2.push(1000000002);
				}
				else if(stiva.top()=='*')
				{
					int a1=0, b1=0;
					stiva2.push(1000000003);
				}
				else
				{
					stiva2.push(1000000004);
				}
				stiva.pop();
			}
			stiva.pop();
		}
	}
	}
	while(!stiva.empty())
	{
		if(stiva.top()=='+')
		{
			int a1=0, b1=0;
			stiva2.push(1000000001);
		}
		else if(stiva.top()=='-')
		{
			int a1=0, b1=0;
			stiva2.push(1000000002);
		}
		else if(stiva.top()=='*')
		{
			int a1=0, b1=0;
			stiva2.push(1000000003);
		}
		else
		{
			stiva2.push(1000000004);
		}
		stiva.pop();
	}
	while(!stiva2.empty())
	{
		stiva3.push(stiva2.top());
		stiva2.pop();
	}
	while(!stiva3.empty())
	{
		if(stiva3.top()<=1000000000)
		{
			stiva4.push(stiva3.top());
		}
		else
		{
			int x1=stiva4.top();
			stiva4.pop();
			int x2=stiva4.top();
			stiva4.pop();
			if(stiva3.top()==1000000001)
				stiva4.push(x1+x2);
			if(stiva3.top()==1000000002)
				stiva4.push(x2-x1);
			if(stiva3.top()==1000000003)
				stiva4.push(x1*x2);
			if(stiva3.top()==1000000004)
				stiva4.push(x2/x1);
		}
		stiva3.pop();
	}
	g<<stiva4.top();
}