例题作业
#include<stdio.h> struct student { int num; char name[20]; char sex; char add[20]; }stu={100,"王",'m',"dajian"}; int main() { printf("%d,%s,%c,%s",stu.num,stu.name,stu.sex,stu.add); }
100,王,m,dajian -------------------------------- Process exited after 0.5513 seconds with return value 15 请按任意键继续. . .
例题2
#include<stdio.h> int 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 %f\n",student1.num,student1.name,student1.score); else if(student1.score<student2.score) printf("%d %s %f\n",student2.num,student2.name,student2.score); else { printf("%d %s %f\n",student1.num,student1.name,student1.score); printf("%d %s %f\n",student2.num,student2.name,student2.score); } return 0; }
11 wang 88 22 li 99 The higher score is : 22 li 99.000000 -------------------------------- Process exited after 11.22 seconds with return value 0 请按任意键继续. . .
3
#include<string.h> #include<stdio.h> struct Person { char name[20]; int conut; }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].conut ++; } } printf("\nResult:\n"); for(i=0;i<3;i++) printf("%5s:%d\n",leader[i].name,leader[i].conut ); return 0; }
ZHANG LI LI LI LI LI ZHANG ZHANG ZHANG LI Result: LI:6 ZHANG:4 SUN:0 -------------------------------- Process exited after 25.63 seconds with return value 0 请按任意键继续. . .
4
#include<stdio.h> #include<stdlib.h> struct Student { int num; char name[20]; float score; }; int main() { struct Student stu[5]={{101,"Wang",88},{102,"wan",78},{103,"li",96},{104,"ling",73},{105,"YUn",80}}; 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("%d,%s,%d",stu[i].num,stu[i].name,stu[i].score); printf("\n"); return 0; }
the order is: 103,li,0101,Wang,0105,YUn,0102,wan,0104,ling,0 -------------------------------- Process exited after 2.245 seconds with return value 0 请按任意键继续. . .
5
#include<stdio.h> #include<string.h> struct STD { int num; char name[20]; char sex[3]; int score; }stu[3]={{10,"yu","m",98},{33,"wang","w",99},{76,"li","m",98}}; int main() { struct STD *p; p=stu; printf("学号\t姓名\t性别\t成绩\n"); for(;p<stu+3;p++) { printf("%d\t%s\t%s\t%d\n",p->num ,p->name ,p->sex ,p->score ); } }
学号 姓名 性别 成绩 10 yu m 98 33 wang w 99 76 li m 98 -------------------------------- Process exited after 2.905 seconds with return value 4206752 请按任意键继续. . .
6
#include<stdio.h> #include<string.h> struct STD { int num; char name[20]; char sex[3]; int score; }; int main() { struct STD stu,*p; p=&stu; stu.num =4; strcpy(stu.name ,"王大鲣"); strcpy(stu.sex ,"M"); stu.score =80; printf("学号=%d\n姓名=%s\n性别=%s\n成绩=%d\n",p->num ,p->name ,p->sex ,p->score ); }
学号=4 姓名=王大鲣 性别=M 成绩=80 -------------------------------- Process exited after 0.2319 seconds with return value 34 请按任意键继续. . .
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 Student max (struct Student stu[]); void print(struct Student stu); 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); }
请输入各学生的信息:学号、姓名、三门课成绩: 11 wang 98 99 99 12 li 78 98 77 22 huai 88 88 77 成绩最高的学生: 学号:11 姓名:wang 三门课成绩: 98.0, 99.0, 99.0 平均成绩: 98.67 -------------------------------- Process exited after 38.59 seconds with return value 0 请按任意键继续. . .
总结:例题虽说有正确的模板,但多练习总没有错。
地址:http://git.oschina.net/wdx13/liti