例题
例9.1
#include<stdio.h> int main() { struct student{//声明结构体类型 int num;//结构体成员 char name[20]; char sex; char add[20]; }student_1={19,"Kakyoin",‘m’,"Harbin"};//定义结构体变量初始化 printf("学号:%d\n姓名:%s\n性别:%c\n地址:%s\n",student_1.num,student_1.name,student_1.sex,student_1.add); }
学号:19 姓名:Kakyoin 性别:m 地址:Harbin -------------------------------- Process exited after 0.004355 seconds with return value 44 请按任意键继续. . .
例9.2
#include<stdio.h> int main() { struct student{ int num; char name[20]; float score; }stu1={19,"Kakyoin",77},stu2={28,"Allams",90}; printf("成绩较高的是:\n"); if(stu1.score>stu2.score) printf("学号:%d\n姓名:%s\n分数:%6.2f",stu1.num,stu1.name,stu1.score); else if(stu1.score<stu2.score) printf("学号:%d\n姓名:%s\n分数:%6.2f",stu2.num,stu2.name,stu2.score); else { printf("学号:%d\n姓名:%s\n分数:%6.2f",stu1.num,stu1.name,stu1.score); printf("学号:%d\n姓名:%s\n分数:%6.2f",stu2.num,stu2.name,stu2.score); } }
成绩较高的是: 学号:28 姓名:Allams 分数: 90.00 -------------------------------- Process exited after 0.003391 seconds with return value 32 请按任意键继续. . .
例9.3
#include<stdio.h> #include<string.h> struct Perspon { char name[20]; int count; }leader[3]={"Li",0,"Zhang",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); return 0; }
Li Li Sun Zhang Li Sun Sun Zhang Li Li Result: Li:5 Zhang:2 Sun:3 -------------------------------- Process exited after 33.29 seconds with return value 0 请按任意键继续. . .
例9.4
#include<stdio.h> struct stu{ int num; char name[20]; int score; }; int main(){ struct stu student[4]={1,"Li",78,2,"Ze",68,3,"Hua",97,4,"Wei",96}; struct stu temp; const int n=4;//const是一个C语言的关键字,它限定一个变量不允许被改变 int i,j,k; for(i=0;i<n-1;i++)//利用冒泡法排序 { k=i; for(j=i+1;j<n;j++){ if(student[i].score>student[k].score) k=j; temp=student[k];student[k]=student[i];student[i]=temp;//把成绩高的换到前面 } for(i=0;i<n;i++) printf("%d %s %d\n",student[i].num,student[i].name,student[i].score); } return 0; }
1 Li 78 2 Ze 68 3 Hua 97 4 Wei 96 -------------------------------- Process exited after 0.00389 seconds with return value 0 请按任意键继续. . .
例9.5
#include<stdio.h> struct Student { int num; char name[20]; float score; }; int main() { struct Student stu[5]={{10101,"Zhang",78},{10103,"Wang",98.5},{10106,"Li",86}, {10108,"Ling",73.5},{10110,"Sun",100}}; struct Student temp; const int n=5; int i,j,k; printf("The order is:\n"); for(i=0;i<n-1;i++) { k=i; for(j=i+1;j<n;j++) if(stu[j].score>stu[k].score) k=j; temp=stu[k]; stu[k]=stu[i]; stu[i]=temp; } for(i=0;i<n;i++) printf("%6d %8s %6.2f\n",stu[i].num,stu[i].name,stu[i].score); printf("\n"); return 0; }
The order is: 10110 Sun 100.00 10103 Wang 98.50 10106 Li 86.00 10101 Zhang 78.00 10108 Ling 73.50 -------------------------------- Process exited after 0.00477 seconds with return value 0 请按任意键继续. . .
例9.6
#include<stdio.h> struct student{ int num; char name[20]; int age; }; struct student stu[3]={1,"Li",20,2,"Ze",25,3,"Hua",25}; int main(){ struct student *p;//定义一个struct student类型的指针 p=&stu[0];//指向数组首元素 for(p=stu;p<(stu+3);p++) printf("%d %s %d\n",(*p).num,(*p).name,(*p).age); return 0; }
1 Li 20 2 Ze 25 3 Hua 25 -------------------------------- Process exited after 0.00326 seconds with return value 0 请按任意键继续. . .
例9.7
#include<stdio.h> #define N 3 struct Student { int num; char name[20]; float score[3]; float aver; }; int main() { void input(struct Student stu[]); //定义一个struct类型的input函数,作用是从终端输入struct Student里面的内容。并且计算每个学生的各科平均成绩 struct Student max (struct Student stu[]); //定义struct类型的max函数,用来比较平均成绩。成绩高的返回 void print(struct Student stu); //调用print函数输出成绩高的学生信息 struct Student stu[N],*p = stu; input(p); print(max(p)); return 0; } void input(struct Student stu[]) { int i; printf("请输入各学生的信息:学号、姓名、三门课成绩:\n"); for(i = 0;i < N;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; int m = 0;; for(i = 0; i < N;i ++) { if(stu[i].aver > stu[m].aver) { m = i; } } return stu[m]; } void print(struct Student stud) { printf("\n 成绩最高的学生:\n"); printf("学号:%d\n",stud.num); printf("姓名:%s\n",stud.name); printf("三门课成绩:%5.1f,%5.1f,%5.1f\n",stud.score[0],stud.score[1],stud.score[2]); printf("平均成绩:%6.2f\n",stud.aver); }
请输入各学生的信息:学号、姓名、三门课成绩: 1 Li 36 89 77 2 Ze 77 98 90 3 Hua 87 99 23 成绩最高的学生: 学号:2 姓名:Ze 三门课成绩: 77.0, 98.0, 90.0 平均成绩: 88.33 -------------------------------- Process exited after 852.9 seconds with return value 0 请按任意键继续. . .
总结:抄写例题的过程让我对结构与结构数组有了跟深入的了解,掌握程度也更高了。复制运行结果的时候全选以后按Ctrl+C总是失败,后来发现回车更好用,(笑)。