一、 简易计算器
***注意点***
操作数:
进栈
操作数:
1、进栈:(1)进栈 (2)优先级高 (3)栈顶是‘(’,同时表达式不是‘)’
2、出栈计算: (1)优先级不高于 (2)表达式‘)’,同时栈顶不是‘(’ (3)表达式‘\0’,同时栈不为空
3、出栈不计算: (1)表达式‘)’,同时栈顶是‘(’
主要程序:
头文件
#ifndef _LINKSTACK_H
#define _LINKSTACK_H
#define SUCCESS 10001
#define FAILURE 10002
#define TRUE 10003
#define FALSE 10004
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 StackClear(Stack **s);
int StackDestroy(Stack **s);
int Pop(Stack **s);
#endif
主程序
#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;
int num1=0,num2=0;
int tmp=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;
}
二、顺序栈和链栈
链栈:1、栈顶放在单链表的头部
2、链栈不需要头结点
3、链栈不存在栈满情况
分配空间时:顺序栈:两次,栈的信息,结点的信息(存储元素的模型)
链栈: 一次,栈的信息(还有一次是在进栈的时候,刚开始不需要,因为还没有结点)。
链栈的各种基本操作
#include "LinkStack.h"
#include <stdlib.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 || NULL == (*s))
{
return FAILURE;
}
Node *p = (Node *)malloc(sizeof(Node)*1);
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 || NULL == s->top)
{
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 "LinkStack.h"
#include <stdio.h>
int main()
{
int ret,i;
Stack *stack = NULL;
ret = StackInit(&stack);
if(ret == SUCCESS)
{
printf("Init Link Stack Success!\n");
}
else
{
printf("Init Failure!\n");
}
ret = StackEmpty(stack);
if(ret == TRUE)
{
printf("Stack is empty!\n");
}
else if(ret == FALSE)
{
printf("Stack is not empty!\n");
}
else
{
printf("Empty:error!\n");
}
for(i = 0; i < 10; i++)
{
ret = Push(&stack,i);
if(ret == FAILURE)
{
printf("Push %d Failure!\n");
}
else
{
printf("Push %d Success!\n");
}
}
ret = GetTop(stack);
if(ret == FAILURE)
{
printf("Get Top Failure!\n");
}
else
{
printf("Top is %d \n",ret);
}
for(i = 0; i < 5; i++)
{
ret = Pop(&stack);
if(FAILURE == ret)
{
printf("Pop Failure!\n");
}
else
{
printf("Pop %d Success!\n",ret);
}
}
ret = GetTop(stack);
if(ret == FAILURE)
{
printf("Get Top Failure!\n");
}
else
{
printf("Top is %d \n",ret);
}
ret = StackClear(&stack);
if(ret == SUCCESS)
{
printf("Clear Success!\n");
}
else
{
printf("Clear Failure!\n");
}
ret == StackDestroy(&stack);
if(ret == SUCCESS)
{
printf("Destroy Success!\n");
}
else
{
printf("Destroy Failure!\n");
}
for(i = 0; i < 10; i++)
{
ret = Push(&stack,i);
if(ret == FAILURE)
{
printf("Push %d Failure!\n");
}
else
{
printf("Push %d Success!\n");
}
}
return 0;
}