核心算法:

mid=FormatMid(mid);   //格式化中缀表达式
JudgeLegalMid(mid); //判断中缀表达式的合法性
MidToPost mtp(mid);
mtp.ToPost(post); //中缀表达式转后缀表达式
cout <<"结果:" <<Calc(post) <<endl; //计算后缀表达式

具体过程——

第一步:格式化中缀表达式

  这一步的目的是为了解决“-”的歧义问题:有时候“-”是一元运算符,有时候“-”是二元运算符。可以用一种巧妙的方案解决歧义——在一元运算符的“-”前面添上0。这样,此后就可以把+-*/统一当做二元运算符处理。

具体代码:(放在FormatMid.h文件中)

#include<string>
#include<vector>
#include<iostream>
using namespace std; string FormatMid(string mid)
{
for (int i=;i<mid.length();i++)
{
if (mid[i]=='-' && (i==||mid[i-]=='('))
{
mid.insert(i,"");
i++;
}
}
string realmid="";
for (int i=;i<mid.length();i++)
{
if (mid[i]!='-')
{
realmid+=mid[i];
}
else
{
int cou=;
while (mid[i]=='-') i++,cou++;
if (cou%==) realmid+='+';
else realmid+='-';
i--;
}
}
return realmid;
}

第二步:判断中缀表达式的合法性

  这一步也是为了程序的鲁棒性,具体代码如下:(放在JudgeLegalMid.h文件中)

#include<iostream>
#include<cstring>
#include<vector>
#include<string>
using namespace std; void illegal()
{
cout <<"表达式不合法!" <<endl;
exit();
} bool isysf(char cc)
{
if (cc=='+' || cc=='-' || cc=='*' || cc=='/') return true;
else return false;
} void JudgeLegalMid(string &c)
{
int l=c.length();
if (!isdigit(c[]) && c[]!='(') illegal(); //首位不是(也不是数字,不合法
if (!isdigit(c[l-]) && c[l-]!=')') illegal(); //末位不是)也不是数字,不合法
int now=; //括号不匹配,不合法
for (int i=;i<l;i++)
{
if (c[i]=='(') now++;
else if (c[i]==')') now--;
if (now<) illegal();
}
if (now!=) illegal();
for (int i=;i<l-;i++)
{
if (c[i]=='+' || c[i]=='*' || c[i]=='/' || c[i]=='-') //二元运算符
{
if (!isdigit(c[i-]) && c[i-]!=')') //左边不是)也不是数字,不合法
illegal();
if (!isdigit(c[i+]) && c[i+]!='(') //右边不是(也不是数字,不合法
illegal();
}
if (c[i]=='.' && !isdigit(c[i-]) && !isdigit(c[i+])) illegal(); //.左右不是数字,不合法
}
for (int i=;i<l;i++) //.向左右扩展 ,在遇到运算符之前,只能扩展出数字才合法
{
if (c[i]=='.')
{
int j=i-,k=i+;
while (j> && isdigit(c[j])) j--;
while (k<l && isdigit(c[k])) k++;
if (c[j]=='.' || c[k]=='.') illegal();
}
}
}

第三步:中缀表达式转后缀表达式

  这一步是比较经典的栈处理,具体代码如下:(放在MidToPost.h文件中,封装成了类)

#ifndef MIDTOPOST_H____
#define MIDTOPOST_H____ #include<iostream>
#include<cstring>
#include<stack>
#include<vector>
#include<string>
#include<sstream>
using namespace std; string cts(char cc)
{
stringstream stream;
stream << cc;
return stream.str();
} class MidToPost
{
private:
string c;
stack<string> s;
int yxj(char ope); //返回一个运算符的优先级
public:
MidToPost(const string cc); //构造函数
void ToPost(vector<string>& res); //转为后缀表达式
string readnum(int p);
}; int MidToPost::yxj(char ope)
{
if (ope=='*' || ope=='/') return ;
if (ope=='+' || ope=='-') return ;
if (ope=='@' || ope=='(') return -;
} string MidToPost::readnum(int p)
{
string s="";
while (isdigit(c[p]) || c[p]=='.')
{
s+=cts(c[p]);
p++;
}
return s;
} MidToPost::MidToPost(const string cc)
{
c=cc;
while (!s.empty()) s.pop();
s.push(string("@"));
} void MidToPost::ToPost(vector<string>& res)
{
res.clear();
int now=;
int l=c.length();
for (int i=;i<l;i++)
{
if (isdigit(c[i]))
{
res.push_back(readnum(i));
while (isdigit(c[i]) || c[i]=='.') i++;
i--;
}
else if (c[i]=='(') s.push(cts(c[i]));
else if (c[i]==')')
{
while (s.top()[]!='(')
{
res.push_back(s.top());
s.pop();
}
s.pop();
}
else
{
int k1,k2;
k1=yxj(s.top()[]);
k2=yxj(c[i]);
while (k1>=k2)
{
res.push_back(s.top());
s.pop();
k1=yxj(s.top()[]);
k2=yxj(c[i]);
}
s.push(cts(c[i]));
}
}
while (s.top()[]!='@')
{
res.push_back(s.top());
s.pop();
}
}
#endif

第四步:计算后缀表达式的值

  同样是经典的栈处理,对于除数为0的情况由于C++自带inf所以就没有特判。代码如下:(放在CalcPost.h文件中)

#include<iostream>
#include<cstring>
#include<vector>
#include<string>
#include<vector>
#include<sstream>
using namespace std; stack<double> s; double Calc(vector<string> & c)
{
while (!s.empty()) s.pop();
int l=c.size();
for (int i=;i<l;i++)
{
if (isdigit(c[i][]))
{
stringstream ss(c[i]);
double tmp;
ss >>tmp;
s.push(tmp);
}
else
{
double res2=s.top();
s.pop();
double res1=s.top();
s.pop();
switch(c[i][])
{
case '+':res1+=res2;break;
case '-':res1-=res2;break;
case '*':res1*=res2;break;
case '/':res1/=res2;break;
}
s.push(res1);
}
}
return s.top();
}

主程序:

#include"MidToPost.h"
#include"CalcPost.h"
#include"JudgeLegalMid.h"
#include"FormatMid.h"
#include<iostream>
#include<cstring>
#include<string>
#include<vector>
using namespace std; const int maxn=;
string mid;
vector<string> post; int main()
{
cout <<"请输入中缀表达式(实数加减乘除括号运算):";
while (cin >>mid)
{
mid=FormatMid(mid);
cout <<"等价转换后的中缀表达式:" <<mid <<endl;
JudgeLegalMid(mid);
MidToPost mtp(mid);
mtp.ToPost(post);
cout <<"后缀表达式:";
for (int i=;i<post.size();i++)
cout <<post[i]<<" ";
cout <<endl;
cout <<"结果:" <<Calc(post) <<endl;
cout <<"请输入中缀表达式(实数加减乘除括号运算):";
}
return ;
}

运行结果:

适用于实数范围的中缀表达式的 + - * / ( ) 计算(C++实现)的更多相关文章

  1. C语言数据结构之栈:中缀表达式的计算

    *注:本人技术不咋的,就是拿代码出来和大家看看,代码漏洞百出,完全没有优化,主要看气质,是吧 学了数据结构——栈,当然少不了习题.习题中最难的也是最有意思的就是这个中缀表达式的计算了(可以算+-*/和 ...

  2. C++实现顺序栈类求解中缀表达式的计算

    控制台第一行打印的数值为使用形如以下方式得到的结果: cout << +*(+)*/- << endl; 即第一个待求解表达式由C++表达式计算所得结果,以用于与实现得出的结果 ...

  3. Java堆栈的应用2----------中缀表达式转为后缀表达式的计算Java实现

    1.堆栈-Stack 堆栈(也简称作栈)是一种特殊的线性表,堆栈的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置进行插入和删除操作,而堆栈只允许在固定一端进行插入和删除 ...

  4. Infix expressions 中缀表达式

    中缀表达式的计算 利用两个栈来实现,操作数栈,操作符栈 只支持个位数运算 最后必须输入一个'#' #include<iostream> using namespace std; templ ...

  5. c语言,中缀表达式转后缀表达式并计算

    //c语言中缀表达式计算 #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ...

  6. Python与数据结构[1] -> 栈/Stack[1] -> 中缀表达式与后缀表达式的转换和计算

    中缀表达式与后缀表达式的转换和计算 目录 中缀表达式转换为后缀表达式 后缀表达式的计算 1 中缀表达式转换为后缀表达式 中缀表达式转换为后缀表达式的实现方式为: 依次获取中缀表达式的元素, 若元素为操 ...

  7. 利用stack结构,将中缀表达式转换为后缀表达式并求值的算法实现

    #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...

  8. RPN-逆波兰计算器-中缀表达式转后缀表达式-javascript

    1.利用栈(Stack)来存储操作数和操作符: 2.包含中缀表达式转后缀表达式的函数,这个是难点,也是关键点: 2.1.将输入字符串转为数组: 2.2.对转换来的字符进行遍历:创建一个数组,用来给存储 ...

  9. 中缀表达式转后缀表达式(用于求字符串表达式值)(js栈和队列的实现是通过数组的push和unshift方法插值,pop方法取值)

    中缀表达式:就是我通常用的算术或逻辑公式: 后缀表达式:不包含括号,运算符放在两个运算对象后面,所有的计算按运算符出现的顺序,严格从左向右进行,不用考虑运算符优先级: 如,(2+1)*3 转换后,2 ...

随机推荐

  1. ruby 数据类型Symbol

    一.符号创建 符号是Symbol类的实例,使用冒号加一个标识符即可创建符号 :a :"This is a symno" 二.符号字符串相互转换 p :symbol.to_s #=& ...

  2. Altium Designer -- 精心总结

    如需转载请注明出处:http://blog.csdn.NET/qq_29350001/article/details/52199356 以前是使用DXP2004来画图的,后来转行.想来已经有一年半的时 ...

  3. CentOS7 添加自定义系统服务案例

    示例一: 执行脚本/root/project/systemctl/test.sh() ######################################################### ...

  4. 触发显示和隐藏 div

    <script> window.onload = function(){ var oDiv1 = document.getElementById("div1"); va ...

  5. IOException: win32 io returned 267. Path:

    unity3d在导出android项目时出现了这个错误,找了一圈也没找到原因,最后把项目名中空格去掉后OK了,坑啊!!!!

  6. Qt Qml 汽车仪表

    上一个原文连接http://blog.csdn.net/z609932088/article/details/53946245 参考资料连接:链接: https://pan.baidu.com/s/1 ...

  7. linux备忘录-日志档案

    linux的日志档案 linux的日志档案记录系统或程序在运行过程中产生的一些信息,例如事件的记录,错误的记录等等.特别是在发生错误时,我们可以通过日志档案找到错误发生的根源,例如当我们无法启动邮件服 ...

  8. highcharts图表插件初探

    转载请注明出处:http://www.cnblogs.com/liubei/p/highchartsOption.html HighCharts简介 Highcharts 是一个用纯JavaScrip ...

  9. winform 根据两点求出线上所有点及画出这条线

    找出所有点: 根据斜率按照一个方向递增,求出对应的另一个方向的整数值. Point pStart = new Point(0, 2); Point pEnd = new Point(8, 2); // ...

  10. 第四次JAVA作业

    public class TvbDog { public static void main(String[] args) {  Dog per=new Dog("陈狗"," ...