首页 > 代码库 > 例题编写
例题编写
例9.1程序
#include<stdio.h> #include<stdlib.h> int main() { struct student { long int num; char name[20]; char sex; char addr[20]; } a={1,"sun",‘m‘,"123 beijing road"}; printf("NO.:%ld\n name:%s\n sex:%c\n addr:%s\n",a.num,a.name,a.sex,a.addr); return 0; }
运行结果
NO.:1 name:sun sex:m addr:123 beijing road -------------------------------- Process exited after 0.006787 seconds with return value 0 请按任意键继续. . .
例9.2
#include<stdio.h> #include<stdlib.h> int main() { struct student { long 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 %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; }
运行结果:
1 孙 89 2 刘 90 The higher score is: 2 刘 90.00 -------------------------------- Process exited after 78.92 seconds with return value 0 请按任意键继续. . .
例9.3:
#include<stdio.h> #include<stdlib.h> #include<string.h> struct person { char name[20]; int count; }leader[3]={"li",0,"sun",0,"zhang",0}; int main() {int i,j; char leader_name[20]; for(i=0;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("\n Result:\n"); for(i=0;i<3;i++) printf("%5s:%d\n",leader[i].name,leader[i].count); return 0; }
运行结果:
sun li liu sun li li liu sun li lisun li Result: li:5 sun:3 zhang:0 -------------------------------- Process exited after 2.568 seconds with return value 0 请按任意键继续. . .
例9.4:
例题编写
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。