C 语言程序设计_09_01.ppt
文本预览下载声明
* 结构体类型定义仅描述结构体的组成,不分配内存空间 只有在定义了结构体变量后系统才为其分配内存。 用无名结构体直接定义变量只能一次 * * * * * * * * * 结构体数组 2 例9.3 统计候选人选票 1. 问题提出: 2. 解题思路: 结构体数组 leadere[3] 3. 编写程序: 4. 运行结果: 5. 程序分析: 6. 程序改进: #include stdio.h #include string.h struct person { char name[20]; int count; }leader[3]={“Li”,0,“Zhang”,0,”Fun“,0}; //全局结构体数组 int main() { int i,j; char leader_name[20]; for(i=1;i=10;i++) { scanf(%s,leader_name); for(j=0;j3;j++) if(strcmp(leader_name,leader[j].name)==0) leader[j].count++; // “.”成员运算符优先于“++”,所以Leader[j].count++相当于(learer[j].count)++ } printf(“\nResoult:\n); for(i=0;i3;i++) printf(%5s:%d\n,leader[i].name,leader[i].count);} 结构体数组 2 例9.3 统计候选人选票 1. 问题提出: 2. 解题思路: 结构体数组 leadere[3] 3. 编写程序: 4. 运行结果: 5. 程序分析: 6. 程序改进: name count Li Zhang Wang 0 0 0 name count Li Zhang Wang 4 3 3 结构体数组 2 例9.4 成绩排序 1. 问题提出: 2. 解题思路: 选择排序 3. 编写程序: 4. 运行结果: 5. 程序分析: #define N 30 temp—struct student 整体互换 6. 程序改进: #include stdio.h #define N 5 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,”Fun”,100}}; struct student temp; int i,j,k; printf(“The order is:\n); 结构体数组 2 例9.4 成绩排序 1. 问题提出: 2. 解题思路: 选择排序 3. 编写程序: 4. 运行结果: 5. 程序分析: #define N 30 temp—struct student 整体互换 6. 程序改进: for(i=0;iN-1;i++) { k=1; for(j=i+1;jN;j++) if (stu[j].scorestu[k].score) k=j; temp=stu[k];stu[k]=stu[i];stu[i]=temp; } for(i=0;iN;i++) printf(“%6d%8s%6.2f\n”,stu[i].num,stu[i].name,stu[i].score); pritnf(“\n”); returen 0; } 结构体指针 3 struct 结构体名 *结构体指针名; num name sex age stu p struct student { int num; char name[20]; char sex
显示全部