前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >双栈实现计算器

双栈实现计算器

作者头像
AngelNH
发布2020-04-16 11:16:07
9170
发布2020-04-16 11:16:07
举报
文章被收录于专栏:AngelNIAngelNI

愿我们能与最好的自己相遇。

双栈实现计算器

代码语言:javascript
复制
#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
stack<int> OPND;
stack<char> OPRT;
int precede(char x);
void operate();
int calculate(string s);
int main()
{
    string str;
    printf("\t\t--------------------------------------------\n");
    printf("\t\t--------------双栈实现简易计算器------------\n");
    printf("\t\t------------------欢迎您使用----------------\n");
    printf("\t\t|                  计算器                  |\n");
    printf("\t\t|              ----------------            |\n");
    printf("\t\t|              |              |            |\n");
    printf("\t\t|              ----------------            |\n");
    printf("\t\t|              1    2    3   +             |\n");
    printf("\t\t|              4    5    6   -             |\n");
    printf("\t\t|              7    8    9   *             |\n");
    printf("\t\t|              0   (    )  /             |\n");
    printf("\t\t--------------------------------------------\n");
    printf("\t\t 请输入一个用#开头和结尾的表达式:\n");
    cin>>str;
    calculate(str);
    getchar();
    getchar();
    return 0;
}
//运算符优先级判断 
int precede(char x)
{
	if (x == '+' || x == '-')  
        return 0;  
    else if (x == '*' || x == '/')  
        return 1;  
    else if (x == '(' || x == ')')  
        return -1;  
    else if (x == '#')  
        return -2;  
}
//二元运算 
inline void operate(char top)
 {
 	int a = OPND.top(); 
    OPND.pop();
    int b = OPND.top(); 
	OPND.pop();
	int c;
	
 	if (top == '+') 
	 {  
        b += a;  
        OPND.push(b);  
    }  
    else if (top == '-')
	{  
        b -= a;  
        OPND.push(b);  
    }  
    else if (top == '*') 
	{  
        b *= a;  
        OPND.push(b);  
    }  
    else if (top == '/') 
	{  
        b /= a;  
        OPND.push(b);  
    }   
 }
//读取表达式 
int calculate(string s)
{
    int a,b;
    char top;
    for(int i = 0;i<s.size();++i)
    {
    	//判断是否为数字 
        if(isdigit(s[i]))
        {
            int num = 0;
            string number;
            number += s[i];
            while(isdigit(s[++i]))
            {
                number += s[i];
            }
            for( int j=0;j<number.size();j++)
            {
                    num = num*10 +number[j] - 48;
            }
            OPND.push(num);
            number.clear();
        }
        //对操作符的操作 
        if(!isdigit(s[i]))
        {
            if(OPRT.empty())
            {
                OPRT.push(s[i]);
            }
            else
            {
                top = OPRT.top();
                if(precede(s[i])>precede(top) || s[i] == '(')
                {
                    OPRT.push(s[i]);
                }
                else
                {	
                    while(precede(s[i])<=precede(top))
                    {
                        if(top=='#'&&s[i]=='#')
                        {
                            OPRT.pop();
                            int ans = OPND.top();
                            cout<<"The answer is :"<<ans<<endl;
                            OPND.pop();
                            return 0;
                        }
                        else if(top=='('&&s[i]==')')
                        {
                            ++i;
                        }
                        else
                        {
							operate(top);
                        } 
						 OPRT.pop(); 
                         top = OPRT.top();              
                    }
                    OPRT.push(s[i]);
                }
            }
        }
    }
}
uR2CLQ.png
uR2CLQ.png
uR2iZj.png
uR2iZj.png
uR29sg.png
uR29sg.png
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-07|,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 双栈实现计算器
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档