例 9.1:
#include<stdio.h> struct Student { long int num; char name[10]; char sex[3]; char address[20]; } stu={1001,"张三","男","哈尔滨"}; int main() { printf("学号=%d\n姓名=%s\n性别=%s\n地址=%s\n",stu.num ,stu.name ,stu.sex ,stu.address ); }
学号=1001
姓名=张三
性别=男
地址=哈尔滨
--------------------------------
Process exited after 0.01108 seconds with return value 0
请按任意键继续. . .
例 9.2:
#include<stdio.h> #include<string.h> struct Student { int num; char name[10]; float score; } ; int main() { struct Student student1,student2; printf("请输入两位同学的信息:\n"); scanf("%d %s %f",&student1.num ,student1.name ,&student1.score ); scanf("%d %s %f",&student2.num ,student2.name ,&student2.score ); printf("成绩最高的学生信息:\n"); if(student1.score >student2.score ) { printf("num=%d\nname=%s\nscore=%f\n",student1.num ,student1.name ,student1.score ); } if(student1.score <student2.score ) { printf("num=%d\nname=%s\nscore=%f\n",student2.num ,student2.name ,student2.score ); } }
请输入两位同学的信息:
1001 张三 69
1002 李四 78
成绩最高的学生信息:
num=1002
name=李四
score=78.000000
--------------------------------
Process exited after 16.38 seconds with return value 0
请按任意键继续. . .
例 9.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 ++; } } printf("统计成功!\n"); for(i=0;i<3;i++) { printf("%s:%d\n",person[i].name ,person[i].count ); } }
请输入您选择的候选人:张三
请输入您选择的候选人:李四
请输入您选择的候选人:王五
请输入您选择的候选人:王五
请输入您选择的候选人:12
统计成功!
张三:1
李四:1
王五:2
--------------------------------
Process exited after 19.8 seconds with return value 0
请按任意键继续. . .
例 9.4:
#include<stdio.h> #include<string.h> struct STD { int number; char name[10]; int score; }; int main() { struct STD stu[5]={{1001,"张三",65},{1002,"李四",75},{1003,"王五",67},{1004,"马六",87},{1005,"羊七",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 马六 87
1002 李四 75
1003 王五 67
1001 张三 65
1005 羊七 61
--------------------------------
Process exited after 0.007641 seconds with return value 13
请按任意键继续. . .
例 9.5:
#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 =76; printf("学号=%d\n姓名=%s\n性别=%s\n成绩=%d\n",stu.num ,stu.name ,stu.sex ,stu.score ); printf("学号=%d\n姓名=%s\n性别=%s\n成绩=%d\n",p->num ,p->name ,p->sex ,p->score ); }
学号=1001
姓名=张三
性别=男
成绩=76
学号=1001
姓名=张三
性别=男
成绩=76
--------------------------------
Process exited after 0.007976 seconds with return value 36
请按任意键继续. . .
例 9.6:
#include<stdio.h> #include<string.h> struct STD { int num; char name[20]; char sex[3]; int score; }; int main() { struct STD stu[3]={{1001,"张三","男",68},{1002,"小红","女",76},{1003,"小明","男",79}},*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 ); } }
学号 姓名 性别 成绩
1001 张三 男 68
1002 小红 女 76
1003 小明 男 79
--------------------------------
Process exited after 0.01121 seconds with return value 10485312
请按任意键继续. . .