多文件组织的文件
来源:互联网 发布:maka软件官网 编辑:程序博客网 时间:2024/05/20 21:43
姓名:泮春宇 不语
时间;16 09.09
要点;多文件组织的文件
(1) .h 头文件:定义数据类型、声明自定义函数、定义宏等
(2).cpp 源文件1:用于实现头文件中声明的自定义函数
(3).cpp 源文件2:定义main()函数,用于调用相关函数,实现问题求解目标。
请将例1.13中按方案3实现的程序,用多文件形式组织并运行。
在需要的地方,用 #include “自定义头文件”,使文件之间的内容能“合起来”完成任务。
如果不能熟练完成,请参考《CodeBlocks中程序的多文件组织》一文。
main.cpp
#include "bigice.h"int main(){ int n=7; //学生记录人数 int m=21; //学生成绩记录数 struct stud1 s1[MaxStud]= { {1,"张斌",9901}, {8,"刘丽",9902}, {34,"李英",9901}, {20,"陈华",9902}, {12,"王奇",9901}, {26,"董强",9902}, {5,"王萍",9901} }; struct stud2 s2[MaxCour]= //规定课程的编号从1到6,同一学生成绩记录连续存放 { {1,1,67}, {1,2,98}, {1,4,65}, {8,1,98}, {8,3,90}, {8,6,67}, {34,2,56}, {34,4,65}, {34,6,77}, {20,1,68}, {20,2,92}, {20,3,64}, {12,4,76}, {12,5,75}, {12,6,78}, {26,1,67}, {26,5,78}, {26,6,62}, {5,1,94}, {5,2,92}, {5,6,89} }; allavg(s1,n,s2,m); return 0;}bigice.h
#include <stdio.h>#define MaxStud 50 //学生人数最多为50#define MaxCour 300 //学生成绩记录数最多为50*6struct stud1{ int no; //学号 char name[10]; //姓名 int bno; //班号};struct stud2{ int no; //学号 int cno; //课程编号 int deg; //分数};double studavg(struct stud2 s2[],int m,int i);double couravg(struct stud2 s2[],int m,int i);void allavg(struct stud1 s1[],int n,struct stud2 s2[],int m);haha.cpp
#include "bigice.h"void allavg(struct stud1 s1[],int n,struct stud2 s2[],int m) //求学生平均分和课程平均分{ int i,j; printf("学生平均分:\n"); printf(" 学号 姓名 平均分\n"); i=0; while (i<n) { j=s1[i].no; printf("%4d %10s %g\n",s1[i].no,s1[i].name,studavg(s2,m,j)); i++; } printf("课程平均分:\n"); for (i=1; i<=6; i++) printf(" 课程%d:%g\n",i,couravg(s2,m,i));}double studavg(struct stud2 s2[],int m,int i) //求学号为i的学生的平均分{ int j,n=0; //n为学号为i的学生选学课程数 double sum=0; //学号为i的学生总分 for (j=0; j<m; j++) if (s2[j].no==i) //学号为i时统计 { n++; sum+=s2[j].deg; } return(sum/n);}double couravg(struct stud2 s2[],int m,int i) //求编号为i的课程的平均分{ int j,n=0; //n为编号为i的课程选修人数 double sum=0; //编号为i的课程总分 for (j=0; j<m; j++) { if (s2[j].cno==i) //课程编号为i时统计 { n++; sum+=s2[j].deg; } } return(sum/n);}
运行结果
心得;善用可提高效率和程序整洁度
0 0
- 多文件组织的文件
- 第二周项目二-多文件组织(如何将一个文件的代码多文件组织)
- 程序的多文件组织
- 程序的多文件组织
- 程序的多文件组织
- 程序的多文件组织
- 程序的多文件组织
- 程序的多文件组织
- 程序的多文件组织
- 程序设计的多文件组织
- 程序的多文件组织
- 程序的多文件组织
- 程序的多文件组织
- 程序的多文件组织
- 程序的多文件组织
- 程序的多文件组织
- 程序的多文件组织
- 程序的多文件组织
- IL2CPP 深入讲解:垃圾回收器的集成(六)
- ajax 设置Access-Control-Allow-Origin实现跨域访问
- 第二周 项目3.2 体验复杂度(汉诺塔)
- android:用getIdentifier()获取资源Id
- Java 报错No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- 多文件组织的文件
- 对乡村地名进行模糊匹配
- 让用户信息安全起来
- MySQL创建用户与授权方法
- IOS开发中常用的一些宏定义
- MyBatis 插入MySQL数据库返回主键异常 java.lang.Integer cannot be cast to java.lang.Long
- java练习---计算平均分和总成绩
- Linux kernel-<armv7a-switch_to>
- 莫比乌斯反演定理证明