抄写例题
#include<stdio.h> main() { struct Student { long int num;//学号为整形 char name[20];//姓名为字符串 char sex;//性别为字符串 char addr[30]; //地址为字符串 }a={10101,"Li Lin",'M',"123 Beijing Road"}; printf("No.:%1d\nname:%s\nsex:%c\naddress:%s\n",a.num,a.name,a.sex,a.addr); return 0; }
No.:10101 name:Li Lin sex:M address:123 Beijing Road -------------------------------- Process exited after 0.1168 seconds with return value 0 请按任意键继续. . .
#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); //输入学生2的数据 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); } return 0; }
10101 Wang 89 10103 Ling 90 The higher score is: 10103 Ling 90.00 -------------------------------- Process exited after 27.45 seconds with return value 0 请按任意键继续. . .
#include<string.h> #include<stdio.h> struct Person { char name[20]; int conut; }leader[3]={"li",0,"zhang",0,"liu",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; }
Li Li Liu Zhang Zhang Liu Li Liu Zhang Li Result: li:0 zhang:0 liu:0 -------------------------------- Process exited after 66.5 seconds with return value 0 请按任意键继续. . .
#include<stdio.h> #include<string.h> struct STD { int number; char name[10]; int score; }; int main() { struct STD stu[5]={{1001,"zhang",65},{1002,"li",75},{1003,"liu",67},{1004,"wang",87},{1005,"tian",61}},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 ); } }
1004 wang 87 1002 li 75 1003 liu 67 1001 zhang 65 1005 tian 61 -------------------------------- Process exited after 0.1332 seconds with return value 0 请按任意键继续. . .
#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 *p; p=&stu_1; stu_1.num=10101; strcpy(stu_1.name,"li lin"); stu_1.sex='M'; stu_1.score=89.5; printf("No.:%1d\nname:%s\nsex:%c\nscore:5.1%f\n",stu_1.num,stu_1.name,stu_1.sex,stu_1.score); printf("\nNo.:%1d\nname:%s\nsex:%c\nscore:5.1%f\n",(*p).num,(*p).name,stu_1.sex,(*p).score); return 0; }
No.:10101 name:li lin sex:M score:5.189.500000 No.:10101 name:li lin sex:M score:5.189.500000 -------------------------------- Process exited after 0.4192 seconds with return value 0 请按任意键继续. . .
#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 =1001; strcpy(stu.name ,"张"); strcpy(stu.sex ,"女"); stu.score =80; printf("学号=%d\n姓名=%s\n性别=%s\n成绩=%d\n",p->num ,p->name ,p->sex ,p->score ); }
学号=1001 姓名=张 性别=女 成绩=80 -------------------------------- Process exited after 0.4291 seconds with return value 0 请按任意键继续. . .
#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,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姓名:%s\n三门课成绩:%5.1f,%5.1f,%5.1f平均成绩:%6.2f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver); }
冉三废 2017/04/09 14:51:16 请输出各学生的信息:学号、姓名、三门课成绩: 10101 li 78 89 98 10103 wang 98.5 87 69 10106 sun 88 76.5 89 成绩最高的学生是: 学号:10101 姓名:10485108 三门课成绩: 78.0, 89.0, 98.0 平均成绩: 88.33 -------------------------------- Process exited after 99.13 seconds with return value 0 请按任意键继续. .
总结:我发现这个结构体的内容差不多相同,相差不大,编程要多练习,我抄写的时候会忘记符号,多注意细节。嗯,以后多练习。