例题抄写
#include<stdio.h> #include<stdlib.h> main() { struct student {int num;//学号为整形 char name[20];//姓名为字符串 char sex;//性别为字符串 int age;//年龄为整形 float score;//成绩为实型 char addr[30]; //地址为字符串 };//注意最后有一个分号 }
-------------------------------- Process exited after 0.1302 seconds with return value 1 请按任意键继续. . .
#include<stdio.h> int main() { struct stu { long int num; char name[20]; char sex[3]; char addr[20]; }a={24,"李清娜","女","dgh"}; printf("No:%ld\nname:%s\nsex:%c\naddress:%s\n",a.num,a.name,a.sex,a.addr); }
No:24 name:李清娜 sex:女 address:dqh -------------------------------- Process exited after 0.1287 seconds with return value 0 请按任意键继续. . .
#include<stdio.h> 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",student1.num,student1.name,student1.score); else if(student1.score<student2.score) printf("%d,%s,%f",student2.num,student2.name,student2.score); else {printf("%d,%s,%f",student1.num,student1.name,student1.score); printf("%d,%s,%f",student2.num,student2.name,student2.score); } return 0; }
1 w 852 p 96
The higher score is:
2 p 96.00
--------------------------------
Process exited after 0.2245 seconds with
return
value 13
请按任意键继续. . .
#include<string.h> #include<stdio.h> struct person { char name[20]; int count; }leader[3]={"liying",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); }
The order is: 10110 sun 100.00 10103 wang 98.50 10106 liying 86.00 10101 zhang 78.00 10108 ling 73.50 -------------------------------- Process exited after 0.2378 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 stu1; struct student *p; p=&stu1; stu1.num=10101; strcpy(stu1.name, "Li Lin"); stu1.sex='M'; stu1.score=89.5; printf("No.:%1d\nname:%s\nsex:%c\nscore:%5.1f\n",stu1.num,stu1.name,stu1.sex,stu1.score); printf("\nNo.:%1d\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.1261 seconds with return value 0
请按任意键继续. . .
#include <stdio.h> struct student { int num; char name[20]; char sex; int age; }; struct student stu[3]={{10101,"Li Lin",'M',18},{10102,"Zhang Fang",'M',19},{10103,"Wang Min",'F',20}}; int main() { struct student *p; p = stu; 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); }
No. Name sex age 10101 Li Lin M 18 10102 Zhang Fang M 19 10103 Wang Min F 20 -------------------------------- Process exited after 0.1379 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\ÐÕÃû£º%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); }
请输入各学生的信息:学号,姓名,三门课成绩: 01 li 76 87 76 02 zhang 98 87 78 03 liu 65 87 98 成绩最好的学生是: 学号:2姓名:zhang 三门课成绩: 98.0, 87.0, 78.0 平均成绩: 87.67 -------------------------------- Process exited after 31.16 seconds with return value 0 请按任意键继续. . .