鱼C论坛

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

用堆栈判断是否为回文,为什么我这里结果不对,请大佬帮忙看看

[复制链接]
发表于 2022-4-25 19:45:17 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 zyf117 于 2022-4-25 23:39 编辑

因为不会调试,所以打印了一些中间变量,查看问题,请大佬们帮忙看看是哪里出了问题,以下是代码和编译结果
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define STACK_INIT_SIZE 20
#define STACKINCREMENT  10

typedef char ElemType;
typedef struct
{
    ElemType *base;
    ElemType *top;
    int stackSize;
}sqStack;

void InitStack(sqStack *s)
{
    s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
    if( !s->base )
    {
        exit(0);
    }

    s->top = s->base;
    s->stackSize = STACK_INIT_SIZE;
}

void Push(sqStack *s, ElemType e)
{
    if( s->top - s->base >= s->stackSize )
    {
        s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
        if( !s->base )
        {
            exit(0);
        }
    }

    *(s->top) = e;
    s->top++;
}

void Pop(sqStack *s, ElemType *e)
{
    if( s->top == s->base )
    {
        return;
    }
    *e = *--(s->top);
}

int StackLen(sqStack s)
{
    return (s.top - s.base);
}

void main()
{
        char a[STACK_INIT_SIZE];
        char b[STACK_INIT_SIZE];
        int i,j = 0;
        int len;
        sqStack Pal1,Pal2;
        ElemType c;

        InitStack(&Pal1);
        InitStack(&Pal2);
       
        printf("请输入一组以@结尾的回文:");
        scanf("%c", &c);
    while( c != '@' )
    {
        Push(&Pal1, c);
        scanf("%c", &c);
    }

        getchar();
       
        len = StackLen(Pal1);
    printf("栈的当前容量是: %d\n", len);
        printf("第一个栈为:");
        for( i=0; i < len; i++ )
    {
        Pop(&Pal1, &c);
        printf("%c",c);
    }
    for( i=0; i < len-1; i++ )
    {
        Pop(&Pal1, &c);
        Push(&Pal2, c);
    }

        printf("\n");
        printf("第二个栈为:");
        for( i=0; i < len; i++ )
    {
        Pop(&Pal2, &c);
        printf("%c",c);
    }
       
        printf("\n");
        for( i=0; i < len-1; i++ )
    {
        Pop(&Pal1, &c);
        a = c;
    }
        printf("第一个数组为");
        for( i=0; i < len; i++ )
    {
        printf("%c ",a);
    }
        printf("\n");
        for( i=0; i < len; i++ )
    {
        Pop(&Pal2, &c);
        b = c;
    }
        printf("第二个数组为:");
        for( i=0; i < len; i++ )
    {
        printf("%c ",b);
    }
        printf("\n");
        printf("比较数组:");
        for( i=0; i < len-1; i++ )
    {
                if(a == b)
                {
                        j++;
                        printf("%c-%c %d  %d\n ",a,b,j,i);
                }
    }
        if(j == len)
        {
                printf("是回文");
        }
        else
        {
                printf("不是回文");
        }
}
回文判断.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-4-26 11:58:08 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <stdbool.h>

  5. #define STACK_INIT_SIZE 20
  6. #define STACKINCREMENT  10

  7. typedef char ElemType;
  8. typedef struct
  9. {
  10.     ElemType *base;
  11.     ElemType *top;
  12.     int stackSize;
  13. }sqStack;

  14. void InitStack(sqStack *s)
  15. {
  16.     s->base = malloc(STACK_INIT_SIZE * sizeof(ElemType));
  17.     if( !s->base )
  18.     {
  19.         exit(0);
  20.     }

  21.     s->top = s->base;
  22.     s->stackSize = STACK_INIT_SIZE;
  23. }

  24. void Push(sqStack *s, ElemType e)
  25. {
  26.     if( s->top - s->base >= s->stackSize )
  27.     {
  28.         s->base = (ElemType *)realloc(s->base, (s->stackSize += STACKINCREMENT) * sizeof(ElemType));
  29.         //s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
  30.         if( !s->base )
  31.         {
  32.             exit(0);
  33.         }
  34.     }

  35.     *(s->top) = e;
  36.     s->top++;
  37. }

  38. void Pop(sqStack *s, ElemType *e)
  39. {
  40.     if( s->top == s->base )
  41.     {
  42.         return;
  43.     }
  44.     *e = *--(s->top);
  45. }

  46. int StackLen(sqStack s)
  47. {
  48.     return (s.top - s.base);
  49. }

  50. void stack_deinit(sqStack *s) {
  51.     if(s) free(s->base);
  52. }

  53. //void main()
  54. int main(void)
  55. {
  56.     sqStack s;
  57.     InitStack(&s);
  58.     while(1) {
  59.         char ch = getchar();
  60.         if(ch == '@') break;
  61.         Push(&s, ch);
  62.     }
  63.     char buff[StackLen(s)];
  64.     size_t size = 0;
  65.     while(StackLen(s) != 0) {
  66.         char e; Pop(&s, &e);
  67.         buff[size++] = e;
  68.     }
  69.     for(size_t i = 0; i < size; ++i) {
  70.         Push(&s, buff[i]);
  71.     }
  72.     bool flag = true;
  73.     for(size_t i = 0; i < size; ++i) {
  74.         char e; Pop(&s, &e);
  75.         if(e != buff[i]) {
  76.             flag = false;
  77.             break;
  78.         }
  79.     }
  80.     printf("%s\n", flag ? "==" : "!=");
  81.     stack_deinit(&s);
  82.     return 0;
  83. #if 0
  84.         char a[STACK_INIT_SIZE];
  85.         char b[STACK_INIT_SIZE];
  86.         int i,j = 0;
  87.         int len;
  88.         sqStack Pal1,Pal2;
  89.         ElemType c;

  90.         InitStack(&Pal1);
  91.         InitStack(&Pal2);
  92.       
  93.         printf("请输入一组以@结尾的回文:");
  94.         scanf("%c", &c);
  95.     while( c != '@' )
  96.     {
  97.         Push(&Pal1, c);
  98.         scanf("%c", &c);
  99.     }

  100.         getchar();
  101.       
  102.         len = StackLen(Pal1);
  103.     printf("栈的当前容量是: %d\n", len);
  104.         printf("第一个栈为:");
  105.         for( i=0; i < len; i++ )
  106.     {
  107.         Pop(&Pal1, &c);
  108.         printf("%c",c);
  109.     }
  110.     for( i=0; i < len-1; i++ )
  111.     {
  112.         Pop(&Pal1, &c);
  113.         Push(&Pal2, c);
  114.     }

  115.         printf("\n");
  116.         printf("第二个栈为:");
  117.         for( i=0; i < len; i++ )
  118.     {
  119.         Pop(&Pal2, &c);
  120.         printf("%c",c);
  121.     }
  122.       
  123.         printf("\n");
  124.         for( i=0; i < len-1; i++ )
  125.     {
  126.         Pop(&Pal1, &c);
  127.         a = c;
  128.     }
  129.         printf("第一个数组为");
  130.         for( i=0; i < len; i++ )
  131.     {
  132.         printf("%c ",a);
  133.     }
  134.         printf("\n");
  135.         for( i=0; i < len; i++ )
  136.     {
  137.         Pop(&Pal2, &c);
  138.         b = c;
  139.     }
  140.         printf("第二个数组为:");
  141.         for( i=0; i < len; i++ )
  142.     {
  143.         printf("%c ",b);
  144.     }
  145.         printf("\n");
  146.         printf("比较数组:");
  147.         for( i=0; i < len-1; i++ )
  148.     {
  149.                 if(a == b)
  150.                 {
  151.                         j++;
  152.                         printf("%c-%c %d  %d\n ",a,b,j,i);
  153.                 }
  154.     }
  155.         if(j == len)
  156.         {
  157.                 printf("是回文");
  158.         }
  159.         else
  160.         {
  161.                 printf("不是回文");
  162.         }
  163.         return 0;
  164. #endif
  165. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 20:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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