//头文件
#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, ©[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, ©[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, ©[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, ©[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;
}