于金池 1965976232@qq.com
例9.1:
#include<stdio.h> int main() {struct Student //声明结构体类型struct Student { long int num; //以下四行为结构体的成员 char name[20]; char sex; char addr[20]; }w={01,"Yu Jinchi",'M',"Beijing Road"}; //定义结构体变量a并初始化 printf("NO.:%ld\nname:%s\nsex:%C\naddress:%s\n",w.num,w.name,w.sex,w.addr); //输出结构体所有成员,也就是学生的所有信息 return 0; }
NO.:1
name:Yu Jinchi
sex:M
address:Beijing Road
--------------------------------
Process exited after 0.104 seconds with return value 0
请按任意键继续. . .
【总结】:这是一个简单的结构体类型题,只需要在定义一个结构体后面定义一个结构体变量并且初始化,并且在后面按顺序输出所有信息即可,在运行过程中没有特别大的错误。
例9.2:
#include<stdio.h> int main() { struct Student //声明结构体类型 struct Student { int num; char name[20]; float score; }student1,student2; //定义两个结构体变量student1,student2 scanf("%d %s %f",&student1.num,student1.name,&student1.score); //输入学生1的数据 scanf("%d %s %f",&student2.num,student2.name,&student2.score); //输入学生1的数据 printf("The higher score is:\n"); //输出最高分 if(student1.score>student2.score) //用if语句进行大小比较,输出分数较大者 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); } return 0; }
01 yujinchi 666
02 pipixia 665
The higher score is:
1 yujinchi 666.00
--------------------------------
Process exited after 53.43 seconds with return value 0
请按任意键继续. . .
【总结】: 先定义一个结构体和两个结构体变量,然后再输入两个结构体变量的相关信息,输出两者之间的最高分,通过if语句进行比较,在编程的时候没有全面考虑到比较可能出现的情况。
例9.3:
#include<stdio.h> #include<string.h> struct Person //声明结构体类型struct Person { 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
zhabg
Sun
Li
Sun
Zhang
Li
Result:
Li:4
Zhang:2
Sun:3
--------------------------------
Process exited after 77.16 seconds with return value 0
请按任意键继续. . .
【总结】:在编程过程中运用了strcmp这样比较函数大小的函数,并且运用了for循环巧妙的计算出了得票数量,最后用result:输出编译的结果。
例9.4:
#include<stdio.h> struct Student 声明结构体类型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; //定义结构体变量temp,用作交换时的临时变量 const int n=5; //定义常变量n 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) //用if语句进行程序的比较 k=j; temp=stu[k];stu[k]=stu[i];stu[i]=temp; //stu[k]和stu[i]元素互换 } 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 Sun100.00
10103 Wang 98.50
10106 Li 86.00
10101 Zhang 78.00
10108 Ling 73.50
--------------------------------
Process exited after 0.1687 seconds with return value 0
请按任意键继续. . .
【总结】:用if语句进行成绩的比较,用temp函数进行函数值的交换,最后按比较后的顺序输出所有的学生信息.在编程的时候循环处产生了错误.后来经过改正,最后编译运行正确。
例题9.5:.
#include<stdio.h> #include<string.h> int main() {struct Student {long num; char name[20]; char sex; float score; }; struct Student stu_1; //定义struct Student类型的变量stu_1 struct Student*p; //定义指向struct Student类型数据的指针变量p p=&stu_1; //p指向stu_1 stu_1.num=10101; //对结构体变量的成员赋值 strcpy(stu_1.name,"Li Lin"); //用字符串复制函数给stu_1.name赋值 stu_1.sex='M'; stu_1.score=89.5; printf("No.:%ld\nname:%s\nsex:%c\nscore:%5.1f\n",stu_1.num,stu_1.name,stu_1.sex,stu_1.score) ; printf("\nNo.:%ld\nname:%s\nsex:%c\nscore:%5.1f\n",(*p).num,(*p).name,(*p).sex,(*p).score) ; //输出结果 return 0; }
No.:10101
name:Li Lin
sex:M
score: 89.5
No.:10101
name:Li Lin
sex:M
score: 89.5
--------------------------------
Process exited after 0.1887 seconds with return value 0
请按任意键继续. . .
【总结】:定义结构体变量stu-1,和指针p并且给结构体变量stu-赋初值,最后以变量stu-1.的形式和指针p两种形式输出该学生的所有信息。编程过程中没有什么过大的错误.
例9.6:
#include<stdio.h> struct Student //声明结构体类型struct Student {int num; char name[20]; char sex; int age; } ; struct Student stu[3]={{10101,"Mi Gui",'M',21 },{10102,"Mai Pipixia",'M',36},{10104,"Yu Kang",'F',27}}; //定义结构体数组并初始化 int main() {struct Student *p; //定义指向struct Student结构体变量的指针变量 printf("No.Name sex age\n"); for(p=stu;p<stu+3;p++) printf("%5d %-15s %2c %4d\n",p->num,p->name,p->sex,p->age); //输出结果 return 0; }
No.Name sex age
10101 Mi Gui M 21
10102 Mai Pipixia M 36
10104 Yu Kang F 27
--------------------------------
Process exited after 0.1269 seconds with return value 0
请按任意键继续. . .
【总结】:定义结构体数组和一个指针并且让指针指向结构体数组的首个结构体的地址,之后利用for循环令p依次指向数组的第一个第二个第三个元素,最后按照No.name sex age的格式输出结构数组中三个结构体元素的所有信息。在运行过程中由于在最后输出时的格式长度不是十分恰当,最后经过改正令No。name sex age和下面正好对应.其余没有什么大错误。
例题9.7:
#include<stdio.h> #define N 3 //学生数为3 struct Student //建立结构体类型struct Student { int num; //学号 char name[20]; //姓名 float score[3]; //3门课成绩 float aver; //平均成绩 }; int main() { void input(struct Student stu[]); //函数声明 struct Student max(struct Student stu[]); //函数声明 void print(struct Student stu); //函数声明 struct Student stu[N],*p=stu; //定义结构体数组和指针 input(p); //调用input函数 print(max(p)); //调用print函数,以max函数的返回值作为实参 return 0; } void input (struct Student stu[]) //定义input函数 {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[]) //定义max函数 {int i,m=0; //用m存放成绩最高的学生在数组中的序号 for(i=0;i<N;i++) if(stu[i].aver>stu[m].aver)m=i; //找出平均成绩最高的学生在数组中的序号 return stu[m]; //返回包含该生信息的结构体元素 } void print(struct Student stud) //定义print函数 {printf("\n成绩最高的学生是:\n"); printf("学号:%d\n姓名:%s\n三科成绩:%5.1f,%5.1f,%5.1f\n平均成绩:%6.2f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver); }
输入各学生信息:学号,姓名,三科成绩:
10 Mi 15 32 14
01 Po 56 10 03
10 Mi 15 32.5 14
成绩最高的学生是:
学号:1
姓名:Po
三科成绩: 56.0, 10.0, 3.0
平均成绩: 23.00
--------------------------------
Process exited after 95.79 seconds with return value 0
请按任意键继续. . .
【总结】:这个程序比较复杂,需要定义一个含有结构体数组作为形参的input的空型函数,并且在这个函数里面输入各学生数据并且求出平均成绩,然后再定义一个max函数求出平均成绩最高的学生的序号,然后再定义一个void类型函数,输出最高成绩的学生的各项信息。一共定义了三个函数除了主函数,分别用来求出各学生平均成绩,在求出平均成绩最高的学生,再输出最高学生的全部信息,在编写程序过程中,函数的声明有的被遗漏了,而且前后不一致,导致定义的函数无法进行正常使用。