/*
* Author:Biechuyangwang(心理咨询木木)
* Date:2018/02/22
*/
附加改错题(20分)
1.(10分)下面程序的功能是:从键盘读入某门课程的成绩,然后根据输入选项将其排序,输入1则按升序排序,输入2则降序排序;最后输出排序结果。下面程序中存在比较隐蔽的错误,请通过分析和调试程序,发现并改正程序中的错误。
#include <stdio.h>
#include <stdlib.h>
#define N 20
int ReadScore(int score[]);
void PrintScore(int score[], int n);
void SelectionSort(int score[], int n, int (*compare)(int a, int b));
void Swap(int *x, int *y);
int Ascending(int a, int b);
int Descending(int a, int b);
int main()
{
int score[N],n;
int order;
n = ReadScore(score);
printf("Total students are %d\n",n);
printf("Enter 1 to sort in ascending order\n");
printf("Enter 2 to sort in descending order\n");
scanf("%d",&order);
if(order == 1)
{
SelectionSort(score, n, Ascending);
printf("Data items in asending order\n");
}
else
{
SelectionSort(score, n, Descending);
printf("Data items in descending order\n");
}
PrintScore(score, n);
return 0;
}
/*读入成绩以负数结束,返回实际输入的成绩个数*/
int ReadScore(int score[])
{
int i=-1;
printf("Input score:\n");
do
{
i++;
scanf("%d",score[i]);
}while (score[i]>=0);
return i;
}
void PrintScore(int score[], int n)
{
int i;
for(i=0; i<n; i++)
printf("%4d",score[i]);
printf("\n");
}
void SelectionSort(int score[], int n, int *compare(int a, int b))
{
int i, j, k;
for(i=0; i<n-1;i++)
{
k = i;
for(j = i+1; j< n; j++)
{
if((*compare)(score[i],score[k]))
{
k = j;
}
}
if(k!= i)
{
Swap(score[i],score[k]);
}
}
}
int Ascending(int a, int b)
{
return a < b;
}
int Descending(int a, int b)
{
return a > b;
}
void Swap(int *x, int *y)
{
int * temp;
*temp = *x;
*x = *y;
*y = *temp;
}
注意:
(1)请将修改正确后的完整源程序拷贝粘贴到答题区内。
(2)对于没有错误的语句,请不要修改。
(3)当且仅当错误全部改正,且程序运行结果调试正确,才给加10分,部分正确不得分。
(4)改错时不能改变程序原有的意图,也不要改变代码的输入输出格式。
运行示例:
Input score:
98 97 88 78 99 57 -1
Total students are 6
Enter 1 to sort in ascending order
Enter 2 to sort in descending order
2
Data items in descending order
99 98 97 88 78 57
1
格式化代码 运行
修改的地方都注释了
#include <stdio.h>
#include <stdlib.h>
#define N 20
int ReadScore(int score[]);
void PrintScore(int score[], int n);
void SelectionSort(int score[], int n, int (*compare)(int a, int b));
void Swap(int *x, int *y);
int Ascending(int a, int b);
int Descending(int a, int b);
int main()
{
int score[N],n;
int order;
n = ReadScore(score);
printf("Total students are %d\n",n);
printf("Enter 1 to sort in ascending order\n");
printf("Enter 2 to sort in descending order\n");
scanf("%d",&order);
if(order == 1)
{
SelectionSort(score, n, Ascending);
printf("Data items in asending order\n");
}
else
{
SelectionSort(score, n, Descending);
printf("Data items in descending order\n");
}
PrintScore(score, n);
return 0;
}
/*读入成绩以负数结束,返回实际输入的成绩个数*/
int ReadScore(int score[])
{
int i=-1;
printf("Input score:\n");
do
{
i++;
scanf("%d",&score[i]); //修改一
}while (score[i]>=0);
return i;
}
void PrintScore(int score[], int n)
{
int i;
for(i=0; i<n; i++)
printf("%4d",score[i]);
printf("\n");
}
void SelectionSort(int score[], int n, int (*compare)(int a, int b)) //修改二
{
int i, j, k;
for(i=0; i<n-1;i++)
{
k = i;
for(j = i+1; j< n; j++)
{
if((*compare)(score[j],score[k])) //修改三
{
k = j;
}
}
if(k!= i)
{
Swap(&score[i],&score[k]); //修改四
}
}
}
int Ascending(int a, int b)
{
return a < b;
}
int Descending(int a, int b)
{
return a > b;
}
void Swap(int *x, int *y)
{
int temp; //修改五(包括下面几个)
temp = *x;
*x = *y;
*y = temp;
}
2.(10分)创建动态链表并打印链表内容。要求通过键盘读入学生的学号和分数,当输入的学号为0时表示链表创建结束,并将读入的学生学号和成绩打印输出。下面程序中存在比较隐蔽的错误,请通过分析和调试程序,发现并改正程序中的错误。
#include <stdio.h>
struct node
{
long num;
float score;
struct node *next;
}
int main()
{
struct node *head, *p, *q;
head = (struct node*)malloc(sizeof(struct node));
p = (struct node*)malloc(sizeof(struct node));
q = p;
head->next = p;
scanf("%ld%f", p->num, p->score );
while (p->num != 0)
{
q = (struct node*)malloc(sizeof(struct node));
p->next = q;
scanf("%ld%f", p->num, p->score);
}
p = head->next;
do
{
printf("%ld%.2f", p->num, p->score);
p = p->next;
}
while ( p->next == NULL );
free(p);
return 0;
}
注意:
(1)请将修改正确后的完整源程序拷贝粘贴到答题区内。
(2)对于没有错误的语句,请不要修改。
(3)当且仅当错误全部改正,且程序运行结果调试正确,才给加10分,部分正确不得分。
(4)改错时不能改变程序原有的意图,也不要改变代码的输入输出格式。
运行示例:
150301 65.5
150302 78
150303 87
150304 98.5
0 0
150301 65.50
150302 78.00
150303 87.00
150304 98.50
1
格式化代码 运行
修改的地方都注释了
#include <stdio.h>
#include<stdlib.h> //修改一
struct node
{
long num;
float score;
struct node *next;
}; //修改二
int main()
{
struct node *head, *p, *q;
head = (struct node*)malloc(sizeof(struct node));
p = (struct node*)malloc(sizeof(struct node));
q = p;
head->next = p;
scanf("%ld%f", &(p->num), &(p->score) ); //修改三
while (p->num != 0)
{
p = (struct node*)malloc(sizeof(struct node)); //修改四
q->next = p;
scanf("%ld%f", &(p->num), &(p->score) ); //修改三
q=p; //修改五
}
p->next=NULL; //修改六
p = head->next;
while(p->next) //修改七
{
printf("%ld %.2f\n", p->num, p->score);
p = p->next;
}
free(p);
return 0;
}