Cod sursa(job #1370029)

Utilizator DanBrezeanuBrezeanu Dan DanBrezeanu Data 3 martie 2015 12:42:54
Problema Evaluarea unei expresii Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 6.04 kb
#include <cstdio>
#include <cctype>
#include <string>
#include <stack>

using namespace std;

stack <char> op;
stack <int> polo;


int priority(char c)
{


	if(c == '+'  or c == '-')
	return 1;

	if(c== '*' or c=='/')
	return 2;

	if(c=='(' or c==')')
	return 3;

    return 0;
}

int main()
{
	freopen("evaluare.in","r",stdin);
	freopen("evaluare.out","w",stdout);


	int p;
	int C=0,aux1,aux2,aux3;
    char c;
    bool ok;
        while(scanf("%c",&c)!=EOF)
	{

	    if(c == '\n')
        {
            if(C)
                polo.push(C);
             break;

        }

		if(isdigit(c))
		{
			C= C*10 + c-'0';
			continue;

		}
		else
		{
			if(C)
			{
				polo.push(C);
				C=0;

			}

            if(op.empty())
            {
                 op.push(c);
                 continue;
            }


			if(c == '(')
            {
                op.push(c);
                continue;
            }


			if(c == ')')
			{
				while(op.top() != '(')
						{

							aux1=polo.top();
							polo.pop();
							aux2=polo.top();
							polo.pop();

							switch(op.top())
							{
                            case '+':
                                aux3=aux1+aux2;
                                polo.push(aux3);
                                op.pop();
                                break;

                            case '-':
                                aux3=aux2-aux1;

                                polo.push(aux3);
                                break;

                            case '*':
                                aux3=aux1*aux2;
                                polo.push(aux3);
                                break;

                            case '/':
                                if(aux1 == 0 or aux2==0)
                                    aux3=0;
                                else
                                    aux3=aux2/aux1;
                                polo.push(aux3);
                                 op.pop();

                                break;
							}

						}
				op.pop();
			}
				else
				{
					p= priority(c);
					ok=1;
                while( !op.empty() and p <= priority(op.top()) )
					{

						if(p == priority(op.top()))
                        {

                            aux1=polo.top();
							polo.pop();
							aux2=polo.top();
							polo.pop();

							switch(op.top())
							{
                            case '+':
                                aux3=aux1+aux2;
                                polo.push(aux3);
                                break;

                            case '-':
                                aux3=aux2-aux1;

                                polo.push(aux3);
                                break;

                            case '*':
                                aux3=aux1*aux2;
                                polo.push(aux3);
                                break;

                            case '/':
                                if(aux1 == 0 or aux2==0)
                                    aux3=0;
                                else
                                    aux3=aux2/aux1;

                                polo.push(aux3);
                                 op.pop();

                                break;
							}

                            op.pop();

                            continue;
                        }



						if(op.top() == '(' )
                        {
                           op.push(c);
                           ok=0;
                           break;
                        }




                            aux1=polo.top();
							polo.pop();
							aux2=polo.top();
							polo.pop();

							switch(op.top())
							{
                            case '+':
                                aux3=aux1+aux2;
                                polo.push(aux3);
                                break;

                            case '-':
                                aux3=aux2-aux1;

                                polo.push(aux3);
                                break;

                            case '*':
                                aux3=aux1*aux2;
                                polo.push(aux3);
                                break;

                            case '/':
                                if(aux1 == 0 or aux2==0)
                                    aux3=0;
                                else
                                    aux3=aux2/aux1;
                                polo.push(aux3);
                                 op.pop();

                                break;
							}

                            op.pop();

                            continue;

					}
					if(ok)
					op.push(c);

				}




	}





	}

		while(!op.empty())
		{
                            aux1=polo.top();
							polo.pop();
							aux2=polo.top();
							polo.pop();

							switch(op.top())
							{
                            case '+':
                                aux3=aux1+aux2;
                                polo.push(aux3);
                                break;

                           case '-':
                                aux3=aux2-aux1;

                                polo.push(aux3);
                                break;

                            case '*':
                                aux3=aux1*aux2;
                                polo.push(aux3);
                                break;

                            case '/':
                                if(aux1 == 0 or aux2==0)
                                    aux3=0;
                                else
                                    aux3=aux2/aux1;
                                polo.push(aux3);
                                 op.pop();

                                break;
							}



                    op.pop();
		}

		printf("%d\n",polo.top());










     return 0;
}