栈的应用---后缀表达式c代码实现

后缀表达式计算结果规则:
	从左到右遍历表达式的每个数字和符号,遍历到数字就进栈,遍历到符号,就将处于栈顶的两个数字出栈,
	进行运算,运算结果进栈,一直到最终获得结果

仍然用的栈的基本数据结构
test.hpp

#ifndef TEST_H_
#define TEST_H_
#include"malloc.h"

#define MAXSIZE 100
typedef float SElemType;

typedef struct
{
	SElemType data[MAXSIZE];
	int top;
}SqStack;

//InitStack(*S):初始化操作,建立一个空栈S
SqStack * initStack();
//DestroyStack(*S):若栈存在,则销毁他
void destroyStack(SqStack * S);
//ClearStack(*S):将栈清空
void clearStack(SqStack * S);
//StackEmpty(S):若栈为空,返回true。否则返回false
bool stackEmpty(const SqStack * S);
//StackLength(S):返回栈S的元素个数
int stackLength(const SqStack S);
//GetTop(S,*e);若栈存在且非空,用e返回S的栈顶元素
void getTop(const SqStack S, SElemType * e);
//Push(*S,e):若栈S存在,插入新元素e到栈S中并成为栈顶元素
void push(SqStack * S, const SElemType e);
//Pop(*S,*e):删除栈S存在中栈顶的元素,并用e返回其值
void pop(SqStack * S, SElemType & e);
#endif // !TEST_H_

#pragma once

test.cpp

#include"test.h"

//InitStack(*S):初始化操作,建立一个空栈S
SqStack * initStack() {

	SqStack *S = (SqStack *)malloc(sizeof(SqStack));
	S->top = 0;
	return S;
}

//DestroyStack(*S):若栈存在,则销毁他
void destroyStack(SqStack * S) {

	if (S->top > 0)
		free(S);

}

//ClearStack(*S):将栈清空
void clearStack(SqStack * S) {

	if (S->top > 0)
	{
		for (size_t i = S->top; i >= 0; i--)
		{
			S->data[i] = 0;
		}
		S->top = 0;
	}
}

//StackEmpty(S):若栈为空,返回true。否则返回false
bool stackEmpty(const SqStack * S) {

	if (S->top > 0)
		return false;
	else
		return true;
}

//StackLength(S):返回栈S的元素个数
int stackLength(const SqStack S) {

	return S.top;

}

//GetTop(S,*e);若栈存在且非空,用e返回S的栈顶元素
void getTop(const SqStack S, SElemType * e) {

	if (S.top > 0)
		*e = S.data[S.top];

}

//Push(*S,e):若栈S存在,插入新元素e到栈S中并成为栈顶元素
void push(SqStack * S, const SElemType e) {

	if (S->top < MAXSIZE)
	{
		S->data[S->top] = e;
		++S->top;
	}

}

//Pop(*S,*e):删除栈S存在中栈顶的元素,并用e返回其值
void pop(SqStack * S, SElemType & e) {

	e = S->data[--S->top];
}

main.cpp

/*
	后缀表达式计算结果规则:
	从左到右遍历表达式的每个数字和符号,遍历到数字就进栈,遍历到符号,就将处于栈顶的两个数字出栈,
	进行运算,运算结果进栈,一直到最终获得结果
*/
#include<iostream>
#include<cstdio>
#include"test.h"

int main()
{
	char s;
	char str[10];
	float tempFirst = 0.0f;
	float tempSecond = 0.0f;
	float result = 0.0f;
	SqStack * S = initStack();
	scanf("%c", &s);
	int i = 0;
	while ( s != '#')
	{
		while (isdigit(s) || s == '.')
		{
			str[i++] = s;
			str[i] = '\0';
			if (i >= 10)
			{
				std::cerr << "数据过大\n";
				return -1;
			}
			scanf("%c", &s);
			if (s == ' ')
			{
				double d = atof(str);
				push(S, d);
				i = 0;
				break;
			}
		}
		switch (s)
		{
		case '+':
			pop(S, tempSecond);
			pop(S, tempFirst);
			push(S, tempFirst + tempSecond);
			break;
		case '-':
			pop(S, tempSecond);
			pop(S, tempFirst);
			push(S, tempFirst - tempSecond);
			break;
		case '*':
			pop(S, tempSecond);
			pop(S, tempFirst);
			push(S, tempFirst * tempSecond);
			break;
		case '/':
			pop(S, tempSecond);
			pop(S, tempFirst);
			push(S, tempFirst / tempSecond);
			break;
		}

		std::cin >> s;

	}
	
	pop(S, result);
	std::cout << result << std::endl;
	return 0;
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值