C 显示“的内容”;“排队”;Isn';行不通
到了我开始为学校作业发狂的地步。箱子的3号和4号工作不正常。这样,它们就不会显示队列的详细信息 对于案例3,应显示队列的第一项,对于案例4,应显示“队列”的所有内容。我的案例5功能在向“队列”添加项目方面运行良好 然而,我编写代码的方式肯定存在一些差异,因为当我向队列中插入项目时,当我按“3&4”显示队列的内容时,没有显示任何内容 我使用的很多参考文献都是整数值的例子,我一直在努力将一些变量转换为基于字符串/字符变量。某些变量必须用某些指针引用的方式让我有点困惑 提前感谢大家的帮助和建议。非常感谢每个人的慷慨与他们的时间和建议C 显示“的内容”;“排队”;Isn';行不通,c,function,queue,C,Function,Queue,到了我开始为学校作业发狂的地步。箱子的3号和4号工作不正常。这样,它们就不会显示队列的详细信息 对于案例3,应显示队列的第一项,对于案例4,应显示“队列”的所有内容。我的案例5功能在向“队列”添加项目方面运行良好 然而,我编写代码的方式肯定存在一些差异,因为当我向队列中插入项目时,当我按“3&4”显示队列的内容时,没有显示任何内容 我使用的很多参考文献都是整数值的例子,我一直在努力将一些变量转换为基于字符串/字符变量。某些变量必须用某些指针引用的方式让我有点困惑 提前感谢大家的帮助和建议。非常感
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
struct books
{
int bookId;
char bookTitle[200];
struct books *nextPtr;
};
struct books *first= NULL;
typedef struct books books;
typedef books *booksPtr;
#define SIZE 5
# define max 5
#define MAX 200
void instructionsa (void)
{
puts ("Enter your choice:\n"
" 1 Display next order information \n"
" 2 Display all orders \n"
" 3 Add order to queue \n"
" 4 Quit program \n"
);
}
int AddOrder(char orderQueue[max][80], int *rear, char bookOrder[80])
{
if(*rear == max -1)
return(-1);
else
{
*rear = *rear + 1;
strcpy(orderQueue[*rear], bookOrder);
return(1);
}
}
void displayallorders()
{
char orderQueue[max][80];
int i;
int rear = - 1;
int front = - 1;
if(front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for(i = front; i <= rear; i++)
printf("%s \n", orderQueue[i]);
printf("n");
}
}
void displayNextOrder()
{
char orderQueue[max][80];
int i;
int rear = - 1;
int front = - 1;
if(front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for(i=0; i<1; i++)
printf("%s \n", orderQueue[i]);
printf("n");
}
}
int main()
{
unsigned int choice;
int i=0;
int bookId;
char bookTitle[200];
char orderQueue[max][80], bookOrder[80];
int front, rear, reply;
int ch;
front = rear = -1; //... Initialize a Queue
instructionsa ();
scanf ("%u", &choice);
while (choice != 10)
{
switch (choice)
{
case 1: //Display next order information
displayNextOrder();
break;
case 2: //Display all orders
displayallorders();
break;
case 3: //Add order to queue
printf("\nEnter new Book to Order: ");
fflush (stdin);
scanf("%[^\n]", bookOrder);
fflush (stdin);
reply = AddOrder(orderQueue, &rear, bookOrder);
if(reply == -1 )
printf("\nQueue is Full \n");
else
printf("\n'%s' is inserted in queue.\n\n",bookOrder);
break;
case 4: //Quit program
break;
default:
puts ("You have entered an invalid code. Try again\n");
instructionsa ();
break;
}
instructionsa ();
scanf ("%u", &choice);
}
puts ("Thank You & Goodbye");
}
#包括
#包括
#包括
#包括
结构书
{
国际图书编号;
字符书名[200];
结构书*nextPtr;
};
struct books*first=NULL;
typedef结构书;
typedef books*booksPtr;
#定义尺寸5
#定义最大值5
#定义最大值200
无效指令A(无效)
{
放置(“输入您的选择:\n”
“1显示下一订单信息\n”
“2显示所有订单\n”
“3将订单添加到队列\n”
“4退出程序\n”
);
}
int AddOrder(char orderQueue[max][80],int*rear,char bookOrder[80])
{
如果(*后==最大值-1)
返回(-1);
其他的
{
*后部=*后部+1;
strcpy(orderQueue[*后方],bookOrder);
申报表(1);
}
}
void displayallorders()
{
char orderQueue[max][80];
int i;
int rear=-1;
int front=-1;
如果(前==-1)
printf(“队列为空\n”);
其他的
{
printf(“队列为:\n”);
对于(i=front;仅供参考,有两件事你应该马上解决。(1)fflush(stdin)
永远不应该被使用。fflush
只是为输出流定义的。有一些方法可以通过下一个换行清除输入流,但不是这样。和(2)“%[^\n]s
作为格式说明符是胡说八道。使用任意一种设置符号(“%[^\n]”
)或字符串(“%s”
)目标,而不是两者都有。当然,还有。