1.题目
中缀表达式转RPN
2.数据结构与算法
DS:stack
Algorithm:
参考我的上一篇文章:《中缀表达式求值 延迟缓冲》
只需对其代码进行扩充,即可得到中缀表达式转RPN的算法。
扩充内容(可以使用文本对比工具)
扫描中缀表达式:
当遇到数字,将数字追加到RPN后。
当遇到符号,若执行计算(即>分支),则将栈顶符号追加到RPN后。
3.源代码
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int calculate(int n);
int calculate(char top, int left, int right);
char getOrder(char top, char now);
int getNum(string str, int &i, string &rpn);
//中缀表达式求值
int infix_exp(string str, string &rpn){
str += '\0';
stack<int> s1; //数字栈
stack<char> s2; //运算符栈
s2.push('\0');
int len = str.size();
int i = 0;
while(!s2.empty()){
if(isdigit(str[i]

本文介绍如何将中缀表达式转换为逆波兰表示法(RPN)。通过扫描中缀表达式,遇到数字时追加到RPN,遇到符号时根据优先级将栈顶符号追加。算法实现参考了《中缀表达式求值 延迟缓冲》并进行扩充,同时提到手工转换和算法转换两种方法。源代码、时间复杂度分析以及相关参考书籍也进行了说明。
最低0.47元/天 解锁文章
1183

被折叠的 条评论
为什么被折叠?



