#include <iostream>
#include <string>
#include <sstream>
using namespace std;
//------------------------------栈----------------------------------
typedef string Elemtype;
// 定义一个链栈
typedef struct Node
{
Elemtype elem;
struct Node* next;
}Node, *LinkedStack;
// 创建一个空栈
void createStack(LinkedStack &l)
{
l = new Node;
l->next = NULL;
}
// 栈是否为空
bool isEmpty(LinkedStack l)
{
if(l->next == NULL){
return true;
}
return false;
}
// 压栈
void push(LinkedStack &l, Elemtype e)
{
LinkedStack temp = new Node;
temp->elem = e;
temp->next = l->next;;
l->next = temp;
}
// 出栈
void pop(Elemtype &e, LinkedStack &l)
{
if(isEmpty(l)) return;
e = l->next->elem;
l->next = l->next->next;
}
// 查看栈顶元素
Elemtype peek(LinkedStack l)
{
return l->next->elem;
}
// 判断该字符是否是小数点或者数字
bool isNum(char ch)
{
if(ch >= '0' && ch <= '9' || ch == '.') return true;
return false;
}
// 判断该字符是否为运算符
bool isOper(char ch)
{
if(ch != '+' && ch != '-' && ch != '*' && ch != '/') return false;
return true;
}
// 判断该字符是否为括号
bool isBracket(char ch)
{
if(ch != '(' && ch != ')') return false;
return true;
}
// 判断优先级
bool highPriority(char ch1, char ch2)
{
if(ch2 == '+' || ch2 == '-'){
if(ch1 == '*' || ch1 == '/') return true;
}
return false;
}
// 获取数字
string getNum(string str, int index)
{
// 缓存数字字符串
string numStr = "";
for(int i = index; i < str.size(); i++){
if(isNum(str[i])) numStr += str[i];
else break;
}
return numStr;
}
//前缀表达式转后缀表达式
LinkedStack toPostfixStr(string prefix)
{
LinkedStack result;
LinkedStack oper;
createStack(result);
createStack(oper);
// 缓存字符
char ch;
// 多一个布尔标记来表示需不需要判断是否为数字
bool isJudge = true;
for(int i = 0; i < prefix.size(); i++){
// 判断是否为数字
if(isJudge && isNum(prefix[i])){
push(result, getNum(prefix, i));
// 改变布尔标识符
isJudge = false;
}else if(isOper(ch = prefix[i])){// 如果是运算符
while(true){
// 如果栈为空,或栈顶为(,或优先级高,直接压栈
if(isEmpty(oper)){
string tempS = "a";
tempS[0] = ch;
push(oper, tempS);
break;
}
// 到这栈不为空,直接peek
string temp = peek(oper);// 获取栈顶元素
if(temp[0] == '(' || highPriority(ch, temp[0])){
string tempS = "a";
tempS[0] = ch;
push(oper, tempS);
break;
}else{// 否则将运算符弹出压到result里,重复以上
Elemtype e;
pop(e, oper);
push(result, e);
}
}
// 更改判断标记
isJudge = true;
}else if(isBracket(ch = prefix[i])){// 如果是括号
// 如果是(,直接压栈
if(ch == '('){
string tempS = "a";
tempS[0] = ch;
push(oper, tempS);
}
else{// 如果是),依次弹出运算符,知道遇到(,并将括号丢弃
Elemtype e;
string temp = peek(oper);
while(temp[0] != '('){
pop(e, oper);
push(result, e);
temp = peek(oper);
}
pop(e, oper);// 去掉运算符中的(
}
// 更改判断标记
isJudge = true;
}
}
// 将剩余运算符压入
while(!isEmpty(oper)){
Elemtype e;
pop(e, oper);
push(result, e);
}
LinkedStack postfix;
createStack(postfix);
while(!isEmpty(result)){
Elemtype e;
pop(e, result);
push(postfix, e);
}
return postfix;
}
// 将字符串的数字转换为数字
double toNum(string str)
{
bool isNeg = false;
bool isPoint = false;
double integer = 0;
double minnum = 0;
int j = 0;// 计算小数点追加次数
for(int i = 0; i < str.size(); i++){
if(str[i] == '-'){
isNeg = true;
continue;
}
if(str[i] == '.'){
isPoint = true;
continue;
}
if(!isPoint){
double temp = str[i] - '0';
integer *= 10;
integer += temp;
}else{
double temp = str[i] - '0';
minnum *= 10;
minnum += temp;
j++;
}
}
// 将nimnum转换为小数
for(int i = 0; i < j; i++){
minnum /= 10;
}
if(isNeg){
return (0-(integer+minnum));
}else{
return integer+minnum;
}
}
// 将数字转换为字符串
string toStr(double num)
{
stringstream ss;
ss << num;
return ss.str();
}
// 求解后缀表达式
double getVal(LinkedStack result)
{
LinkedStack operS;
createStack(operS);
// 遍历表达式
while(!isEmpty(result)){
Elemtype e;
double res;
pop(e, result);
// 如果是数字,直接压栈
if(isNum(e[0])){
push(operS, e);
}else{// 不然就是运算符
//弹出两个元素进行计算
Elemtype num1;
Elemtype num2;
pop(num1, operS);
pop(num2, operS);
if(e[0] == '+'){
res = toNum(num1) + toNum(num2);
push(operS, toStr(res));
}else if(e[0] == '-'){
res = toNum(num2) - toNum(num1);
push(operS, toStr(res));
}else if(e[0] == '*'){
res = toNum(num1) * toNum(num2);
push(operS, toStr(res));
}else{
res = toNum(num2) / toNum(num1);
push(operS, toStr(res));
}
}
}
Elemtype elem;
pop(elem, operS);
return toNum(elem);
}
// 输出后缀表达式
void msg(LinkedStack ls)
{
// 遍历表达式
while(!isEmpty(ls)){
Elemtype e;
pop(e, ls);
cout<<e<<" ";
}
}
int main()
{
string prefix = "(5.3+4.7)*5+(5.9-(4+4))";
LinkedStack ls;
ls = toPostfixStr(prefix);
//msg(toPostfixStr(prefix));
cout<<prefix<<"="<<getVal(toPostfixStr(prefix));
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
c++实现的数据结构-DataStructure.zip
共94个文件
o:23个
exe:23个
cbp:13个
需积分: 5 0 下载量 51 浏览量
2024-05-23
10:52:43
上传
评论
收藏 9.38MB ZIP 举报
温馨提示
c++实现的数据结构(数组二叉树、链式二叉树、线索化二叉树、集合、图遍历DFA\BFS、约瑟夫环、多项式操作) c++实现的数据结构(数组二叉树、链式二叉树、线索化二叉树、集合、图遍历DFA\BFS、约瑟夫环、多项式操作) c++实现的数据结构(数组二叉树、链式二叉树、线索化二叉树、集合、图遍历DFA\BFS、约瑟夫环、多项式操作)
资源推荐
资源详情
资源评论
收起资源包目录
c++实现的数据结构-DataStructure.zip (94个子文件)
DataStructure
Graph
Graph.layout 176B
main.cpp 2KB
main.exe 1.52MB
main.o 38KB
Graph.cbp 1KB
福利.jpg 524KB
collection
collection.layout 357B
obj
Release
main.o 1KB
Debug
main.o 11KB
collection.depend 99B
main.cpp 6KB
main.exe 1.5MB
bin
Release
collection.exe 784KB
Debug
collection.exe 1.5MB
main.o 5KB
collection.cbp 1KB
ValueOfExpression
obj
Debug
main.o 10KB
main.cpp 6KB
main.exe 1.6MB
bin
Debug
ValueOfExpression.exe 1.5MB
ValueOfExpression.depend 118B
main.o 15KB
ValueOfExpression.cbp 1KB
ValueOfExpression.layout 357B
ThreadedBinaryTree
main.cpp 2KB
main.exe 1.49MB
ThreadedBinaryTree.cbp 1KB
main.o 2KB
LinkGraph
obj
Debug
main.o 154KB
main.cpp 3KB
main.exe 1.52MB
bin
Debug
LinkGraph.exe 1.6MB
main.o 44KB
LinkGraph.cbp 1KB
JosephuRing
obj
Debug
main.o 13KB
JosephuRing.layout 360B
JosephuRing.depend 31B
main.cpp 3KB
main.exe 1.49MB
bin
Debug
JosephuRing.exe 1.5MB
main.o 3KB
JosephuRing.cbp 1KB
LinkedBinaryTree
Maze
Maze.layout 359B
Maze.cbp 1KB
obj
Debug
main.o 15KB
main.cpp 4KB
main.exe 1.49MB
bin
Debug
Maze.exe 1.5MB
Maze.depend 98B
main.o 4KB
LinkedBinaryTree.depend 105B
obj
Debug
main.o 11KB
main.cpp 4KB
main.exe 1.52MB
bin
Debug
LinkedBinaryTree.exe 1.5MB
LinkedBinaryTree.layout 358B
main.o 44KB
LinkedBinaryTree.cbp 1KB
Polynomial
obj
Debug
main.o 15KB
main.cpp 4KB
Polynomial.depend 99B
main.exe 1.49MB
bin
Debug
Polynomial.exe 1.5MB
Polynomial.cbp 1KB
Polynomial.layout 360B
main.o 4KB
ArrayBinaryTree
BinaryTree.depend 99B
obj
Debug
main.o 14KB
BinaryTree.layout 361B
main.cpp 2KB
bin
Debug
BinaryTree.exe 1.5MB
BinaryTree.cbp 1KB
ValueOfExpressionⅡ
obj
Debug
main.o 39KB
ValueOfExpressionⅡ.cbp 1KB
main.cpp 1KB
ValueOfExpressionⅡ.depend 108B
bin
Debug
ValueOfExpressionⅡ.exe 1.53MB
ValueOfExpressionⅡ.layout 360B
LinkedBinaryTree
Maze
Maze.layout 359B
Maze.cbp 1KB
obj
Debug
main.o 15KB
main.cpp 4KB
main.exe 1.49MB
bin
Debug
Maze.exe 1.5MB
Maze.depend 98B
main.o 4KB
LinkedBinaryTree.depend 105B
obj
Debug
main.o 11KB
main.cpp 5KB
main.exe 1.52MB
bin
Debug
LinkedBinaryTree.exe 1.5MB
LinkedBinaryTree.layout 358B
main.o 48KB
LinkedBinaryTree.cbp 1KB
共 94 条
- 1
资源评论
逃逸的卡路里
- 粉丝: 7424
- 资源: 3628
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功