[解题报告]计算器

这题做的我真跪了。。代码我都不想再看一眼了。。。

Description

无聊又蛋疼的WZ想自己写一个计算器,这个计算器可以计算出一个数学表达式的值。例如输出1*2-(4-3)就可以计算出结果。.为了降低难度,输入的数据中,数字全部为整型,且全部是0到9的数字。输入的符号只包含加减乘除和括号,表达式一定是正确的不会出现除以0的情况。

Input

输入包含多行,每行是一个数学表达式

Output

每行输出每个表达式的值

Sample Input

1*2-(4-3)
2/1+(1+1*2)

Sample Output

1
5
#include<iostream>
#include<stack>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
  
stack<int> num;
stack<char> algo;
stack<int> num1;
stack<char> algo1;
  
char s[1000];
int qq;
  
  
  
int yunsuan(int a,char n,int b)
{
    if(n == 'a') return a+b;
    if(n == 'b') return a-b;
    if(n == 'h') return a*b;
    if(n == 'i') return a/b;
}
  
void zhancaozuo()
{
    int b=num.top();
    num.pop();
    int a=num.top();
    num.pop();
    char n=algo.top();
    algo.pop();
    int d=yunsuan(a,n,b);
    num.push(d);
    //cout<<'l'<<num.top()<<' ';
}
  
void tongdeng()
{
    int a1=num1.top();
    num1.pop();
    int b1=num1.top();
    num1.pop();
    char n1=algo1.top();
    algo1.pop();
    int d1=yunsuan(a1,n1,b1);
    num1.push(d1);
    //cout<<'o'<<num1.top()<<' ';
}
  
int main()
{
    int sign=0,sign2=0;
    char r;
    while(cin>>s)
    {
    for(int i=0;i<strlen(s);i++)
    {
        if(sign==3)
        {
            zhancaozuo();
            sign=0;
        }
        if(s[i] >= '0' && s[i] <= '9')
        {
            if(s[i+1] < '0' || s[i+1] > '9')
            {
                int x=atoi(s);
                num.push(x);
                int u=num.top();
                //cout<<u<<' ';
                if(sign==1 || sign==3)
                {
                    zhancaozuo();
                    sign=0;
                }
            }
        }
        else if(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/')
        {
            if(i==0 && s[i]=='-')
            {
                num.push(0);
                algo.push('b');
                s[0]='0';
            }
            if(i>0)
            {
            char h;
            if(s[i]=='+') h='a';
            if(s[i]=='-') h='b';
            if(s[i]=='*') h='h';
            if(s[i]=='/') h='i';
            if(algo.size()!=0 && h>algo.top()+1 || algo.size()==0 && h>='h'&&h<='i') sign=1;
            algo.push(h);
            for(int j=0;j<=i;j++)
                s[j]='0';
            }
        }
        else if(s[i]=='(')
        {
            algo.push(96);
            s[i]='0';
            if(sign==1) sign++;
        }
        else if(s[i]==')')
        {
            num1.push(num.top());
            num.pop();
            while(algo.top()!=96)
            {
                algo1.push(algo.top());
                algo.pop();
                num1.push(num.top());
                num.pop();
            }
            while(num1.size()!=1)
            {
                tongdeng();
            }
            num.push(num1.top());
            num1.pop();
            algo.pop();
            if(sign==2)
            sign++;
        }
    }
    if(sign==1 || sign==3)
    zhancaozuo();
    while(num.size()!=0)
    {
        num1.push(num.top());
        num.pop();
    }
    while(algo.size()!=0)
    {
        algo1.push(algo.top());
        algo.pop();
    }
    while(num1.size()!=1)
    {
        tongdeng();
    }
    cout<<num1.top()<<endl;
    while(num.size()!=0) num.pop();
    while(num1.size()!=0) num1.pop();
    while(algo.size()!=0) algo.pop();
    while(algo1.size()!=0) algo1.pop();
    sign=0;
    }  
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值