gpt4 book ai didi

c - 数据结构 "stack"执行期间 do-while 循环中的逻辑错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:09:20 25 4
gpt4 key购买 nike

我的老师给我布置了一道作业,要求我们在堆栈中pushpop 元素。

输入应为:-

  1. First line of input must contain no. of elements in the stack.

  2. Second line of input must contain the choice of user (i.e.) whether to PUSH (i.e. entering '1') or POP element (i.e. entering '2').

  3. If PUSH operation has been selected as user's choice; then third line of input must contain the element to be pushed into stack.

  4. the next line of input must contain either 'y' or 'n' as a reply to if the user wishes to continue these operations.

测试用例 1

输入

3 //(capacity of stack)

1 //(selecting PUSH OR POP)

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

y //(to continue or not continue)

1 //(selecting PUSH OR POP)

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

y //(to continue or not continue)

1 //(selecting PUSH OR POP)

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

y //(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

y

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

字符延续;

char continuation = 'y';

它工作正常,直到我输入以下内容:-

3

1

6

y

1

4

y

1

7

y

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

Error

然后退出程序。

我的问题是:-

Why my code does not work in the first case when i only write char continuation; ?

What is the "logical error" in the second case when I change char continuation; to char continuation = 'y'; ?

最佳答案

我可以通过删除[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"

关于c - 数据结构 "stack"执行期间 do-while 循环中的逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58201848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com