C++制作的计算器(栈)

注释:使用的是栈来实现的

main.cpp

#include <iostream>
#include <string>
#include "compute.h"

int main(int argc, char const *argv[])
{
    char data[1024];
    std::string expression;
    while (1)
    {
        std::cout << "pleace enter:";
        fgets(data, 1024, stdin);
       
        float result =compute_get_expression(data);
        std::cout << "result = "<< result <<std::endl;
        
    }
    return 0;
}

compute.h

#if !defined(__COMPUTE_)
#define __COMPUTE_

typedef void compute;

float compute_get_expression(compute *); /*获得表达式*/

#endif // __COMPUTE_

compute.cpp

#include "compute.h"
#include <string>
#include <iostream>
#include <stack>
#include <ctype.h>
#include <vector>

enum t_or_f
{
    MAX,  //_op1 > _op2
    MIN,  //_op1 < _op2
    EQUAL //_op1 = _op2
};

static std::stack<float> num_sck;       /*存放数字的栈*/
static std::stack<char> op_sck;         /*存放运算符的栈*/
static std::vector<std::string> vector; /*将读到的vaule放在这里*/

/*判断是否为数字或者'.'*/
static bool is_char_num(char p)
{
    if (isdigit(p) || p == '.')
        return true;
    return false;
}

/*判断是否为运算符*/
static bool is_char_op(char p)
{
    if (p == '+' || p == '-' || p == '*' || p == '/' || p == '(' || p == ')' || p == '%')
        return true;
    return false;
}

// 测试时用
template <typename T>
void test_stack(std::vector<T> &vector)
{
    for (auto &&i : vector)
    {
        std::cout << i << " ";
    }
}

/*将string中的数字和运算符解析出来放入vector中*/
static void expression_parse(const std::string &expression)
{
    std::string num;
    std::string _op_;

    for (auto &&i : expression)
    {
        if (i == ' ') // 将所有的空格去掉
            continue;
        if (is_char_num(i))
        {
            num += i; // 判断是否为一个完整的数
            continue;
        }
        if (is_char_op(i))
        {
            if (!num.empty())
            {
                vector.push_back(num);
                num.clear();
            }
            _op_ += i;

            vector.push_back(_op_);
            _op_.clear();
        }
    }
    if (!num.empty())
    {
        vector.push_back(num);
        num.clear();
    }
    test_stack(vector);
}

// 比较运算符优先级
static int priority(char _op1, char _op2)
{
    if (_op1 == '+' || _op1 == '-')
    {
        if (_op2 == '+' || _op2 == '-')
        {
            return EQUAL;
        }
        return MIN;
    }
    else
    {
        if (_op2 == '+' || _op2 == '-')
        {
            return MAX;
        }
        return EQUAL;
    }
}

// 计算数字
static float compute_num(float &vaule1, float &vaule2, char &op)
{
    if (op == '+')
        return vaule1 + vaule2;
    if (op == '-')
        return vaule1 - vaule2;
    if (op == '*')
        return vaule1 * vaule2;
    if (op == '/')
        return vaule1 / vaule2;
    return 0.0;
}

// 判断栈内的i是否为数字
static bool is_string_num(std::string string)
{
    if (isdigit(string.at(0)))
        return true;
    return false;
}

static float compute_handler()
{
    if (vector.empty())
        return -1;

    for (auto &&i : vector)
    {
        if (is_string_num(i))
        {
            num_sck.push(std::stof(i));
            continue;
        }
        else if (i.at(0) == '(')
        {
            op_sck.push(i.at(0));
            continue;
        }
        else if (i.at(0) == ')')
        {
            while (!op_sck.empty() && op_sck.top() != '(')
            {
                float result;
                char op_vaule = op_sck.top();
                op_sck.pop();
                float vaule1 = num_sck.top();
                num_sck.pop();
                float vaule2 = num_sck.top();
                num_sck.pop();
                result = compute_num(vaule2, vaule1, op_vaule);
                num_sck.push(result);
            }
            op_sck.pop();
        }
        else
        {

            if (!op_sck.empty() && op_sck.top() != '(')
            {
                if (priority(op_sck.top(), i.at(0)) == MAX || priority(op_sck.top(), i.at(0)) == EQUAL)
                {
                    float result;
                    char op_vaule = op_sck.top();
                    op_sck.pop();
                    float vaule1 = num_sck.top();
                    num_sck.pop();
                    float vaule2 = num_sck.top();
                    num_sck.pop();
                    result = compute_num(vaule1, vaule2, op_vaule);
                    num_sck.push(result);
                    op_sck.push(i.at(0));
                    continue;
                }
            }

            op_sck.push(i.at(0));
        }
    }
    // 当执行完上面时,表达式过长栈内会还有数值
    while (!op_sck.empty() && !num_sck.empty())
    {

        float result;
        char op_vaule = op_sck.top();
        op_sck.pop();
        float vaule1 = num_sck.top();
        num_sck.pop();
        float vaule2 = num_sck.top();
        num_sck.pop();

        result = compute_num(vaule2, vaule1, op_vaule);

        num_sck.push(result);
    }
    // while结束,num_sock.top()为最终答案
    float result = num_sck.top();
    num_sck.pop();
    vector.clear();
    return result;
}

/// @brief 
/// @param ex 
/// @return 
float compute_get_expression(compute *ex)
{
    std::string expression = reinterpret_cast<const char *>(ex);
    std::cout << "expression = " << expression << std::endl;
    expression_parse(expression);
    float result = compute_handler();
    if (result == -1)
    {
        fprintf(stdout, "creat stack if fail");
    }

    return result;
}

运行后的结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值