当前位置: 首页 > news >正文

c++实现中缀表达式 转换为后缀表达式

使用栈来计算后缀表达式的值:

9+(3 - 1)*3+10/2;

后缀表达式:所有的符号都是在运算数字的后面出现: 9 3 1 – 3 * + 10  2 / +

规则:

中缀表达式转后缀表达式:

1.从左到右遍历中缀表达式的每个数字和符号,若是数字就打印同时入栈数字栈snum;

2.若是左括号,则将其压入运算符栈sope中;

3.若是右括号,则将sope栈顶的运算符弹出并打印,直到遇到左括号(左括号也出栈,但不输出打印);

4.若是运算符,若该运算符的优先级大于栈顶运算符的优先级时,则把它压栈;若小于或等于栈顶运算符优先级时,则将栈顶运算符弹出并打印, 同时出栈snum数字按照此运算符运算后结果入栈,再比较新的栈顶运算符,按同样方法处理,直到该运算符大于栈顶运算符优先级为止,然后将该运算符压栈;

5.若中缀表达式中的各对象处理完毕,则把sope栈中存留的运算符依次弹栈输出打印, 同时取snum中的值按照此运算符运算后的结果入栈,直至sope栈空 。snum栈底元素出栈即为计算结果。

 计算规则:

  • 创建一个双精度数字栈 snum 来存储操作数。Copy
  • 使用 stringstream 将输入字符串 postfix 便于逐个提取 token。
  • 使用 while (ss >> token) 循环来逐个读取 token,直到字符串流结束。每次读取都会跳过空格。
  • 如果 token 的第一个字符是数字(使用 isdigit 函数判断),则将这个 token 转换为 double 类型并推入栈中。
  • 如果 token 不是数字,说明它是一个操作符。
  • 从栈中弹出两个元素(x 和 y),注意:弹出顺序是后进先出(LIFO),所以首先弹出的将是最近进入栈的数字
  • 根据操作符执行相应的数学运算,并将计算结果压入栈中。
  • 针对除法,还需检查除数 y 是否为零,如果是,则抛出异常以避免除以零错误。
  • 最后,函数返回栈顶元素,这个值是后缀表达式的计算结果.

完整代码: 

 

#include <iostream>
#include <string>
#include <exception>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
bool isvalidexpression(const string & expression)
{
    for (char ch : expression)
    {
        if (!isdigit(ch) && ch != '+' && ch != '-' && ch != '*' && ch != '/' && ch != ' ')
        {
            return false;
        }
    }
    return true;
}

int precedence(char ch)
{
    switch (ch)
    {
    case '+':
    case'-':
        return 1;
    case '*':
    case '/':
        return 2;
    default:
        return 0;

    }
}

string infixtopostfix(const string& expression)
{
    stack<char> sope;//字符栈
    string postfix = "";//后缀表达式
    string numbuff = "";
    for (size_t i = 0; i < expression.length(); i++)
    {
        char ch = expression[i];
        if (isdigit(ch))
        {
            numbuff += ch;
        }
        else
        {
            if (!numbuff.empty())
            {
                postfix += numbuff;
                postfix += ' ';
                numbuff.clear();
            }


            if (ch == '(')
            {
                sope.push(ch);
            }
            else if (ch == ')')
            {
                while (!sope.empty() && sope.top() != '(')
                {
                    postfix += sope.top();
                    postfix += ' ';
                    sope.pop();
                }
                sope.pop();//出栈'('
            }
            else
            {
                while (!sope.empty() && precedence(sope.top()) >= precedence(ch))
                {
                    postfix += sope.top();
                    postfix += ' ';
                    sope.pop();
                }
                sope.push(ch);
            }
        }
    }
    if (!numbuff.empty())
    {
        postfix += numbuff;
        postfix += ' ';
    }
    while (!sope.empty())
    {
        postfix += sope.top();
        postfix += ' ';
        sope.pop();
    }

    if (!postfix.empty() && postfix.back() == ' ')
    {
        postfix.pop_back();
    }
    return postfix;

}

double evaluatepostfix(const string& postfix)
{
    stack<double>snum;//数字栈
    stringstream ss(postfix);
    string token;
    while (ss >> token)
    {
        if (isdigit(token[0]))
        {
     snum.push(stod(token));//stod 是一个函数,用于将字符串转换为 double 类型的浮点数。
        }
        else
        {
            double x = snum.top(); snum.pop();
            double y = snum.top(); snum.pop();
            switch (token[0])
            {
            case '+':snum.push(x + y); break;
            case'-':snum.push(x - y); break;
            case '*':snum.push(x * y); break;
            case '/':if (y != 0)
            {
                snum.push(x / y);
            }
                    else
            {
                throw runtime_error("Error:Division By Zero");
            }
                    break;
                
            }
        }
    }

    return snum.top();


}

int main()
{
    //输入一个表达式
    string expression;
    cout << "请输入一个算数表达式:(例如2*3*(4+5))" << endl;
    getline(cin , expression);

    //检测有效性
    if (!isvalidexpression(expression))
    {
        cout << "表达式不合法" << endl;
    }
    //将中缀表达式转换为后缀表达式
    string postfix = infixtopostfix(expression);
    cout << "后缀表达式是:"<<postfix << endl;

    //计算后缀表达式的值
    
    try
    {
        double result = evaluatepostfix(postfix);
        cout << "后缀表达式的结果为:" << result << endl;
     }
    catch (runtime_error& e)
    {
        cout << e.what() << endl;
    }

    system("pause");
    return 0;
}

http://www.lryc.cn/news/484963.html

相关文章:

  • Cisco FMC重置SmartLicense到Evaluatin mode步骤
  • 多表查询综合归纳
  • 【5.线性表-链式表示-王道课后算法题】
  • 存储过程及练习
  • 【在Linux世界中追寻伟大的One Piece】多路转接epoll
  • 设计模式-参考的雷丰阳老师直播课
  • Python +Pyqt5 简单视频爬取学习(一)
  • Python Requests模块全面教程
  • PyQt入门指南六十 与Python其他库的集成方法
  • Android15之解决:Dex checksum does not match for dex:framework.jar问题(二百三十九)
  • 车企自动驾驶功能策略 --- 硬件预埋(卷传感器配置)
  • 【已为网站上传证书,却显示不安全】
  • docker busybox作为initContainers
  • 20.UE5UI预构造,开始菜单
  • Electron教程1-初学入门
  • 从北美火到中国,大数据洞察品牌“STANLEY”的突围之路
  • 深度学习之GAN应用
  • 鸿蒙生态下的安全隐私保护:打造用户信任的应用体验
  • 用pandoc工具实现ipynb,md,word,pdf之间的转化
  • 第三十一天|贪心算法| 56. 合并区间,738.单调递增的数字 , 968.监控二叉树
  • 力扣 最长公共前缀-14
  • IDEA调整警告级别【IntelliJ IDEA 2024.2.0.1】
  • Vulnhub靶场 Billu_b0x 练习
  • Essential Cell Biology--Fifth Edition--Chapter one (6)
  • Jupyter Book 快捷键总结大全
  • Spring Authorization Server OAuth2.1
  • 解决”重复文件名重命名“问题【根据Word系统方式】
  • 【PyTorch】PyTorch Geometric(PyG)安装指南:如何高效配置图神经网络环境
  • SolidWorks21装配体中一个零件无法改为线架图
  • 11.11机器学习_介绍和定义