C++实现1. 使用链表创建栈 2. 完成栈的基本操作,创建、销毁、清空、判断是否为空、获取栈的长度、返回栈顶元素、入栈、出栈 3. 从键盘输入一个四则运算表达式(允许退格操作),完成中序表达式与后序表达式的互相转换并计算结

时间: 2023-06-23 09:10:13 浏览: 81
好的,下面是C++实现: ``` #include <iostream> #include <cstring> using namespace std; struct Node { char data; Node *next; }; class Stack { private: Node *top; int length; public: Stack() { top = NULL; length = 0; } ~Stack() { clear(); } void clear() { while (top != NULL) { Node *temp = top; top = top->next; delete temp; } length = 0; } bool isEmpty() { return (top == NULL); } int getLength() { return length; } char getTop() { if (isEmpty()) { return '\0'; } return top->data; } bool push(char data) { Node *node = new Node; node->data = data; node->next = top; top = node; length++; return true; } bool pop() { if (isEmpty()) { return false; } Node *temp = top; top = top->next; delete temp; length--; return true; } }; int priority(char op) { if (op == '+' || op == '-') { return 1; } if (op == '*' || op == '/') { return 2; } return 0; } bool isOperator(char c) { return (c == '+' || c == '-' || c == '*' || c == '/'); } void infixToPostfix(char infix[], char postfix[]) { Stack stack; int len = strlen(infix); int j = 0; for (int i = 0; i < len; i++) { if (infix[i] == '\b') { if (j > 0) { j--; } continue; } if (isdigit(infix[i])) { postfix[j++] = infix[i]; } else if (isOperator(infix[i])) { while (!stack.isEmpty() && priority(stack.getTop()) >= priority(infix[i])) { postfix[j++] = stack.getTop(); stack.pop(); } stack.push(infix[i]); } else if (infix[i] == '(') { stack.push(infix[i]); } else if (infix[i] == ')') { while (!stack.isEmpty() && stack.getTop() != '(') { postfix[j++] = stack.getTop(); stack.pop(); } if (!stack.isEmpty()) { stack.pop(); } } } while (!stack.isEmpty()) { postfix[j++] = stack.getTop(); stack.pop(); } postfix[j] = '\0'; } int calculate(char op, int num1, int num2) { if (op == '+') { return num1 + num2; } if (op == '-') { return num1 - num2; } if (op == '*') { return num1 * num2; } if (op == '/') { return num1 / num2; } return 0; } int evaluatePostfix(char postfix[]) { Stack stack; int len = strlen(postfix); for (int i = 0; i < len; i++) { if (isdigit(postfix[i])) { stack.push(postfix[i] - '0'); } else if (isOperator(postfix[i])) { int num2 = stack.getTop(); stack.pop(); int num1 = stack.getTop(); stack.pop(); stack.push(calculate(postfix[i], num1, num2)); } } return stack.getTop(); } int main() { char infix[100]; char postfix[100]; cout << "请输入一个中序表达式(允许使用退格键):" << endl; cin.getline(infix, 100); infixToPostfix(infix, postfix); cout << "转换后的后序表达式为:" << postfix << endl; cout << "计算结果为:" << evaluatePostfix(postfix) << endl; return 0; } ``` 在这个程序中,我们使用了链表来实现栈。栈具有的基本操作都被实现了,并且我们还实现了中序表达式到后序表达式的转换,以及后序表达式的计算。

相关推荐

最新推荐

recommend-type

C语言实现带头结点的链表的创建、查找、插入、删除操作

主要介绍了C语言实现带头结点的链表的创建、查找、插入、删除操作方法,对于了解数据结构中链表的各项操作有很好的借鉴价值,需要的朋友可以参考下
recommend-type

C++双向链表实现简单通讯录

主要为大家详细介绍了C++双向链表实现简单通讯录,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

课设报告-“贪吃蛇_C语言_链表实现”-CSDN~.docx

课设报告-“贪吃蛇_C语言_链表实现~_课设”中 兑现将课设报告另发一篇博客~
recommend-type

C语言实现带头结点的单向链表的基本操作

使用C语言实现了单向链表的创建,输出,插入元素和删除元素以及单向链表的逆序连接和两个有序线性表的归并
recommend-type

数据结构 实验二 链表、栈、队列的

数据结构 实验二 链表、栈、队列的数据结构 实验二 链表、栈、队列的数据结构 实验二 链表、栈、队列的数据结构 实验二 链表、栈、队列的数据结构 实验二 链表、栈、队列的
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。