内容:
项目名称:简易计算器程序项目内容:
编写程序,模拟简单运算器的工作
:输入一个算式(没有空格),遇等号“=”说明输入结束,输出结果。假设计算器只能计算加减乘除运算,运算数和结果都是整数。要求完成以下功能:
(1) 从键盘录入中缀表达式,将中缀表达式转换为后缀表达式输出;
(2) 输入后缀表达式,计算后缀表达式的值。
代码实现:
main函数:
#include<iostream>
#include<string>
#include"SqStack.hpp"
using namespace std;
void zhong(SqStack<char>& s2) {
string all;
cout << "请输入中缀表达式:" << endl;
cin >> all;
int a = 0;
for (int i = 0; i <= all.length(); i++) {
if (all[i] <= '9' && all[i] >= '0') {
while (true) {
a = a * 10 + (int)(all[i] - '0');
i++;
if (!(all[i] <= '9' && all[i] >= '0')) {
cout << a << " ";
a = 0;
break;
}
}
}
if (all[i] == '(')
s2.push(all[i]);
if (all[i] == '+' || all[i] == '-' || all[i] == '*' || all[i] == '/') {
while (true) {
char p;
if (s2.isEmpty()) {
s2.push(all[i]);
break;
}
else
s2.getTop(p);
if (p == '(') {
s2.push(all[i]);
break;
}
else if (all[i] == '+' || all[i] == '-') {
s2.pop(p);
cout << p;
s2.push(all[i]);
break;
}
else {
if (p == '*' || p == '/') {
s2.pop(p);
cout << p;
break;
}
else
{
s2.push(all[i]);
break;
}
}
}
}
if (all[i] == ')') {
while (true) {
char n;
s2.getTop(n);
if (n == '(') {
s2.pop(n);
break;
}
else {
cout << n;
s2.pop(n);
}
}
}
}
while (true) {
if (s2.isEmpty())
break;
else {
char c;
s2.pop(c);
cout << c;
}
}
cout << endl;
}
void hou(SqStack<int>& s1) {
cout << "输入后缀表达式:" << endl;
string all;
string f;
getline(cin, f);
getline(cin, all);
int a = 0;
for (int i = 0; i < all.length(); i++) {
if (all[i] <= '9' && all[i] >= '0') {
while (true) {
a = a * 10 + (int)(all[i] - '0');
i++;
if (!(all[i] <= '9' && all[i] >= '0')) {
s1.push(a);
a = 0;
break;
}
}
}
if (all[i] == ' ') {
continue;
}
else if (!(all[i] <= '9' && all[i] >= '0')) {
int a;
int b;
s1.pop(a);
s1.pop(b);
if (all[i] == '+')
s1.push(b + a);
else if (all[i] == '-')
s1.push(b - a);
else if (all[i] == '*')
s1.push(b * a);
else if (all[i] == '/')
s1.push(b / a);
else
cout << "输入错误" << endl;
}
}
int ave;
s1.getTop(ave);
cout << "结果是:" << ave << endl;
}
int main() {
SqStack<int>* z_stack = new SqStack<int>();
SqStack<char>* h_stack = new SqStack<char>();
int sign = 0;
while (true) {
cout << "(1)从键盘录入中缀表达式,将中缀表达式转换为后缀表达式输出" << endl;
cout << "(2)输入后缀表达式,计算后缀表达式的值" << endl;
cout << "(3)退出" << endl;
cin >> sign;
switch (sign) {
case 1:
zhong(*h_stack);
break;
case 2:
hou(*z_stack);
break;
case 3:
return 0;
break;
}
}
system("pause");
return 0;
}
SqStack.hpp的实现:
#ifndef __SQ_STACK_HPP__
#endif
#define __SQ_STACK_HPP__
// ANSI C++标准库头文件
#include <cstring> // 标准串操作
#include <iostream> // 标准流操作
using namespace std;
#define DEFAULT_SIZE 100
template<class ElemType>
class SqStack
{
protected:
// 顺序栈的数据成员:
int top; // 栈顶指针
int maxSize; // 栈的最大容量
ElemType* data; // 元素存储空间
public:
// 顺序栈的方法声明及重载编译系统默认方法声明:
SqStack(int size = DEFAULT_SIZE); // 构造函数
virtual ~SqStack(); // 析构函数
int getLength() const; // 求栈的长度
bool isEmpty() const; // 判断栈是否为空
void clear(); // 将栈清空
void traverse(void (*Visit)(const ElemType&)) const; // 遍历栈
int push(const ElemType e); // 入栈
int getTop(ElemType& e) const; // 取顶元素
int pop(ElemType& e); // 出栈
SqStack(const SqStack<ElemType>& s); // 复制构造函数
SqStack<ElemType>& operator =(const SqStack<ElemType>& s); // 赋值语句重载
};
// 构造函数
template<class ElemType>
SqStack<ElemType>::SqStack(int size) {
this->top = -1;
this->maxSize = size;
this->data = new ElemType[size];
}
// 析构函数
template<class ElemType>
SqStack<ElemType>::~SqStack() {
delete[] data;
}
// 求栈的长度
template<class ElemType>
int SqStack<ElemType>::getLength() const {
return this->top + 1;
}
// 判断栈是否为空
template<class ElemType>
bool SqStack<ElemType>::isEmpty() const {
return this->top == -1;
}
// 将栈清空
template<class ElemType>
void SqStack<ElemType>::clear() {
this->top = -1;
}
// 遍历栈
template<class ElemType>
void SqStack<ElemType>::traverse(void (*Visit)(const ElemType&)) const {
for (int i = 0; i <= this->top; i++) {
(*Visit)(data[i]);
}
}
// 入栈
template<class ElemType>
int SqStack<ElemType>::push(const ElemType e) {
if (this->top == DEFAULT_SIZE-1)
cout << "栈满了" << endl;
else {
this->data[this->top + 1] = e;
this->top++;
}
return 0;
}
// 取顶元素
template<class ElemType>
int SqStack<ElemType>::getTop(ElemType& e) const {
if (this->top == -1)
cout << "栈空了" << endl;
else
e = this->data[this->top];
return 0;
}
// 出栈
template<class ElemType>
int SqStack<ElemType>::pop(ElemType& e) {
if (this->top == -1)
cout << "栈空了" << endl;
else {
e = this->data[this->top];
this->top--;
}
return 0;
}
// 复制构造函数
template<class ElemType>
SqStack<ElemType>::SqStack(const SqStack<ElemType>& s) {
this->top = s.top;
for (int i = 0; i <s.top; i++) {
this->data[i] = s.data[i];
}
}
// 赋值语句重载
template<class ElemType>
SqStack<ElemType>& SqStack<ElemType>::operator =(const SqStack<ElemType>& s) {
this->top = s.top;
for (int i = 0; i < s.top; i++) {
this->data[i] = s.data[i];
}
}