定义和使用结构体变量---恶补之五
2016-09-22 11:53
183 查看
C语言允许用户建立由不同类型数据组成的组合型的数据结构,它称为结构体(structure)
structrue Student
{ int num;
char name[20];
…………
};
structure 结构体名
{成员列表};(变量名表列)
注:当structrt Student b = {.name=”Zhang Fang”};
其他未被初始化的数值型成员初始化为0,字符型初始化为‘\0’,指针型成员初始化为NULL。
//”.”运算符的优先级最高
例9.2 输入两个学生的学号、姓名和成绩,输出较高成绩学生的信息。
例9.3
有3个候选人,每个选民只能投一票选一人,要求编一个统计投票的程序,先后输入备选人的名字,最后输出各人得票结果。
structrue Student
{ int num;
char name[20];
…………
};
structure 结构体名
{成员列表};(变量名表列)
注:当structrt Student b = {.name=”Zhang Fang”};
其他未被初始化的数值型成员初始化为0,字符型初始化为‘\0’,指针型成员初始化为NULL。
//”.”运算符的优先级最高
例9.2 输入两个学生的学号、姓名和成绩,输出较高成绩学生的信息。
#include<stdio.h> int 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,%6.2f",student1.num,student1.name.student1.score); else if(student2.score>student1.score) printf("%d,%s,%6.2f",student2.num,student2.name.student2.score); else { printf("%d,%s,%6.2f",student1.num,student1.name.student1.score); printf("%d,%s,%6.2f",student2.num,student2.name.student2.score); } return 0; }
例9.3
有3个候选人,每个选民只能投一票选一人,要求编一个统计投票的程序,先后输入备选人的名字,最后输出各人得票结果。
#include<stdio.h> struct Person { char name[20]; int count; }leader[3]={"Li",0,"zhang",0,"sun",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("\nResult:\n"); for(i=0;i<3;i++) { printf("%5s:%d\n",leader[i].name,leader[i].count); } return 0; }
相关文章推荐
- oracle函数、包、变量的定义和使用、重点”结构体和数组”
- typedef定义的struct类型与struct定义的结构体变量在使用上的区别
- 有如下CAT_s结构体定义,回答: 1) 在一台64位的机器上,使用32位编译,Garfield变量占用多少内存空间?64位编译又是如何?
- JSP预定义变量的使用
- 结构体的定义以及使用
- 在ASP.net中如何定义和使用全局变量
- javascript中的变量如果没有定义就使用的话
- 如何删除使用export定义过的变量?
- 在vim中使用cscope查找调用、定义函数和变量的地方
- MFC全局变量的定义和使用
- VC++中全局变量、全局常量、全局结构体的定义
- 全局变量声明,定义与使用方法
- JSP入门初级教程之预定义变量的使用
- 全局变量声明,定义与使用方法
- 在内部类中如果要使用函数中定义的变量的情况
- OCX 结构体数组属性的定义和使用
- 定义和使用全局变量
- JSP页面如何访问标签中定义的变量-使用实现
- 全局变量的定义和使用
- 位域结构体变量在主流控制器(ARM/DSP等)编程使用时注意事项