鱼C论坛

 找回密码
 立即注册
查看: 1531|回复: 1

[已解决]c语言 用栈判断字符串是否回文 新手求救!!

[复制链接]
发表于 2020-10-24 15:23:06 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MaxSize 100
  4. typedef int 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 Palindrome(char str[],int n)
  53. {
  54.         SqStack st;
  55.         InitStack(st);
  56.         int i;
  57.         int ch;
  58.         for(i=0;i<n;i++)
  59.                 Push(st,str[i]);
  60.         i=0;
  61.         while(!StackEmpty(st))
  62.         {
  63.                 Pop(st,ch);
  64.                 if(ch!=str[i++])
  65.                 {
  66.                         DestroyStack(st);
  67.                         return 0;
  68.                  }
  69.          }
  70.         DestroyStack(st);
  71.          return 1;
  72.   }
  73. int main(){
  74.         printf("输入一个字符串:\n");
  75.         char str[81];
  76.         int n;
  77.         int flag;
  78.         scanf("%d",&n);
  79.         flag=Palindrome(str,n);
  80.         if (flag)
  81.     {
  82.         printf("字符串是回文");
  83.     }
  84.     else
  85.         {
  86.                 printf("字符串不是回文");
  87.     }
  88.     return 0;
  89. }
复制代码

程序可以运行但是结果有问题
最佳答案
2020-10-24 17:33:27
主函数那里有点问题
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MaxSize 100
  4. typedef int 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 Palindrome(char str[],int n)
  53. {
  54.          SqStack st;
  55.          InitStack(st);
  56.          int i;
  57.          int ch;
  58.          for(i=0;i<n;i++)
  59.                  Push(st,str[i]);
  60.          i=0;
  61.          while(!StackEmpty(st))
  62.          {
  63.                  Pop(st,ch);
  64.                  if(ch!=str[i++])
  65.                  {
  66.                          DestroyStack(st);
  67.                          return 0;
  68.                  }
  69.          }
  70.         DestroyStack(st);
  71.          return 1;
  72.   }
  73. int main(){
  74.         printf("输入一个字符串长度:\n");
  75.         char str[81];
  76.         int n;
  77.         int flag;
  78.         scanf("%d",&n);
  79.         getchar();  // 缓冲输入的回车符
  80.         printf("输入%d个字符:\n",n);
  81.         gets(str);
  82.         flag=Palindrome(str,n);
  83.         if (flag)
  84.     {
  85.         printf("字符串是回文");
  86.     }
  87.     else
  88.         {
  89.                 printf("字符串不是回文");
  90.     }
  91.     return 0;
  92. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-10-24 17:33:27 | 显示全部楼层    本楼为最佳答案   
主函数那里有点问题
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define MaxSize 100
  4. typedef int 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 Palindrome(char str[],int n)
  53. {
  54.          SqStack st;
  55.          InitStack(st);
  56.          int i;
  57.          int ch;
  58.          for(i=0;i<n;i++)
  59.                  Push(st,str[i]);
  60.          i=0;
  61.          while(!StackEmpty(st))
  62.          {
  63.                  Pop(st,ch);
  64.                  if(ch!=str[i++])
  65.                  {
  66.                          DestroyStack(st);
  67.                          return 0;
  68.                  }
  69.          }
  70.         DestroyStack(st);
  71.          return 1;
  72.   }
  73. int main(){
  74.         printf("输入一个字符串长度:\n");
  75.         char str[81];
  76.         int n;
  77.         int flag;
  78.         scanf("%d",&n);
  79.         getchar();  // 缓冲输入的回车符
  80.         printf("输入%d个字符:\n",n);
  81.         gets(str);
  82.         flag=Palindrome(str,n);
  83.         if (flag)
  84.     {
  85.         printf("字符串是回文");
  86.     }
  87.     else
  88.         {
  89.                 printf("字符串不是回文");
  90.     }
  91.     return 0;
  92. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 03:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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