Chinaunix首页 | 论坛 | 博客
  • 博客访问: 236517
  • 博文数量: 65
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 417
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-05 15:12
个人简介

路漫漫其修远兮,吾将上下而求索

文章分类

全部博文(65)

文章存档

2016年(1)

2015年(12)

2014年(34)

2013年(16)

2012年(2)

我的朋友

分类: LINUX

2015-03-05 10:14:32

static.h

点击(此处)折叠或打开

  1. #ifndef STACK_H__
  2. #define STACK_H__

  3. #include <stdio.h>
  4. #include <stdlib.h>

  5. #define datatype unsigned int
  6. #define MAX 0x100

  7. struct stack
  8. {
  9.     datatype data[MAX];    //存储数据
  10.     int top;            //栈顶元素坐标
  11. };

  12. struct stack *create()        //创建栈并初始化
  13. {
  14.     struct stack *s = (struct stack *)malloc(sizeof(struct stack));
  15.     s->top = -1; 
  16.     
  17.     return s;   
  18. }

  19. int empty(struct stack *s)        //空栈判断
  20. {
  21.     return s->top == -1;
  22. }

  23. int full(struct stack *s)        //满栈判断
  24. {
  25.     return s->top == MAX -1;
  26. }

  27. void push(struct stack *s, datatype value)        //元素入栈
  28. {
  29.     if(!full(s))
  30.     {
  31.         s->data[++s->top] = value;
  32.     }
  33. }

  34. datatype pop(struct stack *s)            //出栈
  35. {
  36.     if(!empty(s))
  37.     {
  38.         return s->data[s->top--];
  39.     }
  40. }
  41. #endif


main.c

点击(此处)折叠或打开

  1. #include "stack.h"

  2. int calc(unsigned int a, char c, unsigned int b)    //四则运算,a,b为数字,c为运算符
  3. {
  4.     switch(c)
  5.     {
  6.         case '+':
  7.             return a + b;
  8.         case '-':
  9.             return a - b;
  10.         case '*':
  11.             return a * b;
  12.         case '/':
  13.             return a / b;
  14.     }
  15. }

  16. int main(void)
  17. {
  18.     struct stack *stack_num = create(); //创建存储数字栈
  19.     struct stack *stack_oper = create(); //创建运算符栈
  20.     
  21.     unsigned int num;        //数字-->num
  22.     char oper;                //运算符-->oper
  23.     scanf("%d", &num);
  24.     push(stack_num, num);    //数字入栈

  25.     while(scanf("%c", &oper)) //输入运算符
  26.     {
  27.         if((oper == '+' || oper == '-') && empty(stack_oper)) //第一次输入运算符是加号或减号
  28.         {
  29.             push(stack_oper, oper);
  30.             scanf("%d", &num);
  31.             push(stack_num, num);
  32.         }
  33.         else if((oper == '+' || oper == '-') && !empty(stack_oper)) //输入加减运算符,并且运算符栈还有加减运算符
  34.         {
  35.             num = pop(stack_num);
  36.             push(stack_num, calc(pop(stack_num), pop(stack_oper), num)); //计算加减运算,结果入数字栈(出栈顺序)
  37.             push(stack_oper, oper);                                        //运算符入栈
  38.             scanf("%d", &num);
  39.             push(stack_num, num);
  40.         }
  41.         else if((oper == '*' || oper == '/')) //输入乘除运算符,直接运算并且结果入栈
  42.         {
  43.             scanf("%d", &num);
  44.             push(stack_num, calc(pop(stack_num), oper, num));
  45.         }
  46.         else if(oper == '\n' && !empty(stack_oper))                    //算子以回车结束若运算符栈还有最后一个加减运算符
  47.         {
  48.             num = pop(stack_num);
  49.             push(stack_num, calc(pop(stack_num), pop(stack_oper), num));
  50.             break;
  51.         }
  52.         else if(empty(stack_oper))                        //运算符栈已空,直接跳出
  53.         {
  54.             break;
  55.         }
  56.     }
  57.     
  58.     printf("result = %d\n", pop(stack_num)); //输出最后结果
  59.     
  60.     free(stack_num);                                //回收内存资源
  61.     free(stack_oper);    
  62.     stack_num = NULL;
  63.     stack_oper = NULL;

  64.     return 0;
  65. }

阅读(485) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~