hihocoder1332 简单计算器

描述

编写一个程序可以完成基本的带括号的四则运算。其中除法(/)是整除,并且在负数除法时向0取整。(C/C++/Java默认的除法就是向0取整,python默认的是向负无穷取整。)

例如计算 100 * ( 2 + 12 ) - (20 / 3) * 2, 结果是1388。

输入

一个长度不超过100的字符串,代表要计算的算式。包含数字0-9以及+-*/()。

输入保证计算过程不会超过32位有符号整数,并且其中的'-'都是减号没有负号。

输出

计算的结果。

样例输入
100*(2+12)-(20/3)*2
样例输出
1388

解题思路:运用栈的知识,和分治的思想来模拟,先把中缀表达式转化为后缀表达式(即逆波兰式)然后用栈进行计算。

具体看代码吧:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
using namespace std;

int getrink(char opt)
{
    if(opt == '+' || opt == '-') return 10;
    if(opt == '*' || opt == '/') return 20;
    if(opt == '(') return 0;
    else return -1;
}

int postfix(string &str_post,string &str_mid)
{
    stack<char> opt;
    while(!opt.empty()) opt.pop();
    bool flag=false;
    for(int i=0; str_mid[i]!='\0'; i++)
    {
        if(str_mid[i]>='0'&&str_mid[i]<='9')
        {
            if(flag)
            {
                str_post+='&';
            }
            else
            {
                flag=true;
            }
            while(str_mid[i]>='0'&&str_mid[i]<='9')
            {
                str_post+=str_mid[i];
                i++;
            }
            i--;
        }
        else if(str_mid[i]=='(')
        {
            opt.push(str_mid[i]);
        }
        else if(str_mid[i]==')')
        {
            if(!opt.empty()&&opt.top()!='(')
            {
                str_post+=opt.top();
                opt.pop();
                flag=false;
            }
            if(opt.top()=='(')
            {
                opt.pop();
            }
        }
        else if(str_mid[i]=='+'||str_mid[i]=='-'||str_mid[i]=='*'||str_mid[i]=='/')
        {
            int a1=getrink(str_mid[i]);
            int a2;
            while(!opt.empty())
            {
                a2=getrink(opt.top());
                if(a1<=a2)
                {
                    str_post+=opt.top();
                    opt.pop();
                    flag=false;
                }
                else
				{
					break;
				}
            }
            opt.push(str_mid[i]);
        }
    }
    while(!opt.empty())
	{
		str_post+=opt.top();
		opt.pop();
	}
}

int getresult(int a,int b,char opp)
{
	if(opp=='+') return a+b;
	if(opp=='-') return a-b;
	if(opp=='*') return a*b;
	if(opp=='/') return a/b;
}

int compute(string str)
{
	int op1,op2,temp;
	stack<int> data;
	while(!data.empty()) data.pop();
	for(int i=0;str[i]!='\0';i++)
	{
		if(str[i]=='&') continue;
		else if(str[i]>='0'&&str[i]<='9')
		{
			temp=0;
			while(str[i]>='0'&&str[i]<='9')
			{
				temp=temp*10+str[i]-'0';
				i++;
			}
			i--;
			data.push(temp);
		}
		else if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/')
		{
			op1=data.top();
			data.pop();
			op2=data.top();
			data.pop();
			temp=getresult(op2,op1,str[i]);
			data.push(temp);
		}
	}
	return data.top();
}

int main()
{
	string str_mid,str_post;
	while(cin>>str_mid)
	{
		postfix(str_post,str_mid);
	    cout << str_post << endl;
		cout<<compute(str_post)<<endl;
	}
	return 0;
}

人一我十,人百我万。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值