鱼C论坛

 找回密码
 立即注册
查看: 1034|回复: 0

数据结构栈的应用

[复制链接]
发表于 2020-10-30 14:50:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 cddyy2366 于 2020-11-10 20:58 编辑

大佬们我现在在用栈做一个实现四则运算(加减乘除)的程序,但是现在不论我输什么算式进去他的结果都是随机的数字,我感觉是while(equation!='#' || last!='#')这个while循环有问题但是又不知道具体是什么问题,可以麻烦大家帮我看一下该怎么改吗,谢谢各位了!
源代码如下:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define STACK_INIT_SIZE 100
  4. #define STACKINCREMENT 10
  5. #define OK 1
  6. #define ERROR 0
  7. #define OVERFLOW -2

  8. typedef char SElemType;
  9. typedef int Status;
  10. typedef char OperandType;//运算类型
  11. typedef struct{
  12.         SElemType *base;
  13.         SElemType *top;
  14.         int stacksize;
  15. }SqStack;

  16. Status InitStack(SqStack *S)//构造空栈
  17. {
  18.         S->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
  19.         if(!S->base)exit(OVERFLOW);
  20.         S->top=S->base;
  21.         S->stacksize=STACKINCREMENT;
  22.         return OK;
  23. }

  24. Status Push(SqStack *S,SElemType e)//入栈
  25. {
  26.         if(S->top-S->base >= S->stacksize)
  27.         {
  28.                 S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType));
  29.                 if(!S->base)exit(OVERFLOW);
  30.                 S->top=S->base+S->stacksize;
  31.                 S->stacksize+=STACKINCREMENT;
  32.         }
  33.         *S->top++=e;
  34.         return OK;
  35. }

  36. Status Pop(SqStack *S,SElemType *e)//出栈
  37. {
  38.         if(S->top==S->base)return ERROR;
  39.         *e=*--S->top;
  40.         return OK;
  41. }

  42. SElemType GetTop(SqStack *S,SElemType *e)//得到栈顶元素
  43. {
  44.         if(S->top==S->base)
  45.         {
  46.                 return ERROR;
  47.         }
  48.         *e=*(S->top-1);
  49.         return OK;
  50. }

  51. OperandType Easyoperate(OperandType a,OperandType b,OperandType theta)//计算简单的运算
  52. {
  53.         OperandType answer;
  54.         switch(theta)
  55.         {
  56.                 case '+':
  57.                         answer=a+b;
  58.                         return answer;break;
  59.                 case '-':
  60.                         answer=a-b;
  61.                         return answer;break;
  62.                 case '*':
  63.                         answer=a*b;
  64.                         return answer;break;
  65.                 case '/':
  66.                         answer=a/b;
  67.                         return answer;break;
  68.         }
  69. }

  70. char Compare(char a,char b)//判断算符优先级
  71. {
  72.         int i,j;
  73.         char prior[7][7]=//显示算符优先关系的二维数组
  74.         {
  75.                           /* '+' '-' '*' '/' '(' ')' '#' */
  76.                 /* '+'*/ '>','>','<','<','<','>','>',
  77.                 /* '-'*/ '>','>','<','<','<','>','>',
  78.                 /* '*'*/ '>','>','>','>','<','>','>',
  79.                 /* '/'*/ '>','>','>','>','<','>','>',
  80.                 /* '('*/ '<','<','<','<','<','=',' ',
  81.                 /* ')'*/ '>','>','>','>',' ','>','>',
  82.                 /* '#'*/ '<','<','<','<','<',' ','=',
  83.         };
  84.        
  85.         switch(a)
  86.         {
  87.                 case '+':i=0;break;
  88.                 case '-':i=1;break;
  89.                 case '*':i=2;break;
  90.                 case '/':i=3;break;
  91.                 case '(':i=4;break;
  92.                 case ')':i=5;break;
  93.                 case '#':i=6;break;
  94.         }
  95.         switch(b)
  96.         {
  97.                 case '+':j=0;break;
  98.                 case '-':j=1;break;
  99.                 case '*':j=2;break;
  100.                 case '/':j=3;break;
  101.                 case '(':j=4;break;
  102.                 case ')':j=5;break;
  103.                 case '#':j=6;break;
  104.         }
  105.        
  106.         return prior[i][j];
  107. }

  108. Status In(OperandType c)//判断是数字还是运算符
  109. {
  110.         switch(c)
  111.         {
  112.                 case '+':return OK;
  113.                 case '-':return OK;
  114.                 case '*':return OK;
  115.                 case '/':return OK;
  116.                 case '(':return OK;
  117.                 case ')':return OK;
  118.                 case '#':return OK;
  119.                 default:return ERROR;
  120.         }
  121. }

  122. OperandType Operate()
  123. {
  124.         SqStack OPTR,OPND;
  125.         OperandType a,b,last,theta,num;
  126.         char OP[6]={'+','-','*','/','(',')'};
  127.         InitStack(&OPTR);
  128.         Push(&OPTR,'#');
  129.         InitStack(&OPND);
  130.        
  131.         printf("输入表达式:");
  132.         char equation;
  133.         equation=getchar();
  134.         GetTop(&OPTR,&last);
  135.         while(equation!='#' || last!='#')//输入算式,以#结尾
  136.         {
  137.                 if(!In(equation))//如果不是运算符,把char类型的数字转为int型然后进OPND栈
  138.                 {
  139.                         int data[10];
  140.                         int i=0;
  141.                         while(!In(equation))
  142.                         {
  143.                                 data[i]=equation-'0';
  144.                                 i++;
  145.                                 equation=getchar();
  146.                         }
  147.                         for(int j=0;j<i;j++)
  148.                         {
  149.                                 num=num*10+data[j];
  150.                         }
  151.                         Push(&OPND,num);
  152.                 }
  153.                 else
  154.                 {
  155.                         switch(Compare(last,equation))
  156.                         {
  157.                                 case '<':
  158.                                         Push(&OPTR,equation);
  159.                                         equation=getchar();
  160.                                         break;
  161.                                 case '=':
  162.                                         Pop(&OPTR,&last);
  163.                                         equation=getchar();
  164.                                         break;
  165.                                 case '>':
  166.                                         Pop(&OPTR,&theta);
  167.                                         Pop(&OPND,&b);
  168.                                         Pop(&OPND,&a);
  169.                                         Push(&OPND,Easyoperate(a,b,theta));
  170.                                         break;
  171.                         }
  172.                 }
  173.                 GetTop(&OPTR,&last);
  174.         }
  175.         GetTop(&OPND,&last);
  176.         return last;
  177. }

  178. int main()
  179. {
  180.         printf("%d",Operate());
  181.         return 0;
  182. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-16 04:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表