正常的表达式 逆波兰表达式
a+b --->a,b,+
a+(b-c) ---> a,b,c,-,+
a+(b-c)*d ---> a,b,c,-,d,*,+
a+d*(b-c)--->a,d,b,c,-,*,+
a=1+3 ---> a,1,3,+,=
它的优势在于只用两种简单操作,入栈和出栈就可以搞定任何普通表达式的运算。其运算方式如下:
如果当前字符为变量或者为数字,则压栈,如果是运算符,则将栈顶两个元素弹出作相应运算,结果再入栈,最后当表达式扫描完后,栈里的就是结果。
在表达式中的转换规则
操作数 :进栈
操作符 :1)进栈:
空栈
优先级高
栈顶是‘( ’同时表达式不是‘ )’
2)出栈并计算:
表达式符号的优先级不高于栈顶符号
表达式为‘ )’同时栈顶不为‘( ’
表达式‘\0’同时栈不为空
3)出栈但不计算:
表达式为‘ )’同时栈顶为‘( ’
头文件:
#ifndef _LINKSTACK_H
#define _LINKSTACK_H
#include <stdlib.h>
#define SUCCESS 10000
#define FAILURE 10001
#define TRUE 10002
#define FALSE 10003
typedef int ElemType;
//结点的信息
struct node
{
ElemType data; //数据域
struct node *next; //指针域
};
typedef struct node Node;
//栈的信息
struct stack
{
Node *top; //头指针
int count; //结点个数
};
typedef struct stack Stack;
int StackInit(Stack **s);
int StackEmpty(Stack *s);
int push(Stack **s, ElemType e);
int GetTop(Stack *s);
int pop(Stack **s);
int StackClear(Stack **s);
int StackDestroy(Stack **s);
#endif
子函数:
#include "LinkStack.h"
int StackInit(Stack **s)
{
if (NULL == s)
{
return FAILURE;
}
(*s) = (Stack *)malloc(sizeof(Stack) * 1);
if (NULL == (*s))
{
return FAILURE;
}
(*s)->top = NULL;
(*s)->count = 0;
return SUCCESS;
}
int StackEmpty(Stack *s)
{
if (NULL == s)
{
return FAILURE;
}
return (s->top == NULL) ? TRUE : FALSE;
}
int push(Stack **s, ElemType e)
{
if (NULL == s || (*s) == NULL)
{
return FAILURE;
}
Node *p = (Node *)malloc(sizeof(Node)); //给结点分配空间
if (NULL == p)
{
return FAILURE;
}
p->data = e; //数据域
p->next = (*s)->top;
(*s)->top = p;
(*s)->count++;
return SUCCESS;
}
int GetTop(Stack *s)
{
if (NULL == s || s->top == NULL)
{
return FAILURE;
}
return s->top->data;
}
int pop(Stack **s)
{
if (NULL == s || NULL == *s)
{
return FAILURE;
}
Node *p = (*s)->top;
ElemType e = (*s)->top->data;
(*s)->top = (*s)->top->next;
(*s)->count--;
free(p);
return e;
}
int StackClear(Stack **s)
{
if (NULL == s || NULL == *s)
{
return FAILURE;
}
Node *p = (*s)->top;
while (p)
{
(*s)->top = p->next;
free(p);
p = (*s)->top;
(*s)->count--;
}
return SUCCESS;
}
int StackDestroy(Stack **s)
{
if (NULL == s || NULL == *s)
{
return FAILURE;
}
free(*s);
(*s) = NULL;
return SUCCESS;
}
主函数:
#include <stdio.h>
#include "LinkStack.h"
int Priority(char ch)
{
switch(ch)
{
case '(':
return 3;
case '*':
case '/':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
}
}
int main()
{
Stack *s_opt, *s_num;
char opt[1024] = {0}; //存放表达式
int i = 0, tmp = 0, num1 = 0, num2 = 0;
if (StackInit(&s_opt) != SUCCESS || StackInit(&s_num) != SUCCESS)
{
printf("Init Failure!\n");
}
printf("Please input : \n");
scanf("%s", opt);
while (opt[i] != '\0' || StackEmpty(s_opt) != TRUE) //表达式没结束 或者 操作符栈不为空
{
if (opt[i] >= '0' && opt[i] <= '9') //操作数
{
tmp = tmp * 10 + opt[i] - '0';
i++;
if (opt[i] > '9' || opt[i] < '0') //操作符
{
push(&s_num, tmp);
tmp = 0;
}
}
else //操作符
{
if (opt[i] == ')' && GetTop(s_opt) == '(') //出栈不计算
{
pop(&s_opt);
i++;
continue;
}
if (StackEmpty(s_opt) == TRUE || (Priority(opt[i]) > Priority(GetTop(s_opt)))
|| (GetTop(s_opt) == '(' && opt[i] != ')')) //进栈
{
push(&s_opt, opt[i]);
i++;
continue;
}
if ((opt[i] == '\0' && StackEmpty(s_opt) != TRUE) ||
(opt[i] == ')' && GetTop(s_opt) != '(') ||
(Priority(opt[i]) <= Priority(GetTop(s_opt)))) //出栈计算
{
switch(pop(&s_opt))
{
case '+':
num1 = pop(&s_num);
num2 = pop(&s_num);
push(&s_num, (num1 + num2));
break;
case '-':
num1 = pop(&s_num);
num2 = pop(&s_num);
push(&s_num, (num2 - num1));
break;
case '*':
num1 = pop(&s_num);
num2 = pop(&s_num);
push(&s_num, (num1 * num2));
break;
case '/':
num1 = pop(&s_num);
num2 = pop(&s_num);
push(&s_num, (num2 / num1));
break;
}
}
}
}
printf("%d\n", GetTop(s_num));
return 0;
}