修改程序
题目如下
试设计一个算法,建立一个学生成绩栈或队列。要求从键盘上输入 N 个整数,按照下列要求分别进入不同栈或队列,并分别输出每个栈或队列的内容。
(1) 若输入的整数 x 小于 60 ,则进入第一个栈或队列;
(2) 若输入的整数 x 大于等于 60 ,小于 100 ,则进入第二个栈或队列;
(3) 若输入的整数 x 大于等于 100 ,则进入第三个栈或队列;
我的代码
#include
#include
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue *Q){
//构造新的队列
Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q->front) exit(OVERFLOW);
Q->front->next=NULL;
return OK;
}
Status EnQueue(LinkQueue *Q,QElemType e){
//插入元素e为Q的新的队尾元素
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p->next=NULL;
p->data=e;
Q->rear->next=p;
Q->rear=p;
return OK;
}
Status DeQueue(LinkQueue *Q,QElemType *e){
//若队不空,删除Q的队头元素,并用e返回其值
QueuePtr p;
if(Q->front==Q->rear) return ERROR;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p=Q->front->next;
*e=p->data;
Q->front->next=p->next;
if(Q->rear==p) Q->rear=Q->front;
free(p);
return OK;
}
int main(){
int n,a,i;
char c;
int m1=0,m2=0,m3=0;
LinkQueue Q1, Q2,Q3;
InitQueue(&Q1);
InitQueue(&Q2);
InitQueue(&Q3);
scanf("%d",&n);
for(i=0;iwhile(scanf("%d%c",&a,&c)==2&&c!='\n'){
if(a<60) EnQueue(&Q1,a);
if(a>=60&&a<100) EnQueue(&Q2,a);
if(a>=100) EnQueue(&Q3,a);
}
}
printf("60>x:");
while(DeQueue(&Q1,&m1)==OK){
printf("%d ", m1);
}
printf("\n");
printf("60<=x<100:");
while(DeQueue(&Q2,&m2)==OK){
printf("%d ", m2);
}
printf("\n");
printf("x>=100:");
while(DeQueue(&Q3,&m3)==OK){
printf("%d ", m3);
}
printf("\n");
return 0;
}
问题
x>=100为什么无法输出
1 2 1秒 64M 以文本方式显示 下载
6↵
34 67 78 90 99 100↵
以文本方式显示 下载
60>x:34 ↵
60<=x<100:67 78 90 99 ↵
x>=100:100 ↵
以文本方式显示 下载
60>x:34 ↵
60<=x<100:67 78 90 99 ↵
x>=100:↵
调试一下,是输入的问题。
参考GPT和自己的思路:
在程序中,若输入的整数 x 大于等于100,会进入第三个栈或队列,即 Q3 队列。但在输出第三个队列时,程序中没有考虑到输出队列为空的情况,因此没有输出任何内容,只是输出了一个换行符。为了解决这个问题,可以在输出第三个队列的 while 循环前先进行一个判断,如果队列为空,则直接输出一个空格即可,如下所示:
printf("x>=100:");
if(Q3.front==Q3.rear) printf(" ");
else{
while(DeQueue(&Q3,&m3)==OK){
printf("%d ", m3);
}
}
printf("\n");
这样就能够正确输出第三个队列了。完整修改后的代码如下:
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int QElemType;
typedef struct QNode{
QElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue *Q){
//构造新的队列
Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q->front) exit(OVERFLOW);
Q->front->next=NULL;
return OK;
}
Status EnQueue(LinkQueue *Q,QElemType e){
//插入元素e为Q的新的队尾元素
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p->next=NULL;
p->data=e;
Q->rear->next=p;
Q->rear=p;
return OK;
}
Status DeQueue(LinkQueue *Q,QElemType *e){
//若队不空,删除Q的队头元素,并用e返回其值
QueuePtr p;
if(Q->front==Q->rear) return ERROR;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p=Q->front->next;
*e=p->data;
Q->front->next=p->next;
if(Q->rear==p) Q->rear=Q->front;
free(p);
return OK;
}
int main(){
int n,a,i;
char c;
int m1=0,m2=0,m3=0;
LinkQueue Q1, Q2,Q3;
InitQueue(&Q1);
InitQueue(&Q2);
InitQueue(&Q3);
scanf("%d",&n);
for(i=0;i<n;i++){
while(scanf("%d%c",&a,&c)==2&&c!='\n'){
if(a<60) EnQueue(&Q1,a);
if(a>=60&&a<100) EnQueue(&Q2,a);
if(a>=100) EnQueue(&Q3,a);
}
}
printf("60>x:");
while(DeQueue(&Q1,&m1)==OK){
printf("%d ", m1);
}
printf("\n");
printf("60<=x<100:");
while(DeQueue(&Q2,&m2)==OK){
printf("%d ", m2);
}
printf("\n");
printf("x>=100:");
if(Q3.front==Q3.rear) printf(" ");
else{
while(DeQueue(&Q3,&m3)==OK){
printf("%d ", m3);
}
}
printf("\n");
return 0;
}