【栈】简易计算器

题目描述:

    读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
输入:
    测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
输出:
    对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
样例输入:
1 + 2
4 + 2 * 5 - 7 / 11
0
样例输出:
3.00
13.36


解题思路:

能够用语言描述算法过程,是正确编程的第一步。

设立两个堆栈,一个保存运算符,另一个保存数字。

从左至右遍历字符串,若遇到操作符,则比较该操作符与栈顶操作符优先级大小。如果栈为空,或者该操作符优先级比栈顶操作符的优先级大,则将该操作符压入栈;否则,若该操作符不大于栈顶操作符,则弹出栈顶操作符,然后从数字栈中弹出两个栈顶数字完成运算后压入数字栈,重复比较该操作符与栈顶操作符优先级大小,直到该操作符优先级比栈顶操作符的优先级大,则将该操作符压入栈;

若遍历结束时,取出数字栈中数字就是所求。


实现上的技术:

对操作符编号,使用一个优先级矩阵判断优先级大小;

在表达式的首尾添加标记运算符,该运算符优先级最低;

使用gets读取一行,然后调用函数getop,不断取节点

使用double类型保存运算结果和数字栈类型


代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<stack>
using namespace std;
char str[201];
stack<int> op;
stack<double> num;
//优先级矩阵
//运算符编码:
//0 首尾 
//1:+
//2:-
//3:*
//4:/
int mat[5][5]={
	1,0,0,0,0,
	1,0,0,0,0,
	1,0,0,0,0,
	1,1,1,0,0,
	1,1,1,0,0
};
void getOp(bool &reto,int &retn,int &i){
	//reto: true为运算符 false 为数字
	//retn: 运算符编号 或者 返回的数字
	//i为遍历当面字符串的位置下标
	if(i==0&&op.empty()){
		//如果op一开始是空的,i=0
		//人为添加标记运算符
		reto=true;
		retn=0;
		return;
	}
	if(str[i]==0){
		//str结尾
		reto=true;
		retn=0;
		return;
	}
	if(str[i]>='0'&&str[i]<='9'){
		reto=false;
		int c=1;
		retn=0;
		for(;str[i]!=' '&&str[i]!=0;i++){
			retn*=10;
			retn+=(str[i]-'0');
		}
		if(str[i]==' '){
			i++;
		}
		return;
	}else{
		reto=true;
		switch(str[i]){
		case '+':
			retn=1;
			break;
		case '-':
			retn=2;
			break;
		case '*':
			retn=3;
			break;
		case '/':
			retn=4;
			break;
		}
		i+=2;
		return;
	}
}
int main(){
	while(gets(str)){
		if(str[0]=='0'&&str[1]==0)
			break;
		while(!op.empty()){
			op.pop();	
		}
		while(!num.empty()){
			num.pop();
		}
		int idx=0;
		bool retop;
		int retnum;
		while(true){
			getOp(retop,retnum,idx);
			if(retop){
				if(op.empty()||mat[retnum][op.top()]==1){
					op.push(retnum);
				}else{
					while(mat[retnum][op.top()]==0){
						int tmpop=op.top();
						double tmpnum;//double double double
						op.pop();
						double b=num.top();
						num.pop();
						double a=num.top();
						num.pop();
						if(tmpop==1){
							tmpnum=a+b;
						}else if(tmpop==2){
							tmpnum=a-b;
						}else if(tmpop==3){
							tmpnum=a*b;
						}else if(tmpop==4){
							tmpnum=a/b;
						}
						num.push(tmpnum);
					}
					op.push(retnum);//不要忘了压入栈 不要忘了压入栈 不要忘了压入栈
				}

			}else{
				//数字
				num.push((double)retnum);
			}
			if(op.size()==2&&op.top()==0){
				break;
			}
		}
		printf("%.2f\n",num.top());
	}

}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是C++实现简易计算器的代码: ```c++ #include <iostream> #include <stack> #include <string> using namespace std; int main() { stack<int> numStack; // 存储数字的 stack<char> opStack; // 存储运算符的 string expression; // 表达式字符串 cin >> expression; int len = expression.length(); for (int i = 0; i < len; i++) { char ch = expression[i]; if (ch >= '0' && ch <= '9') { // 如果是数字,入数字 numStack.push(ch - '0'); } else { // 如果是运算符 if (opStack.empty() || ch == '(') { // 如果运算符为空或者当前运算符是左括号,直接入 opStack.push(ch); } else if (ch == ')') { // 如果当前运算符是右括号,弹出运算符中的元素直到遇到左括号 while (opStack.top() != '(') { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } opStack.pop(); // 弹出左括号 } else { // 如果当前运算符是加减乘除 while (!opStack.empty() && opStack.top() != '(' && ((ch == '*' || ch == '/') || (opStack.top() == '+' || opStack.top() == '-'))) { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } opStack.push(ch); } } } while (!opStack.empty()) { // 处理剩余的运算符 int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } cout << numStack.top() << endl; // 输出最终结果 return 0; } ``` 这个程序可以处理包含加减乘除和括号的表达式,但是没有考虑负数的情况。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值