实现一个简单计算器,表达式为字符串表示

实现一个简单计算器,表达式为字符串表示
  • 要求:
  • 实现一个简单计算器,表达式是字符串,如“#(2*(1+3)+8)/4#”,以‘#’做标志位
    如:
    输入
#(2*(1+3)+8)/4#

输出

 4

实现思路:

  • 用栈实现。将输入的中缀表达式通过栈的方式改为后缀表达式计算。

  • (2*(1+3)+8)/4
    可先将“(”压入栈1,“2”压入栈2,“*”继续压入栈1,“(”压入栈1,“1”压入栈2,“+”压入栈1,“3”栈2。
    这时,遇到“)”
    此时
    栈1:(*(+
    栈2:2 1 3
    将栈1的“+”“(”,栈2的“3”“1”抛出计算“1+3”,将值“4”压入栈2,
    此时:
    栈1:(*
    栈2:2 4
    …….
    以下是实现代码:
//栈的头文件
#ifndef STACK1_H
#define STACK1_H

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

const int maxlen = 100;

template <typename elementType>
class Stack1
{
public:
    Stack1();
    ~Stack1(){};
    void push(elementType x);//入栈
    void get_top(elementType &x);//出栈
    void pop();
    bool empty();//判空
    bool full();//判满
    friend istream &operator>>(istream &cin, Stack1<string> &s2);// 重载函数

private:
    int count;
    elementType data[maxlen];
    string t;
};

#endif

//栈的源文件
#include "Stack1.h"
#include <iostream>

template <typename elementType>
Stack1<elementType>::Stack1()
{
    count = 0;
}

template <typename elementType>
bool Stack1<elementType>::empty()//判空
{
    if (0 == count)
    {
        return true;
    }
    return false;
}

template <typename elementType>
bool Stack1<elementType>::full()//判满
{
    if (maxlen==count)
    {
        return true;
    }
    return false;
}

template <typename elementType>
void Stack1<elementType>::push(elementType x)//入栈
{
    if (!full())
    {
        data[count++] = x;
    }
}

template <typename elementType>
void Stack1<elementType>::get_top(elementType &x)//出栈
{
    if (!empty())
    {
        x = data[count - 1];
    }
}

template <typename elementType>
void Stack1<elementType>::pop()
{
    if (!empty())
    {
        --count;
    }
}

template <typename elementType>
istream & operator>>(istream &cin, Stack1<string> &s2)
{
    cin >> s2.t;
    return cin;
}

//主函数
#include "Stack1.cpp"

using namespace std;

//判断是否是数字
bool isnum(char x)
{
    if (x>='0'&&x<='9')
    {
        return true;
    }
    return false;
}

//判断优先级
int priority(char x)
{
    if (x=='+'||x=='-')
    {
        return 0;
    }
    else if (x == '*' || x == '/')
    {
        return 1;
    }
    else if (x == '(' || x == ')')
    {
        return -1;
    }
    else if (x=='#')
    {
        return -2;
    }

}

//计算
int calca(string s)
{
    Stack1<int> num;
    Stack1<char> ope;
    char top;
    int a, b;

    for (int i = 0; i < s.size();i++)
    {
        if (isnum(s[i]))
        {
            int Temp = 0;
            string temp;

            temp += s[i];
            while (isnum(s[++i]))
            {
                temp += s[i];
            }
            for (int j = 0; j < temp.size(); j++)
            {
                Temp = Temp * 10 + temp[j] - 48;
            }
            num.push(Temp);
            temp.clear();
        }//将字符数转换成整形
        if (!isnum(s[i]))
        {
            if (ope.empty())
            {
                ope.push(s[i]);
            }
            else
            {
                ope.get_top(top);
                if ((s[i] == '('))
                {
                    ope.push(s[i]);
                }
                else if (priority(s[i])>priority(top))
                {
                    ope.push(s[i]);
                }
                else
                {
                    while (priority(s[i]) <= priority(top))
                    {
                        if (top=='#'&&s[i]=='#')
                        {
                            int answer;
                            ope.pop();
                            num.get_top(answer);
                            cout << "\n答案是:" << answer << endl;
                            num.pop();
                            return 0;
                        }
                        if (top=='('&&s[i]==')')
                        {
                            ++i;
                        }
                        if (s[i] == ')')
                        {
                            num.get_top(a);
                            num.pop();
                            num.get_top(b);
                            num.pop();
                        }
                        else if (priority(s[i]) <= priority(top))
                        {
                            num.get_top(a);
                            num.pop();
                            num.get_top(b);
                            num.pop();
                        }
                        if (top=='+')
                        {
                            b += a;
                            num.push(b);
                        }
                        else if (top == '-')
                        {
                            b -= a;
                            num.push(b);
                        }
                        else if (top == '*')
                        {
                            b *= a;
                            num.push(b);
                        }
                        else if (top == '/')
                        {
                            b /= a;
                            num.push(b);
                        }
                        ope.pop();
                        ope.get_top(top);
                    }
                    ope.push(s[i]);//用于当top=='#'时,将最后一个运算符入栈    
                }
            }
        }
    }
}

int main()
{
    string s1;
    cout << "输入一个以'#'开头结尾的表达式:" << endl;
    cin >> s1;
    calca(s1);

    cin.get(), cin.get();
}

运行结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值