资源简介
这是严蔚敏《数据结构》配套习题册上的题目:将逆波兰式转换成波兰式,并提示错误(作为简化,只处理"+-*/"和0~9的数字)。
例如:"123*-"转换成波兰式为"-1*23"
逆波兰式"123*-"的表达式树如下:
所以这个转换过程就是:已知一个二叉树的后根遍历序列,求先根遍历序列。
算法是根据后根遍历的序列构造一个表达式树,进而先根遍历此树获得波兰式表达式。
代码片段和文件信息
#include
#include
#include
#include
#include
using namespace std;
using boost::shared_ptr;
struct Exp;
struct Item{
char number;
shared_ptr pExp;
bool isNumber;
explicit Item():isNumber(true) number(‘0‘) pExp(){ }
Item(const Item& i):number(i.number) pExp(i.pExp) isNumber(i.isNumber){ }
};
struct Exp{
char op;
Item lhs;
Item rhs;
Exp(){};
Exp(char _op Item _lhs Item _rhs):op(_op) lhs(_lhs) rhs(_rhs){ }
Exp(const Exp& e):op(e.op) lhs(e.lhs) rhs(e.rhs) { }
};
class Error{
string info;
public:
Error(string _info):info(_info){ }
Error():info(““){ }
string what(){return info;}
};
void printPorland(Exp& exp){
cout << exp.op ;
if(exp.lhs.isNumber) cout << exp.lhs.number;
else printPorland(*exp.lhs.pExp);
if(exp.rhs.isNumber) cout << exp.rhs.number;
else printPorland(*exp.rhs.pExp);
return;
}
int main()
{
stack- ExpStack;
char tmpChar;
Item tmpItem;
Item tmpLhs;
Item tmpRhs;
string numbers = “0123456789“;
string operators = “+-*/“;
cout<<“Input the Express(输入 ‘e‘标识结束):“;
do{
try{
while(cin>>tmpChar){
- 上一篇:STC15的modbus程序
- 下一篇:C++课件+STL
相关资源
- 将图片转换为C语言数组的程序
- Hex文件转bin文件
- 使用 IBM Rational Systems Developer 和 Rati
- pcm转换成wav
- 通达信day格式文件转换含港股和基金
- C语言版经纬度与高斯投影相互转换函
- UNIX/LINUX下C语言中文短信UCS2编码和解
- VC++阳历(公历)到阴历(农历)转换
- WGS84 北京54 西安80 CGCS2000 布尔沙七参
- 经纬度坐标转换为平面坐标的matlab实
- C语言实现51单片机和ADC0809芯片的AD模
- 圆的扫描转换,中点bresenham画圆算法
- 段页式存储管理地址转换
- DICOM图片转换BMP的C++代码
- 大地坐标与三维坐标转换
- 多边形扫描转换算法
- Delphi2Cppdelphi代码转换为C++代码
- Float与IEEE754格式相互转换源代码原创
- C# To C++ Converter 17.10.2 (最新破解版
- 正规式到NFA的转换
- HLS:C语言转换FPGA教程ug871
- 将MATLAB程序代码转换成C++程序代码
- C++ MFC实现的字符点阵转换工具
- 多边形的扫描转换算法实现
- 可以将DLL文件转换成源码
- 图形学实验 多边形的扫描转换 扫面线
- Bmp2jpeg图片格式转换
- 平面坐标间转换
- C++任意进制转换
- C\\C++ 任意(281016)进制转换为(281
评论
共有 条评论