上传截图
书中例题代码及运行结果:
☛例9.1
把一个学生的信息(包括学号、姓名、性别、住址)放在一个结构体变量中,然后输出这个学生的信息
#includeint main() { struct Student { long int num; char name[20]; char sex; char addr[20]; } a = { 10101, "Li Lin", 'M', "123 Beijng Road" }; printf("NO.:%1d\nname:%s\nsex:%c\naddress:%s\n", a.num ,a.name,a.sex,a.addr); return 0; }
/*NO.:10101
name:Lin
sex:M
address:123 Beijing Road
请按任意键继续. . .
*/
此处又忘了要给数组赋初值,后来中括号里加了20...
9.2.输入2个学生的学号,姓名和成绩,输出成绩较高的学生的学号,姓名和成绩。
#includeint main() { struct student { int num; char name[20]; float score; } student1,student2; scanf("%d%s%f", &student1.num, &student1.name, &student1.score); scanf("%d%s%f", &student2.num, &student2.name, &student2.score); printf("The higher score is:\n"); if (student1.score > student2.score) printf("%d %s %6.2f\n", student1.num, student1.name, student1.score); else if (student1.score < student2.score) printf("%d %s %6.2f\n", student2.num, student2.name, student2.score); else { printf("%d %s %6.2f\n", student1.num, student1.name, student1.score); printf("%d %s %6.2f\n", student2.num, student2.name, student2.score); } } /*10101 Wang 89 10103 Lin 90 The higher score is: 10103 Lin 90.00 请按任意键继续. . . */
这个没出什么问题
9.3
有3个候选人,每个选民只能选投票一人,要求编一个统计选票的程序,先后输入被选人的名字,最后输出各人得票结果。
#includestruct person { char name[20]; int count; } leader[3] = {"li",0, "zhan",0,"sun",0}; int main() { int i, j; char leader_name[20]; for (i = 1; i <= 10;i++) { scanf("%s", leader_name); for ( j = 0; j < 3; j++) if (strcmp(leader_name, leader[j].name) == 0) leader[j].count++; } printf("\nResult:\n"); for (i = 0; i < 3; i++) printf("%5s:%d\n", leader[i].name,leader[i].count); } /*li li sun zhan zhan sun li sun zhan li Result: li:4 zhan:3 sun:3 请按任意键继续. . . */
开始不知道如何比较字符串,后来研究知道如果想记录每个人所得的票数,就需要知道是不是同一个人,后来改了过来。。
9.4
有n个学生的信息(包括学号,姓名,成绩),要求按照成绩的高低顺序输出各学生的信息
1 #include2 struct stu 3 { 4 long int num; 5 char name[20]; 6 float score; 7 }; 8 int main() 9 { 10 struct stu t; 11 struct stu s[5]={10101,"zhang",78,10103,"wang",98.5,10106,"li",86,10108,"ling",73.5,10100,"sun",100 }; 12 for(int i=0;i<5;i++){ 13 for(int j=1;j<5-i;j++) 14 { 15 if(s[j].score>s[j-1].score) 16 { 17 t=s[j]; 18 s[j]=s[j-1]; 19 s[j-1]=t; 20 } 21 } 22 } 23 for(int i=0;i<5;i++) 24 { 25 printf("%d%8s%8.2f\n",s[i].num,s[i].name,s[i].score); 26 } 27 }
10100 sun 100.00
10103 wang 98.50
10106 li 86.00
10101 zhang 78.00
10108 ling 73.50
--------------------------------
Process exited after 0.46 seconds with return value 0
请按任意键继续. . .
这题还好,没出什么差错
例9.5
通过指向结构体变量的指针变量来输出结构体中成员的信息。
#include#include <string.h> int main() { struct student { long num; char name[20]; char sex; float score; }; struct student stu1; struct student *p; p = &stu1; stu1.num = 10101; strcpy(stu1.name, "Li Lin"); stu1.sex = 'M'; stu1.score = 89.5; printf("No.:%1d\nname:%s\nsex:%c\nscore:%5.1f\n",stu1.num,stu1.name,stu1.sex,stu1.score); printf("\nNo.:%1d\nname:%s\nsex:%c\nscore:%5.1f\n", (*p).num, (*p).name, (*p).sex, (*p).score); } /*No.:10101 name:Li Lin sex:M score: 89.5 No.:10101 name:Li Lin sex:M score: 89.5 请按任意键继续. . . */
9.6
有n个结构体变量,内含学生学号,姓名和3门课程成绩。要求输出平均成绩最高的学生的信息(包括学号,姓名,3门课程成绩和平均成绩)。
#includestruct student { int num; char name[20]; char sex; int age; }; struct student stu[3] = { { 10101, "Li Lin", 'M', 18 }, { 10102, "Zhang Fang", 'M', 19 }, { 10103, "Wang Min", 'F', 20 } }; int main() { struct student *p; p = stu; printf("No. Name sex age\n"); for (; p < stu + 3; p++) printf("%5d %-20s%2c%4d\n", p->num, p->name, p->sex, p->age); } /*No. Name sex age 10101 Li Lin M 18 10102 Zhang Fang M 19 10103 Wang Min F 20 请按任意键继续. . . */
还是指针出了问题,指错了,后来废了好长时间才改成struct student *p
例9.7 有n个结构体变量,内含学生学号,姓名和3门课程成绩。要求输出平均成绩最高的学生的信息(包括学号,姓名,3门课程成绩和平均成绩)。
#includestruct student { int num; char name[20]; float score[3]; float aver; }; void input(struct student stu[]) { int i; printf("请输入各学生的信息:学号,姓名,三门课成绩:\n"); for (i = 0; i < 3; i++){ scanf("%d%s%f%f%f", &stu[i].num, &stu[i].name, &stu[i].score[0], &stu[i].score[1], &stu[i].score[2]); stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2]) / 3.0; } } struct student max(struct student stu[]) { int i, m = 0; for (i = 1; i<3; i++) if (stu[i].aver>stu[m].aver) m = i; return stu[m]; } void print(struct student stud) { printf("\n成绩最高的学生是:\n"); printf("学号:%d\n姓名: %s\n三门课成绩: %5.1f ,%5.1f,%5.1f\n平均成绩:%5.1f\n", stud.num, stud.name, stud.score[0], stud.score[1], stud.score[2], stud.aver); } int main() { //void input (struct student stu[]); //struct student max(struct student stu[]); //void print(struct student stu); struct student stu[3], *p = stu; input(p); print(max(p)); return 0; } /*请输入各学生的信息:学号,姓名,三门课成绩: 10101 Li 78 89 98 10103 Wang 98.5 87 69 10106 Sun 88 76.5 89 成绩最高的学生是: 学号:10101 姓名: Li 三门课成绩: 78.0 , 89.0, 98.0 平均成绩: 88.3 请按任意键继续. . . */
函数调用出错了,不是int
总结:1.通过这次抄写,让我意识到很多知识已经忘了,下一步一定着重复习这次产生的问题.
2.敲代码更熟练了,哈