实现计算器(C++版 )

题目:
        实现一个计算器,当用户输入1 + 1回车后输出结果为2;当用户输入(5 + 1) % (4 / (3 - 2))输出结果为2。假设输入数字为正整数(输入的数字应该不超过10,记不太清了),且输入无空格。

说明:
        计算器运算是从左到右运算的,如8 / 3 * 3结果是6(这里应和题目),而不是0(8 / 9)。

思路:
        建立符号栈和数字栈
        运算符优先级:) > * 、 / 、% > +、 - > ( 且 ( 是特例,直接入栈
        1. 当读取的运算符优先级高于符号栈顶元素优先级时,入栈。
            1.1 当遇到栈顶为 +、- 读取的字符为 *、/、% 时
            1.2 当遇到栈顶为 ( 读取的字符为 +、-、 *、/、% 时
            1.3 读取的字符为 ( 时
        2. 当读取的运算符优先级低于符号栈顶元素优先级时,弹出符号栈的栈顶元素和数字栈的两个栈顶元素并做运算。
            2.1当遇到栈顶为 *、/、% 读取的字符为 +、- 时
        3. 当读取的运算符优先级等于符号栈顶元素优先级时,弹出符号栈的栈顶元素和数字栈的两个栈顶元素并做运算。
            3.1 当遇到栈顶为 +、- 读取的字符为 +、- 时
            3.2 当遇到栈顶为 *、/、% 读取的字符为*、/、%时
        4. 当读取的是数字,就直接入数字栈。

函数代码结构:

void calculator(string str) {
	str迭代器it初始化;
	符号栈初始化;
	数字栈初始化;
	while (it != str.end()) {
		if (*it == '+' || *it == '-' || *it == '*' || *it == '/' || *it == '%' || *it == '(') {
			//当遇到栈顶为 *、/、% 读取的字符为 +、- 时,即读取的运算符优先级低于符号栈顶元素优先级 
			//或
			//当遇到栈顶为 *、/、% 读取的字符为*、/、%时,即读取的运算符优先级等于符号栈顶元素优先级时
			if (!s1.empty() && (s1.top() == '*' || s1.top() == '/' || s1.top() == '%') && *it != '(') {
				乘除取模运算;
				符号栈出栈;
				读到的字符入符号栈;
			}
			//当遇到栈顶为 +、- 读取的字符为 +、- 时,即读取的运算符优先级等于符号栈顶元素优先级
			else if (!s1.empty() && (s1.top() == '+' || s1.top() == '-') && (*it == '+' || *it == '-')) {
				加减运算;
				符号栈出栈;
				读到的字符入符号栈;
			}
			//当遇到栈顶为 +、- 读取的字符为 *、/、% 时,即读取的运算符优先级高于符号栈顶元素优先级
			//或
			//当遇到栈顶为 ( 读取的字符为 +、-、 *、/、% 时,即读取的运算符优先级高于符号栈顶元素优先级
			//或
			//读取的字符为 ( 时
			else {
				读到的字符直接入符号栈;
			}
		}
		else if (*it == ')') {//匹配到右括号
			while (直到读到符号栈的栈顶为'('为止) {
				加减乘除取模运算;
				符号栈栈顶元素出栈;
			}
			'('出栈;
		}
		else {//匹配到数字
			s2.push((int)(*it - '0'));//转换为整型
		}
		it++;
	}
	处理数字栈中最后一组数据;
	输出结果;
}

函数代码:

void calculator(string str) {
	string::iterator it = str.begin();//迭代器
	stack<char> s1;//s1是用来存放运算符的栈
	stack<int> s2;//s3是用来存放数字的栈
	while (it != str.end()) {
		//运算符优先级:) > *、/、% > +、- > ( 且 ( 特例,直接入栈
		if (*it == '+' || *it == '-' || *it == '*' || *it == '/' || *it == '%' || *it == '(') {
			//当遇到栈顶为 *、/、% 读取的字符为 +、- 时,即读取的运算符优先级低于符号栈顶元素优先级 
			//或
			//当遇到栈顶为 *、/、% 读取的字符为*、/、%时,即读取的运算符优先级等于符号栈顶元素优先级时
			if (!s1.empty() && (s1.top() == '*' || s1.top() == '/' || s1.top() == '%') && *it != '(') {//注意这里要加上栈判空,否则会导致开始的时候出错
				int num2 = s2.top();
				s2.pop();
				int num1 = s2.top();
				s2.pop();
				if (s1.top() == '*') {
					s2.push(num1 * num2);
				}
				else if (s1.top() == '/') {
					s2.push(num1 / num2);
				}
				else {
					s2.push(num1 % num2);
				}
				s1.pop();
				s1.push(*it);
			}
			//当遇到栈顶为 +、- 读取的字符为 +、- 时,即读取的运算符优先级等于符号栈顶元素优先级
			else if (!s1.empty() && (s1.top() == '+' || s1.top() == '-') && (*it == '+' || *it == '-')) {//注意这里要在前面加栈的判空,否则会导致开始的时候出错
				int num2 = s2.top();
				s2.pop();
				int num1 = s2.top();
				s2.pop();
				if (s1.top() == '+') {
					s2.push(num1 + num2);
				}
				else {
					s2.push(num1 - num2);
				}
				s1.pop();
				s1.push(*it);
			}
			//当遇到栈顶为 +、- 读取的字符为 *、/、% 时,即读取的运算符优先级高于符号栈顶元素优先级
			//或
			//当遇到栈顶为 ( 读取的字符为 +、-、 *、/、% 时,即读取的运算符优先级高于符号栈顶元素优先级
			//或
			//读取的字符为 ( 时
			else {
				s1.push(*it);
			}
		}
		else if (*it == ')') {//匹配到右括号
			while (s1.top() != '(') {
				int num2 = s2.top();
				s2.pop();
				int num1 = s2.top();
				s2.pop();
				if (s1.top() == '+') {
					s2.push(num1 + num2);
				}
				else if (s1.top() == '-') {
					s2.push(num1 - num2);
				}
				else if (s1.top() == '*') {
					s2.push(num1 * num2);
				}
				else if (s1.top() == '/') {
					s2.push(num1 / num2);
				}
				else {
					s2.push(num1 % num2);
				}
				s1.pop();
			}
			s1.pop();
		}
		else {//匹配到数字
			s2.push((int)(*it-'0'));//转换为整型
		}
		it++;
	}

	//处理最后两个数字
	int num2 = s2.top();
	s2.pop();
	int num1 = s2.top();
	s2.pop();
	if (s1.top() == '+') {
		s2.push(num1 + num2);
	}
	else if (s1.top() == '-') {
		s2.push(num1 - num2);
	}
	else if (s1.top() == '*') {
		s2.push(num1 * num2);
	}
	else if (s1.top() == '/') {
		s2.push(num1 / num2);
	}
	else {
		s2.push(num1 % num2);
	}
	cout << s2.top() << endl;
}

完整代码:

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

void calculator(string str) {
	string::iterator it = str.begin();//迭代器
	stack<char> s1;//s1是用来存放运算符的栈
	stack<int> s2;//s3是用来存放数字的栈
	while (it != str.end()) {
		//运算符优先级:) > *、/、% > +、- > ( 且 ( 特例,直接入栈
		if (*it == '+' || *it == '-' || *it == '*' || *it == '/' || *it == '%' || *it == '(') {
			//当遇到栈顶为 *、/、% 读取的字符为 +、- 时,即读取的运算符优先级低于符号栈顶元素优先级 
			//或
			//当遇到栈顶为 *、/、% 读取的字符为*、/、%时,即读取的运算符优先级等于符号栈顶元素优先级时
			if (!s1.empty() && (s1.top() == '*' || s1.top() == '/' || s1.top() == '%') && *it != '(') {//注意这里要加上栈判空,否则会导致开始的时候出错
				int num2 = s2.top();
				s2.pop();
				int num1 = s2.top();
				s2.pop();
				if (s1.top() == '*') {
					s2.push(num1 * num2);
				}
				else if (s1.top() == '/') {
					s2.push(num1 / num2);
				}
				else {
					s2.push(num1 % num2);
				}
				s1.pop();
				s1.push(*it);
			}
			//当遇到栈顶为 +、- 读取的字符为 +、- 时,即读取的运算符优先级等于符号栈顶元素优先级
			else if (!s1.empty() && (s1.top() == '+' || s1.top() == '-') && (*it == '+' || *it == '-')) {//注意这里要在前面加栈的判空,否则会导致开始的时候出错
				int num2 = s2.top();
				s2.pop();
				int num1 = s2.top();
				s2.pop();
				if (s1.top() == '+') {
					s2.push(num1 + num2);
				}
				else {
					s2.push(num1 - num2);
				}
				s1.pop();
				s1.push(*it);
			}
			//当遇到栈顶为 +、- 读取的字符为 *、/、% 时,即读取的运算符优先级高于符号栈顶元素优先级
			//或
			//当遇到栈顶为 ( 读取的字符为 +、-、 *、/、% 时,即读取的运算符优先级高于符号栈顶元素优先级
			//或
			//读取的字符为 ( 时
			else {
				s1.push(*it);
			}
		}
		else if (*it == ')') {//匹配到右括号
			while (s1.top() != '(') {
				int num2 = s2.top();
				s2.pop();
				int num1 = s2.top();
				s2.pop();
				if (s1.top() == '+') {
					s2.push(num1 + num2);
				}
				else if (s1.top() == '-') {
					s2.push(num1 - num2);
				}
				else if (s1.top() == '*') {
					s2.push(num1 * num2);
				}
				else if (s1.top() == '/') {
					s2.push(num1 / num2);
				}
				else {
					s2.push(num1 % num2);
				}
				s1.pop();
			}
			s1.pop();
		}
		else {//匹配到数字
			s2.push((int)(*it-'0'));//转换为整型
		}
		it++;
	}

	//还差一步
	int num2 = s2.top();
	s2.pop();
	int num1 = s2.top();
	s2.pop();
	if (s1.top() == '+') {
		s2.push(num1 + num2);
	}
	else if (s1.top() == '-') {
		s2.push(num1 - num2);
	}
	else if (s1.top() == '*') {
		s2.push(num1 * num2);
	}
	else if (s1.top() == '/') {
		s2.push(num1 / num2);
	}
	else {
		s2.push(num1 % num2);
	}
	cout << s2.top() << endl;
}

int main() {

	string str = "(5+1)%(4/(3-2+1/2)+2)+1";
	cout << (5 + 1) % (4 / (3 - 2 + 1 / 2) + 2) + 1 << endl;
	calculator(str);
	
	return 0;
}

返回链接

  • 18
    点赞
  • 129
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值