单栈实现逆波兰计算器

//头文件
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>


#define ERROR 0
#define OK 1
#define YES 1
#define NO 0


typedef int Status;


typedef struct StackNode
{
	char data;
	double num;
	struct StackNode *next;
}StackNode;
typedef struct LinkStack
{
	StackNode *top;
	int count;
}LinkStack;


LinkStack* link;




//输入链表并转换为后缀表达式
void GetInput(char* rpn,char* copy);
//将后缀表达式转换为结果
double GetAnswer(char *rpn);
//判断是否为运算符,并返回运算符等级
int IsOperator(char s);
//判断是什么运算符,并返回
int WhatOperator(char s);
//判断栈空
Status StackEmpty(LinkStack *S);
//链栈初始化
void IniStack(void);
//出栈字符
Status PopChar(LinkStack *S,char *e);
//进栈字符
Status PushChar(LinkStack *S, char e);
//出栈数字
Status PopNum(LinkStack *S, double *e);
//进栈数字
Status PushNum(LinkStack *S, double e);
//实现文件
#include "rpn.h"


//输入链表并转换为后缀表达式
void GetInput(char* rpn, char* copy)
{
	int i, j = 0, n,button = 0;
	char *temp = (char*)malloc(sizeof(char));
	n = strlen(rpn);
	for (i = 0; i < n; i++)
	{
		if (IsOperator(rpn[i]))
		{
			if (IsOperator(rpn[i])== -1)
			{
				button++;
				PushChar(link, rpn[i]);
			}
			else if (IsOperator(rpn[i]) == 3)
			{
				while (link->top->data != '(')
				{
					PopChar(link, &copy[j]);
					j++;
					copy[j] = ' ';
					j++;
				}
				PopChar(link, temp);
				button--;
			}
			else if (IsOperator(rpn[i]) == 2)
			{
				if (!StackEmpty(link))
				{
					while (IsOperator(rpn[i]) <= IsOperator(link->top->data))
					{
						PopChar(link, &copy[j]);
						j++;
						copy[j] = ' ';
						j++;
						if (StackEmpty(link))
						{
							break;
						}
					}
				}
				PushChar(link, rpn[i]);
			}
			else if (IsOperator(rpn[i]) == 1)
			{
				if (!StackEmpty(link))
				{
					while ((IsOperator(rpn[i]) <= IsOperator(link->top->data)))
					{
						PopChar(link, &copy[j]);
						j++;
						copy[j] = ' ';
						j++;
						if (StackEmpty(link))
						{
							break;
						}
					}
				}
				PushChar(link, rpn[i]);
			}
		}
		else
		{
			copy[j] = rpn[i];
			j++;
			if (IsOperator(rpn[i + 1]))
			{
				copy[j] = ' ';
				j++;
			}
		}
	}
	if (!IsOperator(rpn[i]))
	{
		copy[j] = ' ';
		j++;
	}
	while (link->count != 0)
	{
		PopChar(link, &copy[j]);
		j++;
		copy[j] = ' ';
		j++;
	}
}
//将后缀表达式转换为结果
double GetAnswer(char *copy)
{
	int i = 0, n, j;
	double *temp_a, *temp_b, temp_total;
	temp_a = (double *)malloc(sizeof(double));
	temp_b = (double *)malloc(sizeof(double));
	char backup[512];
	n = strlen(copy);
	while (i < n)
	{
		if (IsOperator(copy[i]))
		{
			PopNum(link, temp_a);
			PopNum(link, temp_b);
			switch (WhatOperator(copy[i]))
			{
			case 1:
				temp_total = *temp_b + *temp_a;
				PushNum(link, temp_total);
				break;
			case 2:
				temp_total = *temp_b - *temp_a;
				PushNum(link, temp_total);
				break;
			case 3:
				temp_total = *temp_b * *temp_a;
				PushNum(link, temp_total);
				break;
			case 4:
				temp_total = *temp_b / *temp_a;
				PushNum(link, temp_total);
				break;
			default:
				break;
			}
			i++;
		}
		if (!IsOperator(copy[i]) && copy[i] != ' ')
		{
			j = 0;
			memset(backup, 0, 512);
			while (copy[i] != ' ')
			{
				backup[j] = copy[i];
				j++;
				i++;
			}
			PushNum(link, atof(backup));
		}
		if (copy[i] == ' ')
		{
			i++;
		}
	}
	return link->top->num;
}
//判断是否为运算符,并返回运算符等级
int IsOperator(char s)
{
	int result;
	if (s == '+' || s == '-')
	{
		result = 1;
	}
	else if (s == '*' || s == '/')
	{
		result = 2;
	}
	else if (s == '(')
	{
		result = -1;
	}
	else if (s == ')')
	{
		result = 3;
	}
	else
	{
		result = 0;
	}
	return result;
}
//判断是什么运算符,并返回
int WhatOperator(char s)
{
	if (s == '+')
	{
		return 1;
	}
	else if (s == '-')
	{
		return 2;
	}
	else if (s == '*')
	{
		return 3;
	}
	else if (s == '/')
	{
		return 4;
	}
	else
	{
		return 0;
	}
}
//判断栈空
Status StackEmpty(LinkStack *S)
{
	if (S->count == 0)
	{
		return YES;
	}
	else
	{
		return NO;
	}
}
//链栈初始化
void IniStack(void)
{
	link = (LinkStack*)malloc(sizeof(LinkStack));
	link->count = 0;
	link->top = NULL;
}
//出栈字符
Status PopChar(LinkStack *S,char *e)
{
	StackNode *p;
	if (StackEmpty(S))
	{
		return ERROR;
	}
	*e = S->top->data;
	p = S->top;
	S->top = S->top->next;
	free(p);
	p = NULL;
	S->count--;
	return OK;
}
//进栈字符
Status PushChar(LinkStack *S, char e)
{
	StackNode *s;
	s = (StackNode*)malloc(sizeof(StackNode));
	s->data = e;
	s->next = S->top;
	S->top = s;
	S->count++;
	return OK;
}
//进栈数字
Status PushNum(LinkStack *S, double e)
{
	StackNode *s;
	s = (StackNode*)malloc(sizeof(StackNode));
	s->num = e;
	s->next = S->top;
	S->top = s;
	S->count++;
	return OK;
}
//出栈数字
Status PopNum(LinkStack *S, double *e)
{
	StackNode *p;
	if (StackEmpty(S))
	{
		return ERROR;
	}
	*e = S->top->num;
	p = S->top;
	S->top = S->top->next;
	free(p);
	p = NULL;
	S->count--;
	return OK;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值