《C语言程序设计实例教程》课后习题答案.docx

上传人:b****9 文档编号:138260 上传时间:2022-10-04 格式:DOCX 页数:35 大小:20.12KB
下载 相关 举报
《C语言程序设计实例教程》课后习题答案.docx_第1页
第1页 / 共35页
《C语言程序设计实例教程》课后习题答案.docx_第2页
第2页 / 共35页
《C语言程序设计实例教程》课后习题答案.docx_第3页
第3页 / 共35页
《C语言程序设计实例教程》课后习题答案.docx_第4页
第4页 / 共35页
《C语言程序设计实例教程》课后习题答案.docx_第5页
第5页 / 共35页
点击查看更多>>
下载资源
资源描述

《C语言程序设计实例教程》课后习题答案.docx

《《C语言程序设计实例教程》课后习题答案.docx》由会员分享,可在线阅读,更多相关《《C语言程序设计实例教程》课后习题答案.docx(35页珍藏版)》请在冰豆网上搜索。

《C语言程序设计实例教程》课后习题答案.docx

#include #include #define STU_NUM30int Menu(void) ;void ReadScore(longnum[], float score[], intn) ;

void

AverSumofScore(floatscore[], int n) ;void SortbyScore(longnum[], float score[], intn, int (*compare) (floata, float b) ) ;int Ascending(floata, float b) ;int Descending(floata, float b) ;void SwapFloat(float*x, float *y) ;void SwapLong(long*x, long *y) ;void AsSortbyNum(longnum[], float score[], intn) ;

void SearchbyNum(longnum[], float score[], intn) ;

void

StatistcAnalysis(floatscore[], int n) ;void PrintScore(longnum[], float score[], intn) ;

int main()

{

int itemSelected;int n=0;

float

score[STU_NUM];long num[STU_NUM];printf("Inputstudent

number(n<%d) :

 \n", STU_NUM) ;

scanf("%d", &n) ;while

(1)

{

itemSelected=Menu() ;scanf("%d", &itemSelected) ;

switch(itemSelected){

case

1:

 ReadScore(num, score,n) ;

break;

case

2:

 AverSumofScore(score,n) ;

break;

case

3:

 SortbyScore(num, score, n, Descending) ;printf("Sort indescending order byscore:

\n") ;PrintScore(num, score, n);

break;

case

4:

 SortbyScore(num, score, n, Ascending) ;printf("Sort inascending order byscore:

\n") ;PrintScore(num, score, n);

break;

case

5:

 AsSortbyNum(num, score, n) ;

printf("Sort inascending order bynumber:

 \n") ;PrintScore(num, score, n);

break;

case

6:

 SearchbyNum(num, score, n) ;

break;

case

7:

 StatistcAnalysis(score, n) ;

break;

case

8:

 PrintScore(num, score,n) ;

break;

case

0:

 printf("End ofprogram!

") ;exit(0) ;

default:

 printf("Inputerror!

\n") ;}

}

return0;

}

int Menu(void){

printf("Managementfor Students'scores\n") ;printf("1. Inputrecord\n") ;printf("2. Caculatetotal and average scoreof course\n") ;printf("3. Sort indescending order byscore\n") ;printf("4. Sort inascending order byscore\n") ;printf("5. Sort inascending order bynumber\n") ;printf("6. Search bynumber\n") ;printf("7. Statisticanalysis\n") ;printf("8. Listrecord\n") ;printf("0. Exit\n") ;printf("Please Inputyour choice:

 \n") ;return0;

}

void ReadScore(longnum[], float score[], intn)

{

int i;

printf("Inputstudent' s ID andscore:

\n") ;for(i=0; i

scanf("%ld%f", &num[i], &score[i]) ;}

}

void

AverSumofScore(floatscore[], int n){

int i;

float sum=0;for(i=0; i

sum=sum+score[i];}

printf("sum=%.0f, aver=%.2f\n", sum, n>0?

sum/n:

0);

}

void SortbyScore(longnum[], float score[], intn, int (*compare) (floata, float b) ){

int i, j, k;for(i=0; i

k=i;

for(j=i+1; j

if((*compare) (score[j],score[k]) ) k=j;}

if(k!

=i)

{

SwapFloat(&score[k], &score[i]) ;

SwapLong(&num[k], &num[i]) ;

}

}

}

int Ascending(floata, float b){

return a

int Descending(floata, float b){

return a>b;}

void SwapFloat(float*x, float *y){

float temp;temp = *x;

*x = *y;

*y = temp;

}

void SwapLong(long*x, long *y){

long temp;

temp = *x;

*x = *y;

*y = temp;

}

void AsSortbyNum(longnum[], float score[], intn)

{

int i, j, k;for(i=0; i

k=i;

for(j=i+1; j

if(num[j]

if(k!

=i)

{

SwapFloat(&score[k], &score[i]) ;

SwapLong(&num[k], &num[i]) ;

}

}

}

void SearchbyNum(longnum[], float score[], intn)

{

long number;int i;

printf("Input thenumber you want tosearch:

 \n") ;scanf("%ld", &number) ;for(i=0; i

if(num[i]==number){

printf("%ld\t%.0f\n", num[i], score[i]) ;return;

}

}

printf("\nNotfound!

\n") ;}

void

StatistcAnalysis(floatscore[], int n){

int

i, total, t[6]={0,0,0,0,0,0} ;

for(i=0; i

if(score[i]>=0&&score[i]<60) t[0]++;else

if(score[i]<70)t[1]++;

else

if(score[i]<80)t[2]++;

else

if(score[i]<90)t[3]++;

else

if(score[i]<100)t[4]++;

else

if(score[i]==100)t[5]++;

}

for(total=0, i=0; i<=5; i++)

{

total=total+t[i];}

for(i=0; i<=5; i++){

if(i==0) printf("<60\t%d\t%.2f%%\n", t[i], (float) t[i]/n*100) ;else if(i==5)printf("%d\t%d\t%.2f%%\n", (i+5) *10, t[i], (float) t[i]/n*100) ;else

printf("%d-%d\t%d\t%.2f%%\n", (i+5) *10, (i+5) *10+9, t[i], (float) t[i]/n*100) ;

}

}

void PrintScore(longnum[], float score[], intn)

{

int i;

for(i=0; i

printf("%ld\t%.0f\n", num[i], score[i]) ;}

}

#include #include #include #include #define MAX_LEN10/*字符串最大长度*/#define STU_NUM30/*最多的学生人数*/#define COURSE_NUM6/*最多的考试科目数*/typedef struct student{

long num;

char name[MAX_LEN];float score;} STU;

int main()

{

int a, c, i, j, k;float sum;

float aver;STU s[30], temp;long b;

char sk[10];printf("Input studentnumber(n<30) :

 \n") ;scanf("%d", &a) ;do

{

printf("Management forStudents'

scores\n1. Inputrecord\n2. Caculate totaland average score ofcourse\n3. Sort indescending order byscore\n4. Sort inascending order byscore\n5. Sort inascending order bynumber\n6. Sort indictionary order byna

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1