利用链式栈来完成计算器

 头文件:

 #ifndef _LINKSTACK_H
  2 #define _LINKSTACK_H
  3 
  4 #include <stdlib.h>
  5 
  6 #define SUCCESS  10000
  7 #define FAILURE  10001
  8 #define TRUE     10002
  9 #define FALSE    10003
 10 
 11 typedef int ElemType;
 12 
 13 typedef struct node Node;
 14 
 15 struct node
 16 {
 17     ElemType data;
 18     struct node *next;
 19 };
 20 
 21 
 22 struct stack
 23 {
 24     Node *top;
 25     int count;
 26 };
 27 typedef struct stack Stack;
 28 
 29 int StackInit(Stack **s);
 30 int StackEmpty(Stack *s);
 31 int push(Stack **s,ElemType e);
 32 int GetTop(Stack *s);
 33 int pop(Stack **s);
 34 int Clear(Stack **s);
 35 int Destroy(Stack **s);
 36 
 37 #endif
 38 

 

 

 

 

 

 

 

 

 主函数:

4 int Priority(char ch)
  5 {
  6     switch(ch)
  7     {
  8         case'(':
  9             return 3;
 10         case '*':
 11         case '/':
 12             return 2;
 13         case '+':
 14         case '-':
 15             return 1;
 16         default:
 17             return 0;
 18     }
 19 }
 20 
 21 int main()
 22 {
 23     Stack *s_opt,*s_num;
 24     char opt[1024] = {0};
 25     int i = 0, tmp = 0, num1 = 0, num2 = 0;
 26 
 27     if(StackInit(&s_opt) != SUCCESS || StackInit(&s_num) != SUCCESS)
 28     {
 29         printf("Init Failure!\n");
 30     }
 31 
 32     printf("Please input :\n");
 33     scanf("%s",opt);
 34 
 35     while(opt[i] != '\0' || StackEmpty(s_opt) != TRUE)
 36     {
 37         if(opt[i] >= '0' && opt[i] <= '9')
 38         {
 39             tmp = tmp * 10 + opt[i] - '0';
 40             i++;
 41             if(opt[i] > '9' || opt[i] < '0')
 42             {
 43                 push(&s_num,tmp);
 44                 tmp = 0;
 45             }
 46         }
 47         else
 48         {
 49             if(opt[i] == ')' && GetTop(s_opt) == '(')
 50             {
 51                 pop(&s_opt);
 52                 i++;
 53                 continue;
 54             }
 55 
 56             if(StackEmpty(s_opt) == TRUE || (Priority(opt[i]) > Priority(GetTop(s_opt)))
 57                || (GetTop(s_opt) == '(' && opt[i] != ')'))
 58             {
 59                 push(&s_opt,opt[i]);
 60                 i++;
 61                 continue;
 62             }
 63 
 64             if((opt[i] == '\0' && StackEmpty(s_opt) != TRUE) ||
 65                (opt[i] == ')' && GetTop(s_opt) != '(')  ||
 66                (Priority(opt[i]) <= Priority(GetTop(s_opt))))
 67             {
 68                 switch(pop(&s_opt))
 69                 {
 70                     case '+':
 71                         num1 = pop(&s_num);
 72                         num2 = pop(&s_num);
 73                         push(&s_num,(num1 + num2));
 74                         break;
 75                     case '-':
 76                         num1 = pop(&s_num);
 77                         num2 = pop(&s_num);
 78                         push(&s_num,(num1 - num2));
 79                         break;
 80                     case '*':
 81                         num1 = pop(&s_num);
 82                         num2 = pop(&s_num);
 83                         push(&s_num,(num1 * num2));
 84                         break;
 85                     case '/':
 86                         num1 = pop(&s_num);
 87                         num2 = pop(&s_num);
 88                         push(&s_num,(num1 / num2));
 89                         break;
 90                 }
 91             }
 92         }
 93     }
 94     printf("%d\n",GetTop(s_num));
 95     return 0;
 96 }
                     

 

自定义函数:

  1 #include "LinkStack.h"
  2 
  3 int StackInit(Stack **s)
  4 {
  5     if(NULL == s)
  6     {
  7         return FAILURE;
  8     }
  9     (*s) = (Stack *)malloc(sizeof(Stack) * 1);
 10     if(NULL == (*s))
 11     {
 12         return FAILURE;
 13     }
 14 
 15     (*s)->top = NULL;
 16     (*s)->count = 0;
 17 
 18     return SUCCESS;
 19 
 20 }
 21 
 22 int StackEmpty(Stack *s)
 23 {
 24     if(NULL == s)
 25     {
 26         return FAILURE;
 27     }
 28 
 29     return (s->top == NULL) ? TRUE : FALSE;
 30 }
 31 
 32 int push(Stack **s,ElemType e)
 33 {
 34     if(NULL == s || (*s) == NULL)
 35     {
 36         return FAILURE;
 37     }
 38 
 39     Node *p = (Node *)malloc(sizeof(Node));
 40     if(NULL == p)
 41     {
 42         return FAILURE;
 43     }
 44 
 45     p->data = e;
 46     p->next = (*s)->top;
 47     (*s)->top = p;
 48     (*s)->count++;
 49 
 50     return SUCCESS;
 51 }
 52 
 53 int GetTop(Stack *s)
 54 {
 55     if(NULL == s)
 56     {
 57         return FAILURE;
 58     }
 59     return s->top->data;
 60 }
 61 
 62 int pop(Stack **s)
 63 {
 64     if(NULL == s || NULL == (*s))
 65     {
 66         return FAILURE;
 67     }
 68 
 69     Node *p = (*s)->top;
 70     ElemType e = (*s)->top->data;
 71     (*s)->top = (*s)->top->next;
 72     (*s)->count--;
 73     free(p);
 74 
 75     return e;
 76 }
 77 
 78 int Clear(Stack **s)
 79 {
 80     if(NULL == s || NULL == (*s))
 81     {
 82         return FAILURE;
 83     }
 84 
 85     Node *p = (*s)->top;
 86 
 87     while(p)
 88     {
 89         (*s)->top = p->next;
 90         free(p);
 91         p = (*s)->top;
 92         (*s)->count--;
 93     }
 94     return SUCCESS;
 95
 96
 97    int Destroy(Stack **s)
 98 {
 99     if(NULL == s || NULL == (*s))
100     {
101         return FAILURE;
102     }
103 
104     free(*s);
105     (*s) = NULL;
106 
107     return SUCCESS;
108 }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值