简易计算器

一 问题

题目描述

读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
输入

测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
输出

对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
样例输入

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

样例输出

12178.21

二 问题分析

 这是很常见的问题,对应于数据结构的栈与队列。  首先应当将中序表达式,转化为后缀表达式。然后利用站计算。

三 具体代码

#include<iostream>
#include<string>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
using namespace std;
struct node
{
	double num;
	char op;
	bool flag;
};
string str;
stack<node> s;
queue<node> q;
map<char,int> op;
vector<double> result;
void change(); 
void cal();
void display_queue();
void display_stack();
int main()
{
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
	op['+']=op['-']=1;
	op['*']=op['/']=2;
	while(1)
	{	
		getline(cin,str);
		if(str=="0")
		{
			break; 
		} 
		for(string::iterator it=str.end(); it!=str.begin();it--)
		{
			if((*it)==' ')
			{
				str.erase(it);
			}
		}
		cout<<str<<endl;
		change();
		cal();
		while(!s.empty()) s.pop();
		while(!q.empty()) q.pop();
	}
	for(vector <double>::iterator it=result.begin(); it!=result.end(); it++)
	{
		cout<<*it<<endl;	
	}
	return 0;
}
void change()
{
	double num;
	node temp;
	string tp="";
	string::iterator it=str.begin();
	while(it!=str.end())
	{
		double num;
		if(*it>='0'&&*it<='9')
		{
			while(it!=str.end()&&*it>='0'&&*it<='9')
			{
				tp=tp+*it;
				it++;
			}
			sscanf(tp.data(),"%lf",&num);
			cout<<"num="<<num<<endl;
			temp.flag=true;
			temp.num=num;
			q.push(temp);
			tp="";	
		} 
		else
		{
		
			
			if(s.empty()||op[*it]>op[s.top().op])
			{
				temp.flag=false;
				temp.op=*it;
				s.push(temp);
			}else
			{
				while(!s.empty()&&op[*it]<=op[s.top().op])
				{
					q.push(s.top());
					s.pop(); 
				}
				temp.flag=false;
				temp.op=*it;
				s.push(temp);
			}
	
			it++;
		}	 
	 }
	while(!s.empty())
	{
		q.push(s.top());
		s.pop(); 
	}
	 display_queue();
	 display_stack();
} 
void display_queue()
{
	queue<node> temp=q;
	cout<<"+++++++++++++q"<<endl;
	while(!temp.empty())
	{
		cout<<temp.front().flag<<endl;
		if(temp.front().flag)
		{
				cout<<temp.front().num<<endl;
		}else
		{
				cout<<temp.front().op<<endl;
		}
		cout<<"--------------"<<endl;
		temp.pop();
	}
}
void display_stack()
{
	stack<node> temp=s;
	cout<<"+++++++++++++s"<<endl;
	while(!temp.empty())
	{
		cout<<temp.top().flag<<endl;
		if(temp.top().flag)
		{
				cout<<temp.top().num<<endl;
		}else
		{
				cout<<temp.top().op<<endl;
		}
		cout<<"--------------"<<endl;
		temp.pop();
	}
}
void cal()
{
	while(!q.empty())
	{
		if(q.front().flag)
	    {
			if(!q.empty())
			{
				s.push(q.front());
				q.pop();
			}
							    	
		}
		else
		{
			node temp;
			char ch=q.front().op;
			if(!q.empty())
			{
				q.pop();
			}
			double temp1,temp2;
			if(!s.empty())
			{
				temp1=s.top().num;
				s.pop();
			}
			if(!s.empty())
			{
				temp2=s.top().num;
				s.pop();;
			}
			temp.flag=true;
			if(ch=='+')
			{
				temp.num=temp2+temp1;
			}
			if(ch=='-')
			{
				temp.num=temp2-temp1;
			}
			if(ch=='*')
			{
				temp.num=temp2*temp1;
			}
			if(ch=='/')
			{
				temp.num=temp2/temp1;
			}
			s.push(temp);
		}
		display_queue();
		display_stack();
	}
	result.push_back(s.top().num);
}

四 运行结果

测试用例

 测试用例

结果

运行结果

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值