Question A: simple calculator

Keywords: calculator

Title Description

Read in a non negative integer evaluation expression containing only +, -, *, /, and evaluate the value of the expression.

input

The test input contains several test cases, each of which takes up one line, with no more than 200 characters in each line. Integers and operators are separated by a space. There is no illegal expression. When there is only 0 in a row, the input ends and the corresponding result does not output.

output

Output 1 line for each test case, that is, the value of the expression, accurate to 2 decimal places.

sample input

30 / 90 - 26 + 97 - 5 - 6 - 13 / 88 * 6 + 51 / 29 + 79 * 87 + 57 * 92
0

sample output

12178.21
//Question A: simple calculator 
/*Title Description

Read in a non negative integer evaluation expression containing only +, -, *, /, and evaluate the value of the expression.

sample input

30 / 90 - 26 + 97 - 5 - 6 - 13 / 88 * 6 + 51 / 29 + 79 * 87 + 57 * 92
0
*/

#include<iostream>
#include<string>
#include<stack>
#include<queue>
#include<map>
using namespace std;

struct node//Take numbers and operators as a node and distinguish them with flag s
{
	double num;
	char op;
	bool flag;//1 is the operand 0 is the operator
};

string str;//Infix expression
stack<node> s;//Operator stack
queue<node> q;//Postfix Expression 
map<char,int> opm;//Operator precedence 

//step 1: infix expression to suffix expression (it is not necessary to use parentheses to indicate which part of the expression is operated first)
void change()
{
	node temp;
	for(int i=0;i<str.length();)//Scanning infix
	{
		if(str[i]>='0'&&str[i]<='9')
		{
			temp.flag=1;//Mark as number
			temp.num=str[i++]-'0';
			while(i<str.length() && str[i]>='0' && str[i]<='9')//Follow up is still a number and does not exceed the length
			{
				temp.num=temp.num*10+(str[i]-'0');
				i++;
			}
			q.push(temp);//Operand enters suffix sequence
		}
		else
		{
			temp.flag=0;//Mark as operator
			while(!s.empty() && opm[str[i]]<=opm[s.top().op])//Stack is not empty and current operator ≤ stack top operator priority
			{
				q.push(s.top());//Stack and add suffix sequence
				s.pop();
			}
			temp.op=str[i];
			s.push(temp);
			i++;
		}
	}
	while(!s.empty())//The remaining operators in the stack enter the suffix sequence
	{
		q.push(s.top());
		s.pop();
	}
}

//step 2: evaluate suffix expression
double result()
{
	double a,b;
	node temp,result;
	while(!q.empty())
	{
		temp=q.front();
		q.pop();
		if(temp.flag ==1)
		{
			s.push(temp);
		}
		else
		{
			b=s.top().num;
			s.pop();
			a=s.top().num;
			s.pop();
			result.flag =1;
			if(temp.op=='+') result.num =a+b;
			else if(temp.op=='-') result.num =a-b;
			else if(temp.op=='*') result.num =a*b;
			else if(temp.op=='/') result.num =a/b;
			s.push(result);
		}
	}
	return s.top().num ;
}

int main()
{
	opm['+']=opm['-']=1;
	opm['*']=opm['/']=2;

	while(getline(cin,str),str!="0")//End when case is 0
	{
		for(string::iterator it=str.begin();it!=str.end();it++)//Remove spaces between integers and operators
		{
			if(*it==' ')
			{
				str.erase(it);
			}
		}
		while(!s.empty()) {s.pop();}//Initialization stack
		change();
		printf("%.2f\n",result());//The value of the expression, accurate to 2 decimal places
	}
	return 0;
}
	

 

Posted by rhodrykorb on Sat, 30 Nov 2019 16:27:57 -0800