C++栈的应用:实现中缀表达式的加减乘除运算(浮点数float型)

代码的思路是:
1.先将中缀表达式以字符串的形式读入,放进字符串中

2.先进型字符串的括号判错,看看有没有(4+5/5的情况,即括号缺失的情况

3.将字符串转化为数字数组(二维)一维存放数据和运算符的ASCII码,另一纬对应地方存放标志符:0是数字,1是运算符。

4.最后使用栈进行后缀表达式运算,涵盖了判除数不为0零的操作。
代码是自己写的,欢迎交流

我是使用字符串读取的中缀表达式,直接用数组没有想到方法。下面是我的代码和结果:

#include<iostream>
#include<cstdio>
#define Maxsize 20
#define MaxCoutlength 20
using namespace std;
//做一个简单的加减乘除运算器

//栈定义
typedef  struct
{
	float num[Maxsize];
	int top;
}SqStack_num;


//初始化栈
void InitStack(SqStack_num& S)
{
	S.top = -1;
}


//新元素入栈
bool Push(SqStack_num& S, float x)
{
	if (S.top == Maxsize - 1)
		return false;
	else
		S.num[++S.top] = x;
	return true;
}


//新元素出栈
bool Pop(SqStack_num& S, float & x)
{
	if (S.top == -1)
		return false;
	else
		x = S.num[S.top--];
	return true;
}

bool emptySqStack(SqStack_num S)
{
	if (S.top == -1)
		return true;
	else return false;
}
//访问栈顶元素
bool gettop(SqStack_num S,float &x)
{
	if (S.top == -1)
		return false;
	else
		x=S.num[S.top];
	return true;
}

//括号匹配
bool brackeCheck(char str[], int length, SqStack_num S)
{
	int i = 0;float x = 0;
	for (i = 0; i <= length - 1; i++)
	{
		if (str[i] == '(')
			Push(S, int(str[i]));
		else
			if (str[i] == ')')
			{
				if (Pop(S, x))
				{
					if (str[i] == ')' && x != 40)
						return false;
				}
				else
					return false;

			}
	}
	return emptySqStack(S);
}

//将字符129转为数字129,特殊字符也转为ASCII

//转为后缀表达式
int PostfixExpression(SqStack_num S, char* str, int length, float str_new[][MaxCoutlength])
{
	int j = 0;
	int i = 0;
	InitStack(S);
	float ok=0;
	for (i = 0; i <= length - 1; )
	{
		int num = 0;//str表达式的某个字符放在这
		float sym = 0;//栈里弹出的运算符放在这
		num = int(str[i]);
		if (num <=57 && num >= 48)
		{
			while ((num <=57 && num >= 48)|| num==46)
			{   
				if(num!=46)
				{
					str_new[0][j] = str_new[0][j] * 10 + num - 48;
					num = int(str[++i]);
				}
				else
				{
					i++;
					num = int(str[i]);
					int xiao = 1;
					while (num <= 57 && num >= 48)
					{
						float xiaoshu = 1;
						int q = 1;
						for (q = 1; q <= xiao; q++)
						{
							xiaoshu = 0.1 * xiaoshu;
						}
						xiao++;
						str_new[0][j] = str_new[0][j]  + (num - 48)*xiaoshu;
						num = int(str[++i]);
					}
				}
			}
			j++;
		}
		else
		{ 
			if (num == 40)
			{
				Push(S, num);
				i++;
				continue;
			}
			if (num == 41)
			{
				while (Pop(S, sym) && sym !=40)
				{
						str_new[0][j] = sym;
						str_new[1][j++] = 1;  //代表这个地方是运算符,0代表是数字
				}
				i++;
				continue;
			}
			if (emptySqStack(S))
			{
				Push(S, int(str[i]));
				i++;
				continue;
			}
			if (num == 42 || num == 47)
			{
				do 
				{
					gettop(S, sym);
					if (sym == 42 || sym == 47)
					{
						Pop(S, sym);
						str_new[0][j] = sym;
						str_new[1][j++] = 1;
					}
				} while (sym == 42 || sym == 47);
				Push(S, num);
				i++;
				continue;
				
			}
			if (num == 43 || num == 45)
			{
				do
				{
					gettop(S, sym);
					if (sym == 42 || sym == 47)
					{
						Pop(S, sym);
						str_new[0][j] = sym;
						str_new[1][j++] = 1;
					}
				} while (sym != 40 );
				Push(S, num);
				i++;
				continue;
			}
		}
	}
	while (Pop(S, ok))
	{
		str_new[0][j] = ok;
		str_new[1][j++] = 1;
	}
	return j;
}


//利用后缀表达式计算
bool count( SqStack_num S,float str_new[][MaxCoutlength], int j,float &result)
{
	InitStack(S);
	int i = 0; int num = 0;
	float a = 0, b = 0;
	for (i = 0; i <j; i++)
	{
		if (str_new[1][i] == 0)
			Push(S, str_new[0][i]);
		else
		{
			if (Pop(S, b) && Pop(S, a))
			{
				if (str_new[0][i] == 42)
				{
					Push(S, a * b);
					continue;
				}
				if (str_new[0][i] == 47)
				{
					if(b != 0)
					{ 
						Push(S, float(a / b));
						continue;
					}
					else
					{
						cout << "除数不能为0"<<endl;
						return false;
					}
				}
				
				if (str_new[0][i] == 43)
				{
					Push(S, a + b);
					continue;
				}
				if (str_new[0][i] == 45)
				{
					Push(S, a - b);
					continue;
				}

			}
			else
				cout << "出错了"<<endl;
			
		}
	}
	Pop(S, result);
	return true;
}

int main()
{
	//初始化一个顺序栈
	SqStack_num S;
	InitStack(S);
	char str[MaxCoutlength]; 
	int length = 0;
	
	while (1)
	{
		float str_new[2][MaxCoutlength] = { 0 };
		cout << "请输入计算表达式:";
		cin >> str;
		if (str[0] == '#')
			return 0;
		length = 0;
		length = strlen(str);
		if (length == 0)  //成功输入检测
		{
			cout << "输入错误"<<endl;
			continue;
		}
		if (brackeCheck(str, length, S)) //括号检测
		{
			int j=PostfixExpression(S, str, length, str_new);
			float result=0;
			if(count(S, str_new, j,result))
				cout << "最终结果是:" << result<<endl;
			//检测没有错误后计算
		}
		else
		{
			cout << "输入错误"<<endl;
			continue;
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值