例题作业
#include<stdio.h> struct student { int num; char name[20]; char sex; char add[20]; }stu={1,"zhang",'W',"siyu"}; int main() { printf("%d,%s,%c,%s",stu.num,stu.name,stu.sex,stu.add); }
1,zhang,W,siyu -------------------------------- Process exited after 0.5593 seconds with return value 0 请按任意键继续. . .
总结:
這个程序还算可以,没有太多难度,也巩固了一下自己的知识点。
例题2.
#include<stdio.h> struct Student { int num; char name[20]; int score; }std1,std2; int main() { printf("请输入成绩:\n"); scanf("%d %s %d",&std1.num,std1.name,&std1.score); scanf("%d %s %d",&std2.num,std2.name,&std2.score); printf("输出成绩最高同学信息\n"); if(std1.score<std2.score) printf("%d,%s,%d\n",std2.num,std2.name,std2.score); else { if(std1.score>std2.score) printf("%d,%s,%d\n",std1.num,std1.name,std1.score); else printf("%d,%s,%d\n",std2.num,std2.name,std2.score); } return 0; }
请输入成绩: 1 卡 66 2 期 99 输出成绩最高同学信息 2,期,99 -------------------------------- Process exited after 33.03 seconds with return value 0 请按任意键继续. . .
总结:
这个程序稍有些难度,但是在运行时没有出现什么不能理解的错误。
例题3.
#include<stdio.h> #include<string.h> struct Person { char name[20]; int count; } ; int main() { struct Person person[3]={"张",0,"思",0,"宇",0}; int i,j; char name[10]; for(i=0;i<5;i++) { printf("请输入选择的人:"); scanf("%s",name); for(j=0;j<3;j++) { if(strcmp(name,person[j].name )==0) person[j].count ++; } } for(i=0;i<3;i++) { printf("%s:%d\n",person[i].name ,person[i].count ); } }
请输入选择的人:张 请输入选择的人:张 请输入选择的人:张 请输入选择的人:思 请输入选择的人:思 张:3 思:2 宇:0 -------------------------------- Process exited after 12.6 seconds with return value 0 请按任意键继续. . .
这个程序在票数累加时第三个for循环总是输出不对,原因:i的值在第一个for循环后未重新定义;
例题4.
#include<stdio.h> #include<string.h> struct STD { int number; char name[10]; int score; }; int main() { struct STD stu[5]={{1001,"张",99},{1002,"思",100},{1003,"雨",97},{1004,"李",98},{1005,"王",96}}; struct STD temp; struct STD *p; p=stu; int i,j; for(j=0;j<4;j++) { for(i=0;i<4;i++,p++) { if(stu[i].score <stu[i+1].score) { temp=stu[i] ; stu[i] =stu[i+1] ; stu[i+1]=temp; } } } for(i=0;i<5;i++) { printf("%d\t%s\t%d\n",stu[i].number ,stu[i].name ,stu[i].score ); } }
1002 思 100 1001 张 99 1004 李 98 1003 雨 97 1005 王 96 -------------------------------- Process exited after 0.4441 seconds with return value 0 请按任意键继续. . .
总结:
这个程序在输出的表格时忘了\t,输了几遍都不对,最后在书上找到的,i的值在输出一遍的时候才反应过来要重新赋值。
例题5.
#include<stdio.h> #include<string.h> struct STD { int num; char name[20]; char sex[3]; int score; }; int main() { struct STD stu; struct STD *p; p=&stu; stu.num =1; strcpy(stu.name ,"张"); strcpy(stu.sex ,"M"); stu.score =76; printf("正常输出:"); printf("学号=%d 姓名=%s 性别=%s 成绩=%d\n",stu.num ,stu.name ,stu.sex ,stu.score ); printf("以p-<输出:\n"); printf("学号=%d 姓名=%s 性别=%s 成绩=%d\n",p->num ,p->name ,p->sex ,p->score ); }
正常输出:学号=1 姓名=张 性别=M 成绩=76 以p-<输出: 学号=1 姓名=张 性别=M 成绩=76 -------------------------------- Process exited after 1.358 seconds with return value 0 请按任意键继续. . .
总结:
这个程序还可以,就是一个固定输出。没有出现问题。
例题9.6
#include<stdio.h> struct Student { int num; char name[20]; char sex; int age; }stu[3]={{10101,"lilin",'M',18},{10101,"zhang fang",'M',19},{10104,"wang min",'F',20}},*p; int main() { printf("No. Name sex age\n"); for(p=stu;p<stu+3;p++) printf("%5d %-20s %2c %4d\n",p->num,p->name,p->sex,p->age); return 0; }
No. Name sex age 10101 lilin M 18 10101 zhang fang M 19 10104 wang min F 20 -------------------------------- Process exited after 0.3117 seconds with return value 0 请按任意键继续. . .
总结:
指针指向数组,正常输出,未出现错误。
例题9.7
#include<stdio.h> #define N 3 void input(struct Student stu[]); struct Student max(struct Student stu[]); void print(struct Student stu); struct Student { int num; char name[20]; float score[3]; float aver; }; int main() { struct Student stu[N],*p=stu; input(p); print(max(p)); return 0; } void input(struct Student stu[]) { int i=0; printf("请输入各学生的信息:学号、姓名、三门课成绩"); for(;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]); } } struct Student max(struct Student stu[]) { int i,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输出成绩最高学生信息:"); 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 w 66 999 888 11 j 66 88 75 12 o 99 87 33 输出成绩最高学生信息:学号:10 姓名:w 三门课成绩: 66.0,999.0,888.0 平均成绩:1953.00 -------------------------------- Process exited after 92.89 seconds with return value 0 请按任意键继续. . .
总结:
这个程序是照书抄的,不是自己打的,感觉不是很顺,我的会更加繁琐一些。