第九章系统安全性 例9.1利用结构体类型,编程计算一名同学5门课的平均分 main( i struct stuscore ichar name [20] float score [5] float average struct stuscore x- Wang -Wei",90.5,85,70,90,98.5 float sum=0 for(i=0;i<5;i++) sum+=x score Lil x average=sum/5 printf "The average score of %os is %04 1fn",xname, xaverage)
第九章 系 统 安 全 性 例 9.1 利用结构体类型,编程计算一名同学5门课的平均分。 main() { struct stuscore {char name[20]; float score[5]; float average; }; struct stuscore x={"Wang -Wei", 90.5,85, 70, 90, 98.5}; int i; float sum=0; for(i=0; i<5; i++) sum+=x.score[i]; x.average=sum/5; printf("The average score of %s is %4.1f\n", x.name, x.average); }
第九章系统安全性 916应用举例 例92将上例改为键入任意多人的5门课的成绩,打印平均 分 #t include <con 0.h> mainO struct stuscore ichar name [20] float score [ 5] float average X int float sum char re p while(1)
第九章 系 统 安 全 性 9.1.6 应用举例 例 9.2 将上例改为键入任意多人的5门课的成绩,打印平均 分。 #include <con.0.h> main() { struct stuscore {char name[20]; float score[5]; float average; } x; int i; float sum; char rep; while(1)
第九章系统安全性 i printf(" nDo you want to continue? (Y/N)") rep=etched if(rep==Nrep==n'break sum=0 printf("iNput name(as Xu -jun)and 5 scores(all depart by L: In") scanf(%os",xname) for(i=0;i<5;i++ scanf("%f",&xscore [i]) for(i=0;i5;i++) sum+=x score li] x average=sum/5 printf("the average score of %S is %04 1fn",xname,xaverage )
第九章 系 统 安 全 性 { printf("\nDo you want to continue?(Y/N)"); rep=getche(); if(rep==′N′ || rep==′n′) break; sum=0; printf("\nInput name(as Xu -jun) and 5 scores(all depart by ): \n"); scanf("%s", x.name); for(i=0; i<5; i++) scanf("%f", &x.score[i]); for(i=0; i<5; i++) sum+=x.score[i]; x.average=sum/5; printf("The average score of %s is %4.1f\n", x.name, x.average); } }
第九章系统安全性 运行情况为 Do you want to continue? (Y/Ny Input name(as Xu -jun)and 5 scores(all depart byL (E) Guo-Yong808959987.5664 输入) The average score of Guo -Yong is 84.4 输出) Do you want to continue? (Y/Ny Input name(as Xu -jun)and 5 scores(all depart byL): ( E) Liu-Ying8788899998 (输入) The average score of liu -Ying is 92.2 (输出) Do you want to continue? (Y/NN
第九章 系 统 安 全 性 运行情况为 Do you want to continue?(Y/N)y Input name(as Xu -jun) and 5 scores(all depart by ): ( Guo -Yong 80 89.5 99 87.5 66 The average score of Guo -Yong is 84.4 Do you want to continue?(Y/N)y Input name(as Xu -jun) and 5 scores(all depart by ): ( Liu -Ying 87 88 89 99 98 The average score of Liu -Ying is 92.2 Do you want to continue?(Y/N)N
第九章系统安全性 92结构体型数组 921结构体型数组的定义 相似于整型数组inta[3]。结构体型数组的定义不同 之处是要先定义好结构体类型。例如: struct student定义如 前,然后 struct student stu[3];就定义了一个 struct student结构体型数组stu,数组元素stu[0],stu[1],stu [2]分别是三个 struct student结构体型变量。又如: struct staff worker[100];定义了另一个结构体类型 struct staf的 数组 worker数组元素 worker[0], worker[1],…, worker [99]分别是100个 struct staff结构体型变量
第九章 系 统 安 全 性 9.2 结构体型数组 9.2.1 结构体型数组的定义 相似于整型数组 int a[3]。 结构体型数组的定义不同 之处是要先定义好结构体类型。例如:struct student 定义如 前, 然后struct student stu[3]; 就定义了一个struct student结构体型数组stu,数组元素stu[0],stu[1],stu [2]分别是三个struct student结构体型变量。又如:struct staff worker[100]; 定义了另一个结构体类型struct staff的 数组worker 数组元素worker[0],worker[1],…,worker [99]分别是100个 struct staff结构体型变量