4.2结构体
2015-04-02 11:54
260 查看
/*#include <stdio.h> int main() { struct Student //声明结构体类型 { long int num; char name[20]; char sex; char addr[20]; }a = {10101,"Li lin",'M',"123 Beijing Road"}; //定义结构体变量a并初始化 printf("No.:%ld\nname:%s\nsex:%c\naddress:%s\n",a.num,a.name,a.sex,a.addr); return 0; }*/ /*#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 higer 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; }*/ //有3个候 4000 选人,每个选民只能投票选一人,要求编一个统计选票的程序,先后输入被候选人的名字,最后输出个人得票结果 /*#include <stdio.h> #include <string.h> struct Person //声明结构体类型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 =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); return 0; }*/ //有n个学生的信息(包括学号,姓名,成绩),要求按照成绩的高低顺序输出各学生的信息 /*#include <stdio.h> struct Student { int num; char name[20]; float score; }; int main() { struct Student stu[5] = {{10101,"Zhang",78},{10103,"Wang",98.5},{10106,"li",86}, {10108,"ling",73.5},{10110,"Sun",100}}; struct Student temp; const int n = 5; int i, j ,k; printf("The order is:\n"); for( i = 0; i < n-1; i++ ) { k = i; for( j = i+1; j < n; j++ ) if( stu[j].score > stu[k].score ) k = j; temp = stu[k]; stu[k] = stu[i]; stu[i] = temp; } for( i = 0; i < n; i++ ) printf("%6d%8s%6.2f\n",stu[i].num,stu[i].name,stu[i].score); printf("\n"); return 0; }*/
相关文章推荐
- Lua和C语言的交互详解
- 关于C语言中参数的传值问题
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言内存对齐实例详解
- c语言实现的带通配符匹配算法
- C语言实现顺序表基本操作汇总
- 探讨C语言的那些小秘密之断言
- 深入探讨C语言中局部变量与全局变量在内存中的存放位置
- C语言泛型编程实例教程
- 基于C语言fflush()函数的使用详解
- C语言单链队列的表示与实现实例详解
- 关于C语言除0引发的思考
- 深入分析C中不安全的sprintf与strcpy
- Lua教程(四):在Lua中调用C语言、C++的函数
- C语言求幂计算的高效解法
- C语言实现输入一颗二元查找树并将该树转换为它的镜像
- 12个关于C语言的有趣问答
- 探讨register关键字在c语言和c++中的差异
- C中实现矩阵乘法的一种高效的方法