gpt4 book ai didi

c - printf scanf 执行顺序正在交换

转载 作者:行者123 更新时间:2023-11-30 19:46:18 25 4
gpt4 key购买 nike

printf("Enter the value\n");
scanf("%d ",&data);

但是 scanfprintf 之前执行。可能是什么原因?

这是我的整个程序

#include<stdio.h>
#include<stdlib.h>
struct Queue{
int front,rear;
int capacity;
int *array;
};

void enqueue(struct Queue *q){

int data;
if(q->front == (q->rear+1)%q->capacity){
printf("Queue is full\n");
return;
}else{
printf("Enter the value\n");
scanf("%d ",&data);
q->rear = (q->rear+1) % q->capacity;
q->array[q->rear] =data;

if(q->front == -1)
q->front = q->rear;
}

}
void dequeue(struct Queue *q){

if( q->front == -1){
printf("Queue is Empty\n");
return;
}else{
printf("value dequeued %d \n",q->array[q->front]);
if(q->front == q->rear)
q->front = q->rear = -1;
else
q->front = (q->front+1)% q->capacity;
}
}
void printQueue(struct Queue *q){

if( q->front == -1){
printf("Queue is Empty\n");
return;
}else{
int f = q->front, r =q->rear;

while((f% (q->capacity)) != r)
{
printf ("\t %d", q->array[f] );
f++;
}
printf("\t %d",q->array[q->rear]);
printf("\n");
}
}


main(){

struct Queue *q = malloc(sizeof(struct Queue));
if(!q){
printf("Memory Error\n");
exit;
}
q->capacity = 3;
q->front= q->rear = -1;
q->array = malloc(q->capacity * sizeof(int));
if(!q->array){
printf("Memory Error\n");
exit;
}

int option;
char ch ='a';
while(1) {
printf("Enter the choice 1.Enqueue or 2.Dequeue 3.print\n");
fflush(stdout);
scanf("%d",&option);
switch(option){
case 1:
enqueue(q);
break;
case 2:
dequeue(q);
break;
case 3:
printQueue(q);
break;
default:
printf("Wrong choice\n");
}


printf("Do you want to continue? press y to continue\n");
scanf("%s", &ch);
if(ch != 'y')break;
}
}

当我将一个元素排入队列时,它会等待输入,然后执行 printf 语句。 e 您想继续吗?理想情况下,当我输入要排队的元素并按 Enter 键时,它应该询问我是否要继续,然后它应该等待输入,即“y”。但是,一旦我输入要入队的元素,它就会等待输入“y”,输入“y”后,“你想继续吗”就会被执行

最佳答案

重新设计了问题代码。最大的问题是使用 scanf(),它会将未读取的值(例如回车符)留在标准输入中。未读字符会干扰下次使用 scanf()。通过将 %*c 添加到格式字符串,更改了 scanf() 以吞噬标准输入中的多余字符:

#include<stdio.h>
#include<stdlib.h>
struct Queue{
int front,rear;
int capacity;
int *array;
};

void enqueue(struct Queue *q){

int data;
if(q->front == (q->rear+1)%q->capacity){
printf("Queue is full\n");
return;
}else{
printf("Enter the value\n");
scanf(" %d%*c",&data);
q->rear = (q->rear+1) % q->capacity;
q->array[q->rear] =data;

if(q->front == -1)
q->front = q->rear;
}

}
void dequeue(struct Queue *q){

if( q->front == -1){
printf("Queue is Empty\n");
return;
}else{
printf("value dequeued %d \n",q->array[q->front]);
if(q->front == q->rear)
q->front = q->rear = -1;
else
q->front = (q->front+1)% q->capacity;
}
}
void printQueue(struct Queue *q){

if( q->front == -1){
printf("Queue is Empty\n");
return;
}else{
int f = q->front, r =q->rear;

while((f% (q->capacity)) != r)
{
printf ("\t %d", q->array[f] );
f++;
}
printf("\t %d",q->array[q->rear]);
printf("\n");
}
}


int main()
{
int option;
char ch ='a';

struct Queue *q = malloc(sizeof(struct Queue));
if(!q)
{
printf("Memory Error\n");
goto CLEANUP;
}

q->capacity = 3;
q->front= q->rear = -1;
q->array = malloc(q->capacity * sizeof(int));
if(!q->array)
{
printf("Memory Error\n");
goto CLEANUP;
}

while(1)
{
printf("Enter the choice 1.Enqueue or 2.Dequeue 3.print\n");
scanf(" %d%*c",&option);
switch(option)
{
case 1:
enqueue(q);
break;

case 2:
dequeue(q);
break;

case 3:
printQueue(q);
break;

default:
printf("Wrong choice\n");
break;
}


printf("Do you want to continue? press y to continue\n");
scanf(" %c%*c", &ch);
if(ch != 'y')
break;
}

CLEANUP:

return(0);
}

关于c - printf scanf 执行顺序正在交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24176455/

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