Chinaunix首页 | 论坛 | 博客
  • 博客访问: 210411
  • 博文数量: 87
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 798
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-14 14:54
文章分类

全部博文(87)

文章存档

2015年(87)

我的朋友

分类: C/C++

2015-08-26 15:15:40


参考博客:http://blog.sina.com.cn/s/blog_67af05770100hux8.html
仍有错误!!!!!

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <conio.h>

  5. #define OK 1
  6. #define ERROR 0
  7. #define OVERFLOW 0
  8. #define TRUE 1
  9. #define FALSE 0
  10. #define STACK_INIT_SIZE 300 //存储空间初始化分配量
  11. #define STACKINCREMENT 10 //存储空间分配增量

  12. typedef int Status; //函数返回状态
  13. typedef int ElemType; //元素类型

  14. typedef struct{//顺序栈结构定义
  15.     ElemType *base;
  16.     ElemType *top; //栈底指针
  17.     int stacksize;
  18. }Stack;

  19. Status InitStack(Stack *S);
  20. Status GetTop(Stack *S);
  21. Status Push(Stack *S, ElemType e);
  22. Status Pop(Stack *S, ElemType e);
  23. Status In(char c);
  24. char Precede(char a, char b);
  25. Status Operate(ElemType a, char c, ElemType b);
  26. Status EvalueExpression();

  27. Status InitStack(Stack *S)
  28. { //构造一个空栈S
  29.     S->base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
  30.     if(!S->base)
  31.     {
  32.         printf("分配内存失败!\n");
  33.         exit (ERROR);
  34.     }
  35.     S->top=S->base;
  36.     S->stacksize=STACK_INIT_SIZE;

  37.     return OK;
  38. }

  39. Status GetTop(Stack *S)
  40. { //若栈不空,用e返回S的栈顶元素,并返回OK;否则返回ERROR
  41.     if(S->top == S->base)
  42.         return ERROR;
  43.     printf("*(S->top-1))=%d\n", *(S->top-1));
  44.     return *(S->top-1);
  45. }

  46. Status Push(Stack *S, ElemType e)
  47. { //插入元素e为新的栈顶元素
  48.     printf("入栈\n");

  49.     if(S->top-S->base>=S->stacksize)
  50.     {
  51.         printf("栈满\n");
  52.         printf("%d\n", S->stacksize);
  53.         S->base = (ElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(ElemType));
  54.         if(!S->base)
  55.             exit (OVERFLOW);
  56.         S->top=S->base+S->stacksize;
  57.         S->stacksize+=STACKINCREMENT;
  58.     }
  59.     *S->top++=e;    
  60.     return OK;
  61. }

  62. Status Pop(Stack *S, ElemType e)
  63. { //若栈不空,则删除,用e返回其值,并返回OK;否则返回ERROR
  64.     if(S->top==S->base)
  65.         return ERROR;
  66.     e=*--S->top;
  67.     return OK;
  68. }

  69. Status In(char c)
  70. { //判别c是否为运算符
  71.     if (c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')'||c=='#')
  72.         return OK;
  73.     else
  74.         return ERROR;
  75. }

  76. char Precede(char a,char b)
  77. { //算符间优先关系
  78.     switch(a)
  79.     {
  80.     case '+':
  81.         switch (b)
  82.         {
  83.         case '+': return '>'; break;
  84.         case '-': return '>'; break;
  85.         case '*': return '<'; break;
  86.         case '/': return '<'; break;
  87.         case '(': return '<'; break;
  88.         case ')': return '>'; break;
  89.         case '#': return '>'; break;
  90.         default: break;
  91.         }
  92.         break;
  93.     case '-':
  94.         switch (b)
  95.         {
  96.         case '+': return '>'; break;
  97.         case '-': return '>'; break;
  98.         case '*': return '<'; break;
  99.         case '/': return '<'; break;
  100.         case '(': return '<'; break;
  101.         case ')': return '>'; break;
  102.         case '#': return '>'; break;
  103.         default: break;
  104.         }
  105.          break;
  106.     case '*':
  107.         switch (b)
  108.         {
  109.         case '+': return '>'; break;
  110.         case '-': return '>'; break;
  111.         case '*': return '>'; break;
  112.         case '/': return '>'; break;
  113.         case '(': return '<'; break;
  114.         case ')': return '>'; break;
  115.         case '#': return '>'; break;
  116.         default: break;
  117.         }
  118.         break;
  119.     case '/':
  120.         switch (b)
  121.         {
  122.         case '+': return '>'; break;
  123.         case '-': return '>'; break;
  124.         case '*': return '>'; break;
  125.         case '/': return '>'; break;
  126.         case '(': return '<'; break;
  127.         case ')': return '>'; break;
  128.         case '#': return '>'; break;
  129.         default: break;
  130.         }
  131.         break;
  132.     case '(':
  133.         switch (b)
  134.         {
  135.         case '+': return '<'; break;
  136.         case '-': return '<'; break;
  137.         case '*': return '<'; break;
  138.         case '/': return '<'; break;
  139.         case '(': return '<'; break;
  140.         case ')': return '='; break;
  141.         default: break;
  142.         }
  143.         break;
  144.     case ')':
  145.         switch (b)
  146.         {
  147.         case '+': return '>'; break;
  148.         case '-': return '>'; break;
  149.         case '*': return '>'; break;
  150.         case '/': return '>'; break;
  151.         case ')': return '>'; break;
  152.         case '#': return '>'; break;
  153.         default: break;
  154.         }
  155.         break;
  156.     case '#':
  157.         switch (b)
  158.         {
  159.         case '+': return '<'; break;
  160.         case '-': return '<'; break;
  161.         case '*': return '<'; break;
  162.         case '/': return '<'; break;
  163.         case '(': return '<'; break;
  164.         case '#': return '='; break;
  165.         default: break;
  166.         }
  167.         break;
  168.     default: break;
  169.     }//switch(a)
  170. }

  171. Status Operate(int a,char c,int b)
  172. { //二元运算
  173.     switch (c)
  174.     {
  175.     case '+': return a+b; break;
  176.     case '-': return a-b; break;
  177.     case '*': return a*b; break;
  178.     case '/':
  179.         if(b==0)
  180.         {
  181.             printf("(提示:存在除数为零错误)\n");
  182.             return ERROR;
  183.         } //除数不能为零
  184.         else
  185.             return a/b;
  186.         break;
  187.     }
  188. }

  189. Status EvalueExpression()
  190. {
  191.     char c;
  192.     int a, b, x, theta;
  193.     Stack OPTR, OPND;
  194.     
  195.     InitStack(&OPTR);
  196.     Push(&OPTR, '#');
  197.     InitStack(&OPND);
  198.     printf("输入一个字符\n");
  199.     c = getchar();
  200.     while (getchar() != '\n')
  201.         continue;
  202.     while( || GetTop(&OPTR)!='#')
  203.     {
  204.         printf("c=%c\n", c);
  205.         if (!In(c))
  206.         {
  207.             printf("输入一个字符\n");

  208.             printf("数据\n");
  209.             Push(&OPND, c);
  210.             c = getchar();
  211.             while (getchar() != '\n')
  212.                 continue;
  213.         }
  214.         else
  215.         {
  216.             printf("运算符\n");

  217.             switch (Precede(GetTop(&OPTR), c))
  218.             {
  219.             case '<' : // 栈顶元素优先权低
  220.                 printf("压入运算符栈\n");
  221.                 Push(&OPTR, c);
  222.                 c = getchar();
  223.                 while (getchar() != '\n')
  224.                     continue;

  225.                 break;
  226.             case '=' : //脱括号并接收下一个字符
  227.                 Pop(&OPTR, x);
  228.                 c = getchar();
  229.                 while (getchar() != '\n')
  230.                     continue;
  231.                 break;
  232.             case '>' : //退栈并将结果入栈
  233.                 Pop(&OPTR, theta);
  234.                 Pop(&OPND, b);
  235.                 Pop(&OPND, a);
  236.                 Push(&OPND, Operate(a, theta, b));
  237.                 break;
  238.             }//switch
  239.         }//else
  240.     }//while
  241.     printf("%d\n", GetTop(&OPND));
  242.     return 0;
  243. }

  244. int main()
  245. {
  246.     EvalueExpression();

  247.     return 0;
  248. }

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