C++堆栈计算器

软件环境:VS 2017

内容:

  1. 输入一个数学表达式(假定表达式输入格式合法),计算表达式结果并输出
  2. 数学表达式由单个字符和运算符“+”、“-”、“*”、“/”、“(”、“)”构成,例如 2+3*(4+5)-6/4.
  3. 变量输出采用整数,只舍不入。
  4. 附录代码如下:
    #include"iostream"
    #include "stack"
    #include "string"
    #include <cctype>
    using namespace std;
    
    bool IsOne(char c) {
    	return (c == '+' || c == '-');
    }
    bool IsTwo(char c)
    {
    	return (c == '*' || c == '/');
    }
    string infix_postfix(string infix) {
    	//中缀改为后缀
    	stack<char>stack;
    	string result;
    	for ( unsigned int i = 0; i < infix.size(); i++) 
    	{
    		if (isdigit(infix[i]) || infix[i] == '.')
                       //function isdigit(),参数是数字,返回为真
    		{
    			while (isdigit(infix[i]) || infix[i] == '.')
    			{
    				result += infix[i++];
    			}
    			i--;
    			result += ' ';//different nums are departed
    		}
    		else if (IsOne(infix[i])) {
    			//读到一级运算符
    			while (stack.size() && (IsOne(stack.top()) || IsTwo(stack.top()))) {
    				//A&&(B|C)形式
    				result += stack.top();
    				stack.pop();
    			}
    			stack.push(infix[i]);
    		}
    		else if (infix[i] == ')') {
    			//括号优先
    			while (stack.top() != '(') {
    				result += stack.top();
    				stack.pop();
    			}
    			stack.pop();//最后记得弹出“(”
    		}
    		else if (IsTwo(infix[i])) {
    			//读到二级运算符
    			while (stack.size() && IsTwo(stack.top()))
    			{
    				result += stack.top();
    				stack.pop();
    			}
    			stack.push(infix[i]);//压入二级运算符
    		}
    		else
    			stack.push(infix[i]);
    	}
    	while (stack.size())
    	{
    		result += stack.top();
    		stack.pop();
    	}
    	return result;
    }
    
    double TenTimes(int n) {
    	double res = 1;
    	for (int i = 0; i < n; i++) {
    		res *= 10;
    	}
    	return res;
    }
    double Achieve(string s) {
    	//实现了大于9以及小数的计算
    	double res = 0;
    	char c;
    	int dec = 0;
    	for (unsigned int i = 0; i < s.size(); i++) {
    		c = s[i];
    		if (c == '.')dec = i;//标记小数点的位置
    		else if (!dec)res = res * 10 + c - '0';//只要往后一位,前面的计算结果*10
    		else res += (c - '0') / TenTimes(i - dec);//计算小数值
    	}
    	return res;
    }
    double Calculate(string s) {
    	double res ,t;
    	stack<double>num;
    	string temp;
    	for (unsigned int i = 0; i < s.size(); i++) {
    		temp = "";
    		if (isdigit(s[i]) || s[i] == '.') {
    			while (isdigit(s[i]) || s[i] == '.') {
    				temp += s[i++];// 如果最后一位是数字,这样做会报错
    				num.push(Achieve(temp));
    			}
    		}
    		else {
    			switch (s[i])
    			{
    			case '+':
    				t = num.top();
    				num.pop(); 
    				t += num.top();
    				num.pop();
    				num.push(t);
    				break;
    			case '-':
    				t = num.top();
    				num.pop();
    				t=num.top() - t;
    				num.pop();
    				num.push(t);
    				break;
    			case '*':
    				t = num.top();
    				num.pop();
    				t *= num.top();
    				num.pop();
    				num.push(t);
    				break;
    			case '/':
    				t = num.top();
    				num.pop();
    				t=num.top()/t;
    				num.pop();
    				num.push(t);
    				break;
    			default:
    				cerr << "Fatal Error:Result would be wrong!" << endl;
    				system("pause");
    				break;
    			}
    		}
    		}
    	res = num.top();
    	return res;
    	}
    int main() {
    	string mid, result;
    	cin >> mid;
    	result = infix_postfix(mid);
    	cout << "infix change to postfix:" << endl;
    	cout << result << endl;
    	cout << "the result is: " << Calculate(result) << endl;
    	system("pause");
    	return 0;
    }
     栈(stack)是操作受限的线性表,限定对元素的插入和删除运算只能在表的一端进行。本次实验主要运用了栈的几个基本运算:弹出栈顶元素top()、删除栈顶元素pop()、压栈push()、判断栈空empty()…
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值