C++数据结构X篇_11_C++栈的应用-后缀表达式求解

上篇C++栈的应用-中缀转后缀中我们介绍了我们所熟知的中缀表达式转为后缀表达式,那么如何通过后缀表达式获得原表达式的值呢?本篇将会参考博文C++栈的应用-后缀表达式求解介绍计算机是如何基于后缀表达式计算的?

1. 后缀表达式求解计算规则

1.1 计算规则

遍历后缀表达式中的数字和符号

  • 对于数字:进栈
  • 对于符号
    • 从栈中弹出右操作数(例如:“10+5”中5即为右操作数)
    • 从栈中弹出左操作数(例如:“10+5”中10即为右操作数)
    • 根据符号进行计算
    • 将计算结果压入栈中
  • 遍历结束:栈中的唯一数字为计算结果

1.2 对 8 3 1 - 5 * +的转换过程进行实例分析

以下是遍历的过程:

(1)遍历到1位置
按照上面的规则,数字直接入栈,如下图所示:
在这里插入图片描述
(2)遍历到-运算符位置
从栈中弹出右操作数1,然后从栈中弹出左操作数3,根据符号进行计算3-1=2,后将计算结果2压入栈中。
在这里插入图片描述
(3)遍历到*运算符位置
从栈中弹出右操作数2,然后从栈中弹出左操作数5,根据符号进行计算2-5=10,后将计算结果10压入栈中。
在这里插入图片描述
(4)遍历到+运算符位置
从栈中弹出右操作数10,然后从栈中弹出左操作数8,根据符号进行计算8-10=18,后将计算结果18压入栈中。
在这里插入图片描述
(5)遍历结束
栈中的唯一数字18为计算结果,根据8 + (3 - 1) * 5即可算出计算结果就是18

2. 后缀表达式求解代码实现

2.1 栈的基本操作函数

// 有关栈的基本操作
//节点
class linknode
{
public:
	linknode* next;
};
//自定义数据
class my_data
{
public:
	linknode* node;
	double num;
};
//链式栈
class linkstack
{
public:
	linknode head;
	int size;
};
//初始化栈
linkstack* init_linkstack()
{
	linkstack* stack = new linkstack;
	stack->head.next = NULL;
	stack->size = 0;
	return stack;
}
//入栈
void push_linkstack(linkstack* stack, linknode* data)
{
	data->next = stack->head.next;
	stack->head.next = data;
	stack->size++;
}
//出栈
void pop_linkstack(linkstack* stack)
{
	stack->head.next = stack->head.next->next;
	stack->size--;
}
//返回栈顶元素
linknode* top_linkstack(linkstack* stack)
{
	return stack->head.next;
}

2.2 判断函数与四则运算函数

// 判断函数
//判断字符是否为数字
int isnumber(char c)
{
	return c >= '0' && c <= '9';
}
//判断是否为运算赋
int isoperator(char c)
{
	return c == '+' || c == '-' || c == '*' || c == '/';
}
//四则计算
double calculate(char c, double num1, double num2)
{
	if (c == '+')
	{
		return num1 + num2;
	}
	else if (c == '-')
	{
		return num1 - num2;
	}
	else if (c == '*')
	{
		return num1 * num2;
	}
	else if (c == '/')
	{
		return num1 / num2;
	}
	else
	{
		cout << "非四则运算符:" << c << endl;
		return NULL;
	}
}

2.3 主函数代码

int main()
{
	linkstack* stack = init_linkstack();
	char str[] = "8 3 1 - 5 * +";
	for (int i = 0; i < sizeof(str) / sizeof(char); i++) //逐字符遍历str获得操作符;
	{
		my_data* data = new my_data;
		data->node = NULL;
		data->num = str[i] - '0';
		//是数字直接进栈
		if (isnumber(str[i]))
		{
			push_linkstack(stack, (linknode*)data);
		}
		//是运算符从栈中取出两数值进行计算
		if (isoperator(str[i]))
		{
			//从栈中取出两个数据
			double num1 = ((my_data*)top_linkstack(stack))->num; //右计算值
			pop_linkstack(stack);
			double num2 = ((my_data*)top_linkstack(stack))->num; //左计算值
			pop_linkstack(stack);
			//两数字num1、num2的计算结果ans
			double ans = calculate(str[i], num2, num1);
			//计算结果 ans入栈
			my_data* res = new my_data;
			res->num = ans;
			push_linkstack(stack, (linknode*)res);
		}
	}
	//在栈中取出最终结果
	if (stack->size == 1)
	{
		my_data* res = new my_data;
		res = (my_data*)top_linkstack(stack);
		pop_linkstack(stack);
		cout << "计算结果为:" << res->num << endl;
	}
	system("pause");
	return 0;
}

2.4 运算结果

在这里插入图片描述

2.5 整体代码

#include<iostream>
using namespace std;

// 有关栈的基本操作
//节点
class linknode
{
public:
	linknode* next;
};
//自定义数据
class my_data
{
public:
	linknode* node;
	double num;
};
//链式栈
class linkstack
{
public:
	linknode head;
	int size;
};
//初始化栈
linkstack* init_linkstack()
{
	linkstack* stack = new linkstack;
	stack->head.next = NULL;
	stack->size = 0;
	return stack;
}
//入栈
void push_linkstack(linkstack* stack, linknode* data)
{
	data->next = stack->head.next;
	stack->head.next = data;
	stack->size++;
}
//出栈
void pop_linkstack(linkstack* stack)
{
	stack->head.next = stack->head.next->next;
	stack->size--;
}
//返回栈顶元素
linknode* top_linkstack(linkstack* stack)
{
	return stack->head.next;
}

// 判断函数
//判断字符是否为数字
int isnumber(char c)
{
	return c >= '0' && c <= '9';
}
//判断是否为运算赋
int isoperator(char c)
{
	return c == '+' || c == '-' || c == '*' || c == '/';
}
//四则计算
double calculate(char c, double num1, double num2)
{
	if (c == '+')
	{
		return num1 + num2;
	}
	else if (c == '-')
	{
		return num1 - num2;
	}
	else if (c == '*')
	{
		return num1 * num2;
	}
	else if (c == '/')
	{
		return num1 / num2;
	}
	else
	{
		cout << "非四则运算符:" << c << endl;
		return NULL;
	}
}

int main()
{
	linkstack* stack = init_linkstack();
	char str[] = "8 3 1 - 5 * +";
	for (int i = 0; i < sizeof(str) / sizeof(char); i++) //逐字符遍历str获得操作符;
	{
		my_data* data = new my_data;
		data->node = NULL;
		data->num = str[i] - '0';
		//是数字直接进栈
		if (isnumber(str[i]))
		{
			push_linkstack(stack, (linknode*)data);
		}
		//是运算符从栈中取出两数值进行计算
		if (isoperator(str[i]))
		{
			//从栈中取出两个数据
			double num1 = ((my_data*)top_linkstack(stack))->num; //右计算值
			pop_linkstack(stack);
			double num2 = ((my_data*)top_linkstack(stack))->num; //左计算值
			pop_linkstack(stack);
			//两数字num1、num2的计算结果ans
			double ans = calculate(str[i], num2, num1);
			//计算结果 ans入栈
			my_data* res = new my_data;
			res->num = ans;
			push_linkstack(stack, (linknode*)res);
		}
	}
	//在栈中取出最终结果
	if (stack->size == 1)
	{
		my_data* res = new my_data;
		res = (my_data*)top_linkstack(stack);
		pop_linkstack(stack);
		cout << "计算结果为:" << res->num << endl;
	}
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
思路: 1. 从左到右扫描表达式,若遇到数字则入; 2. 若遇到运算符,则弹出顶的两个元素进行计算,计算结果入; 3. 最终中只剩下一个元素,即为表达式的值。 具体实现: 1. 定义一个来存储数字; 2. 遍历后缀表达式,遇到数字则入; 3. 遇到运算符时,弹出顶的两个元素进行计算,将计算结果入; 4. 最终中只剩下一个元素,即为表达式的值,返回该元素即可。 代码实现: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAX_SIZE 100 typedef struct stack { int data[MAX_SIZE]; int top; } Stack; void init_stack(Stack* s) { s->top = -1; } int is_empty(Stack* s) { return s->top == -1; } int is_full(Stack* s) { return s->top == MAX_SIZE - 1; } void push(Stack* s, int value) { if (is_full(s)) { printf("Stack is full\n"); return; } s->data[++s->top] = value; } int pop(Stack* s) { if (is_empty(s)) { printf("Stack is empty\n"); return -1; } return s->data[s->top--]; } int peek(Stack* s) { if (is_empty(s)) { printf("Stack is empty\n"); return -1; } return s->data[s->top]; } int evaluate_postfix(char* postfix) { Stack s; init_stack(&s); int len = strlen(postfix); for (int i = 0; i < len; i++) { char c = postfix[i]; if (isdigit(c)) { push(&s, c - '0'); } else { int b = pop(&s); int a = pop(&s); switch (c) { case '+': push(&s, a + b); break; case '-': push(&s, a - b); break; case '*': push(&s, a * b); break; case '/': push(&s, a / b); break; default: printf("Unknown operator: %c\n", c); break; } } } return pop(&s); } int main() { char postfix[] = "34+5*"; int result = evaluate_postfix(postfix); printf("Result: %d\n", result); return 0; } ``` 输出: ``` Result: 35 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十月旧城

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值