鱼C论坛

 找回密码
 立即注册
查看: 3270|回复: 4

[已解决]C语言 数据结构括号匹配 主函数错误 求助!!

[复制链接]
发表于 2020-10-31 10:41:48 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 beannaeb 于 2020-10-31 18:36 编辑
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MaxSize 100
  4. typedef char ElemType;

  5. typedef struct
  6. {        ElemType data[MaxSize];
  7.         int top;
  8. }SqStack;

  9. //初始化栈运算算法
  10. void InitStack(SqStack &st)
  11. {       
  12.         st.top=-1;
  13. }

  14. //销毁栈运算算法
  15. void DestroyStack(SqStack st)
  16. {
  17. }

  18. //进栈运算算法
  19. int Push(SqStack &st,ElemType x)
  20. {        if(st.top==MaxSize-1)                        //栈满上溢出返回0
  21.                 return 0;
  22.         else
  23.         {        st.top++;
  24.                 st.data[st.top]=x;
  25.                 return 1;                                        //成功进栈返回1
  26.         }
  27. }

  28. //出栈运算算法
  29. int Pop(SqStack &st,ElemType &x)         //x为引用型参数
  30. {        if(st.top==-1)                                        //栈空返回0
  31.                 return 0;
  32.         else
  33.         {        x=st.data[st.top];
  34.                 st.top--;                                        //成功出栈返回1
  35.                 return 1;
  36.         }
  37. }

  38. //取栈顶元素运算算法
  39. int GetTop(SqStack st,ElemType &x)        //x为引用型参数
  40. {        if(st.top==-1)                                        //栈空返回0
  41.                 return 0;
  42.         else
  43.         {        x=st.data[st.top];
  44.                 return 1;                                        //成功取栈项返回1
  45.         }
  46. }

  47. //判断栈空运算算法
  48. int StackEmpty(SqStack st)
  49. {        if(st.top==-1)return 1;                        //栈空返回1
  50.         else return 0;                                        //栈不空返回0
  51. }

  52. int Match(char exp[],int n)
  53. {
  54.         SqStack st;
  55.         InitStack(st);
  56.         int flag=1,i=0;
  57.         char ch;
  58.         while(i<n&&flag==1)
  59.         {
  60.                 switch(exp[i])
  61.                 {
  62.                         case'(':case'[':case'{':
  63.                                 Push(st,exp[i]);break;
  64.                                 case')':
  65.                                         if(!Pop(st,ch)||ch!='(')
  66.                                         flag=0;
  67.                                         break;
  68.                                         case']':
  69.                                                 if(!Pop(st,ch)||ch!='[')
  70.                                                 flag=0;
  71.                                                 break;
  72.                                                 case'}':
  73.                                                         if(!Pop(st,ch)||ch!='{')
  74.                                                         flag=0;
  75.                                                         break;
  76.                 }
  77.                 i++;
  78.         }
  79.         if(StackEmpty(st)&&flag==1)
  80.         {
  81.                 DestroyStack(st);
  82.                 return 1;
  83.         }
  84.         else
  85.         {
  86.                 DestroyStack(st);
  87.                 return 0;
  88.         }
  89. }
  90. int main()
  91. {       
  92.         SqStack st;
  93.         int n;
  94.         int flag;
  95.         char *exp;
  96.         printf("判断结果如下\n");
  97.         flag=Match(exp,n);
  98.         printf("[(])%s\n",(flag==1?"是匹配的表达式":"不是匹配的表达式"));
  99.         printf("[()]%s\n",(flag==1?"是匹配的表达式":"不是匹配的表达式"));
  100.         printf("[()])%s\n",(flag ==1?"是匹配的表达式":"不是匹配的表达式"));
  101.         printf("([()]%s\n",(flag==1?"是匹配的表达式":"不是匹配的表达式"));
  102. }
复制代码
最佳答案
2020-10-31 20:46:18
本帖最后由 巴巴鲁 于 2020-10-31 21:06 编辑

很离谱,主函数里n和exp没有初始化或赋值,就当参数传进去了
关于括号匹配,我自己刚刚写了一个代码,仅供参考:
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAXSIZE 100

  4. typedef struct
  5. {
  6.         char data[MAXSIZE];
  7.         int top;
  8. }SqStack;

  9. // 入队运算
  10. int PushStack(SqStack *head, char ch)
  11. {
  12.         if (head->top >= MAXSIZE - 1) // 栈满溢出
  13.         {
  14.                 return 0;
  15.         }
  16.         else
  17.         {
  18.                 head->top++;
  19.                 head->data[head->top] = ch;
  20.                 return 1;
  21.         }
  22. }

  23. // 出队运算
  24. int PopStack(SqStack *head)
  25. {
  26.         if (head->top == -1)
  27.         {
  28.                 printf("栈为空,无法出队\n");
  29.                 return 0;
  30.         }
  31.         else
  32.         {
  33.                 head->top--;
  34.                 return 1;
  35.         }
  36. }


  37. // 取栈顶元素
  38. char  Gettop(SqStack *head)
  39. {
  40.         char ch;
  41.         ch = head->data[head->top];
  42.         return ch;
  43. }

  44. // 销毁栈
  45. void DestroyStack(SqStack *head)
  46. {
  47.         head->top = -1;
  48. }

  49. // 判断栈是否为空
  50. int EmptyStack(SqStack *head)
  51. {
  52.         if (head->top == -1)
  53.         {
  54.                 return 0;
  55.         }
  56.         return 1;
  57. }

  58. void MatchStack(SqStack *head)
  59. {
  60.         int flag = 1;
  61.         int i = 0;
  62.         char ch;
  63.         getchar();
  64.         char exp[MAXSIZE];
  65.         printf("请输入待判定的括号(以回车字符结束):");
  66.         while ((ch = getchar()) != '\n'  && flag == 1)
  67.         {
  68.                 switch (ch)
  69.                 {
  70.                         // 若是左括号,压入栈中
  71.                         case '(':
  72.                         case '[':
  73.                         case '{':
  74.                                 PushStack(head, ch);
  75.                                 break;
  76.                         case ')':
  77.                                 if (EmptyStack(head) && Gettop(head) == '(')
  78.                                 {
  79.                                         PopStack(head);
  80.                                 }
  81.                                 else
  82.                                 {
  83.                                         flag = 0;
  84.                                 }
  85.                                 break;
  86.                         case ']':
  87.                                 if (EmptyStack(head) && Gettop(head) == '[')
  88.                                 {
  89.                                         PopStack(head);
  90.                                 }
  91.                                 else
  92.                                 {
  93.                                         flag = 0;
  94.                                 }
  95.                                 break;
  96.                         case '}':
  97.                                 if (EmptyStack(head) && Gettop(head) == '{')
  98.                                 {
  99.                                         PopStack(head);
  100.                                 }
  101.                                 else
  102.                                 {
  103.                                         flag = 0;
  104.                                 }
  105.                                 break;
  106.                 }
  107.         }

  108.         if (EmptyStack(head))
  109.         {
  110.                 printf("不是匹配的表达式\n\n");
  111.         }
  112.         else
  113.         {
  114.                 printf("是匹配的表达式\n\n");
  115.         }
  116.         DestroyStack(head);

  117. }


  118. int main()
  119. {
  120.         SqStack *head = (SqStack *)malloc(sizeof(SqStack));
  121.         if (head == NULL)
  122.         {
  123.                 printf("内存调用失败,程序异常退出!\n");
  124.                 exit(0);
  125.         }
  126.         head->top = -1;

  127.         int pro, n, flag;

  128.         while (1)
  129.         {
  130.                 printf("--------------------------\n");
  131.                 printf("1.括号是否匹配\n");
  132.                 printf("2.退出程序\n");
  133.                 printf("--------------------------\n");
  134.                 printf("请输入待执行的功能:");
  135.                 scanf_s("%d", &pro);

  136.                 if (pro > 2 || pro < 1)
  137.                 {
  138.                         printf("输入有误,请重新输入!\n");
  139.                         continue;
  140.                 }

  141.                 switch (pro)
  142.                 {
  143.                         case 1:
  144.                                 MatchStack(head);
  145.                                 break;
  146.                         case 2:
  147.                                 printf("成功退出程序,欢迎下次再来^__^\n");
  148.                                 exit(0);
  149.                 }
  150.         }
  151. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-10-31 11:29:19 | 显示全部楼层
具体报错的内容你得发一下吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-31 12:07:03 | 显示全部楼层
小甲鱼的铁粉 发表于 2020-10-31 11:29
具体报错的内容你得发一下吧

程序可以运行但是没有进行判断
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-31 20:46:18 | 显示全部楼层    本楼为最佳答案   
本帖最后由 巴巴鲁 于 2020-10-31 21:06 编辑

很离谱,主函数里n和exp没有初始化或赋值,就当参数传进去了
关于括号匹配,我自己刚刚写了一个代码,仅供参考:
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MAXSIZE 100

  4. typedef struct
  5. {
  6.         char data[MAXSIZE];
  7.         int top;
  8. }SqStack;

  9. // 入队运算
  10. int PushStack(SqStack *head, char ch)
  11. {
  12.         if (head->top >= MAXSIZE - 1) // 栈满溢出
  13.         {
  14.                 return 0;
  15.         }
  16.         else
  17.         {
  18.                 head->top++;
  19.                 head->data[head->top] = ch;
  20.                 return 1;
  21.         }
  22. }

  23. // 出队运算
  24. int PopStack(SqStack *head)
  25. {
  26.         if (head->top == -1)
  27.         {
  28.                 printf("栈为空,无法出队\n");
  29.                 return 0;
  30.         }
  31.         else
  32.         {
  33.                 head->top--;
  34.                 return 1;
  35.         }
  36. }


  37. // 取栈顶元素
  38. char  Gettop(SqStack *head)
  39. {
  40.         char ch;
  41.         ch = head->data[head->top];
  42.         return ch;
  43. }

  44. // 销毁栈
  45. void DestroyStack(SqStack *head)
  46. {
  47.         head->top = -1;
  48. }

  49. // 判断栈是否为空
  50. int EmptyStack(SqStack *head)
  51. {
  52.         if (head->top == -1)
  53.         {
  54.                 return 0;
  55.         }
  56.         return 1;
  57. }

  58. void MatchStack(SqStack *head)
  59. {
  60.         int flag = 1;
  61.         int i = 0;
  62.         char ch;
  63.         getchar();
  64.         char exp[MAXSIZE];
  65.         printf("请输入待判定的括号(以回车字符结束):");
  66.         while ((ch = getchar()) != '\n'  && flag == 1)
  67.         {
  68.                 switch (ch)
  69.                 {
  70.                         // 若是左括号,压入栈中
  71.                         case '(':
  72.                         case '[':
  73.                         case '{':
  74.                                 PushStack(head, ch);
  75.                                 break;
  76.                         case ')':
  77.                                 if (EmptyStack(head) && Gettop(head) == '(')
  78.                                 {
  79.                                         PopStack(head);
  80.                                 }
  81.                                 else
  82.                                 {
  83.                                         flag = 0;
  84.                                 }
  85.                                 break;
  86.                         case ']':
  87.                                 if (EmptyStack(head) && Gettop(head) == '[')
  88.                                 {
  89.                                         PopStack(head);
  90.                                 }
  91.                                 else
  92.                                 {
  93.                                         flag = 0;
  94.                                 }
  95.                                 break;
  96.                         case '}':
  97.                                 if (EmptyStack(head) && Gettop(head) == '{')
  98.                                 {
  99.                                         PopStack(head);
  100.                                 }
  101.                                 else
  102.                                 {
  103.                                         flag = 0;
  104.                                 }
  105.                                 break;
  106.                 }
  107.         }

  108.         if (EmptyStack(head))
  109.         {
  110.                 printf("不是匹配的表达式\n\n");
  111.         }
  112.         else
  113.         {
  114.                 printf("是匹配的表达式\n\n");
  115.         }
  116.         DestroyStack(head);

  117. }


  118. int main()
  119. {
  120.         SqStack *head = (SqStack *)malloc(sizeof(SqStack));
  121.         if (head == NULL)
  122.         {
  123.                 printf("内存调用失败,程序异常退出!\n");
  124.                 exit(0);
  125.         }
  126.         head->top = -1;

  127.         int pro, n, flag;

  128.         while (1)
  129.         {
  130.                 printf("--------------------------\n");
  131.                 printf("1.括号是否匹配\n");
  132.                 printf("2.退出程序\n");
  133.                 printf("--------------------------\n");
  134.                 printf("请输入待执行的功能:");
  135.                 scanf_s("%d", &pro);

  136.                 if (pro > 2 || pro < 1)
  137.                 {
  138.                         printf("输入有误,请重新输入!\n");
  139.                         continue;
  140.                 }

  141.                 switch (pro)
  142.                 {
  143.                         case 1:
  144.                                 MatchStack(head);
  145.                                 break;
  146.                         case 2:
  147.                                 printf("成功退出程序,欢迎下次再来^__^\n");
  148.                                 exit(0);
  149.                 }
  150.         }
  151. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-1 09:10:01 | 显示全部楼层
巴巴鲁 发表于 2020-10-31 20:46
很离谱,主函数里n和exp没有初始化或赋值,就当参数传进去了
关于括号匹配,我自己刚刚写了一个代码,仅供 ...

好吧...谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-12 23:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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