首页 > 解决方案 > 执行数据结构“堆栈”期间执行循环中的逻辑错误

问题描述

我的老师给了我一个家庭作业问题,我们必须将其中的push元素pop放在堆栈中。

输入应为: -

  1. 输入的第一行必须包含 no。堆栈中的元素。

  2. 第二行输入必须包含用户(即)选择是PUSH(即进入'1')还是POP元素(即进入 '2')。

  3. 如果用户选择了 PUSH 操作;那么输入的第三行必须包含要压入堆栈的元素。

  4. 如果用户希望继续这些操作,下一行输入必须包含'y'或作为回复。'n'

测试用例 1

输入

3//(capacity of stack)

1//(selecting PUSH OR POP)

6//(entering the element which is to entered)

是的//(to continue or not continue)

1//(selecting PUSH OR POP)

4//(entering the element which is to entered)

是的//(to continue or not continue)

1//(selecting PUSH OR POP)

7//(entering the element which is to entered)

是的//(to continue or not continue)

2//(selecting PUSH OR POP)

n//(to continue or not continue)

输出

deleted element is

7 4 6

对于上述问题,我编写了以下代码:-

# include <stdio.h>
# include <stdlib.h>

struct Stack
{
    int capacity;
    int top;
    int *array;
};

void push(struct Stack *stack, int a) //function to PUSH a character in the stack.
{
    stack->array[++stack->top] = a;
}

int pop(struct Stack *stack) //function to POP a character in the stack.
{
    return stack->array[stack->top--];
}

int main(void)
{
    struct Stack obj;
    obj.top = -1;

    printf("Enter the capacity of stack\n");
    scanf("%d", &obj.capacity); //Inputting the capacity of the stack.

    obj.array = calloc(obj.capacity, sizeof(int));

    int operation;
    int element;
    char continuation;

    do
    {
        printf("\nEnter 1 if you want to PUSH or 2 for POP\n");
        scanf("%d", &operation);

        printf("\nEnter the element which is to be pushed\n");
        scanf("%d", &element);

        scanf("%*c"); //To ignore any newline in stdin buffer.
        printf("\nEnter 'y' if you want to continue else enter 'n'\n");
        scanf("[a-z]%c", &continuation);

        if(operation == 1)
        {
            if(obj.top < obj.capacity)
            {
                push(&obj, element);
            }
            else
            {
                printf("Error\n");
                return EXIT_FAILURE;
            }
        }

        else if(operation == 2)
        {
            printf("deleted element is\n");
            while(obj.top != -1) //will POP all elements on the stack and print it.
            {
                printf("%d", pop(&obj));
                if(obj.top != 0)
                {
                    printf(" ");
                }
            }
        }

        else
        {
            printf("Wrong operation specified\n");
            return EXIT_FAILURE;
        }
    } while(continuation == 'y');

    return 0;
}

我在上面的代码中遇到的问题是在输入以下输入后: -

3

1

6

是的

程序关闭(即退出do-while循环)。但是,当我在代码中更改以下行时: -

char continuation;

char continuation = 'y';

在我输入以下内容之前它工作正常: -

3

1

6

是的

1

4

是的

1

7

是的

之后给我一个突然的输出: -

Error

然后退出程序。

我的问题是: -

为什么我的代码在我只写的第一种情况下不起作用char continuation;

当我更改为时,第二种情况下的“逻辑错误”是char continuation;什么char continuation = 'y';

标签: cloops

解决方案


我可以通过删除"[a-z]%c"和替换它来解决我的问题" %c"

这是我的新(正确)代码。

# include <stdio.h>
# include <stdlib.h>

struct Stack
{
    int capacity;
    int top;
    int *array;
};

void push(struct Stack *stack, int a) //function to PUSH a character in the stack.
{
    stack->array[++stack->top] = a;
}

int pop(struct Stack *stack) //function to POP a character in the stack.
{
    return stack->array[stack->top--];
}

int main(void)
{
    struct Stack obj;
    obj.top = -1;

    printf("Enter the capacity of stack\n");
    scanf("%d", &obj.capacity); //Inputting the capacity of the stack.

    obj.array = calloc(obj.capacity, sizeof(int));

    int operation;
    int element;
    char continuation;

    do
    {
        printf("\nEnter 1 if you want to PUSH or 2 for POP\n");
        scanf("%d", &operation);

        if(operation == 1)
        {
            printf("\nEnter the element which is to be pushed\n");
            scanf("%d", &element);

            if(obj.top < obj.capacity)
            {
                push(&obj, element);
            }
            else
            {
                printf("Error\n");
                return EXIT_FAILURE;
            }
        }

        else if(operation == 2)
        {
            printf("deleted element is\n");
            while(obj.top != -1) //will POP all elements on the stack and print it.
            {
                printf("%d", pop(&obj));
                if(obj.top != 0)
                {
                    printf(" ");
                }
            }
        }

        else
        {
            printf("Wrong operation specified\n");
            return EXIT_FAILURE;
        }

        scanf("%*c"); //To ignore any newline in stdin buffer.
        printf("\nEnter 'y' if you want to continue else enter 'n'\n");
        scanf(" %c", &continuation);

    } while(continuation == 'y');

    return 0;
}

注意: - 我也避免printf按照评论中的建议进行交错。

但是,我仍然无法理解为什么我以前的代码不起作用。

附言

我终于弄错了,而不是写"[a-z]%c"应该是"%[a-z]c"


推荐阅读