数据结构课设——表达式求值(c++)

完成基本功能:
(1) 从文件或者键盘读入中缀表达式。
(2) 设计操作数为多位整数,操作符为加、减、乘、除、求模的中缀表达式求值算法
(3) 设计将中缀表达式转换为后缀表达式的算法
(4) 设计将中缀表达式转换为前缀表达式的算法
(5) 设计后缀表达式的求值算法
(6) 设计前缀表达式的求值算法
(7) 输出各种形式的表达式
(8) 扩充运算符集,可进行指数运算
使用说明:
(1)输入的表达式可以带等号也可不带
(2)表达式操作符之间可以有任意空格,但总体表达式不能超过50个字符
(3)只支持加、减、乘、除、求模,乘方运算
(4)程序功能之间高内聚,低耦合,方便修改

中缀表达式转后缀表达式

规则:

从左到右遍历中缀表达式的每个数字和符号,若是数字就输出,即成为后缀表达式的一部分;若是符号,则判断其与栈顶符号的优先级,如果是右括号或者优先级不高于栈顶符号(乘除优先于加减)则栈顶元素依次出栈并输出,并将当前符号进栈,一直到最终输出后缀表达式为止

程序源文件

Caculate.h

#pragma once
#include<iostream>
#include<vector>

using namespace std;

//定义运算符优先级,越大优先级越高
constexpr auto ADD = 1;
constexpr auto SUB = 1;
constexpr auto MUL = 2;
constexpr auto DIV = 2;
constexpr auto Demo = 2;	//求模取余
constexpr auto Index = 3;	//指数 例如 2^3 = 8



class Caculate {
   
public:

	//是否输入了表达式, 默认false
	bool is_Input = false;

	//展示菜单
	void show_Menu();

	//获取运算符优先级
	int get_Value(string operation);

	//获取简单表达式计算结果
	//a 代表操作数 1,str 代表 操作运算符,b 代表操作数 2
	// a str b : 例如 a - b, a*b
	int get_result(int a, string str, int b);
	//输入中缀表达式
	void input_Infix();

	//存储中缀表达式
	vector<string> infix_Expression;

	//存储前缀表达式
	vector<string> prefix_Expression;

	//存储后缀表达式
	vector<string> postfix_Expression;


	//由中缀表达式转成前缀表达式
	void to_Prefix();

	//由中缀表达式转成后缀表达式
	void to_Postfix();

	//前缀表达式计算求值
	void caculate_Prefix();

	//中缀表达式计算求值
	void caculate_Infix();

	//后缀表达式计算求值
	void caculate_Postfix();

	//遍历展示vector容器中元素
	void show_Vector(vector<string> expression);

	//展示表达式的各种形式
	void show_Expression();

	//退出系统
	void exit_System();
};

Caculate.cpp

#include<iostream>
#include<stack>
#include<string>
#include<stack>
#include<sstream>
#include<algorithm>
#include<math.h>
#include "Calculate.h"

using namespace std;



//退出系统
void Caculate::exit_System() {
   
	cout << "欢迎下次使用!:)" << endl;
	system("pause");
	exit(0);
}

//展示菜单
void Caculate::show_Menu() {
   
	cout << endl;
	cout << "***********************************************" << endl;
	cout << "*************** 表达式求值计算器 ***************" << endl;
	cout << "***********************************************" << endl;
	cout << "*********** >1. 输入中缀表达式   ****************" << endl;
	cout << "*********** >2. 中缀表达式求值   ****************" << endl;
	cout << "*********** >3. 前缀表达式求值   ****************" << endl;
	cout << "*********** >4. 后缀表达式求值   ****************" << endl;
	cout << "*********** >5. 表达式的各种形式 ****************" << endl;
	cout << "*********** >0. 退出系统        ****************" << endl;
	cout << "***********************************************" << endl;
	cout << "***********************************************" << endl;
	cout << endl;
}

//获取元素优先级
int Caculate::get_Value(string operation) {
   
	int value = 0;

	if (operation == "+") {
   
		value = ADD;
	}
	else if (operation == "-") {
   
		value = SUB;
	}
	else if (operation == "*") {
   
		value = MUL;
	}
	else if (operation == "/") {
   
		value = DIV;
	}
	else if (operation == "%") {
   
		value = Demo;
	}
	else if (operation == "^") {
   
		value = Index;
	}
	else {
   
		value = 0;
	}

	return value;
}

//获取简单表达式计算结果
int Caculate::get_result(int a, string str, int b) {
   
	int result = 0;
	if (str == "+") {
   
		result = a + b;
	}
	else if (str == "-")
  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值