C程序设计综合性实验报告模版
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
《C程序设计》综合性实验
实验报告
题目:学生成绩管理
姓名:
班级:
学号:
指导教师:
完成时间:
一、实验题目
学生成绩管理
二、实验目的
1. 掌握一维数组、二维数组的使用方法。
2. 掌握结构体数组的定义和使用。
3. 综合应用数据文件的读写语句保存结构体数组中的数据。
三、实验要求
有4个学生,每个学生有3门课的成绩,从键盘上输入以上数据,计算每个学生的平均分,并把这些信息(包括学号、姓名、班级、3门课的成绩及平均分)保存到一个文件(score.txt)中,并显示在屏幕上。
具体要求:
①定义结构体类型(student_type),其中包括学号(num[11])、姓名(name[8])、班级(class[20])、3门课成绩(score[3])和平均成绩(ave)。利用该结构体类型定义数组stud[4]。
②在主函数中输入学生学号、姓名、班级、3门课的成绩,并计算出平均成绩,然后调用save()函数将学生数据保存在score.txt 文件中,调用display()函数读取score.txt文件,并将其中的内容显示在屏幕上。
③定义保存文件函数save()和显示文件函数display()。
四、程序流程图
五、程序代码
#include
struct student_type
{char num[13];
char name[20];
char classname[20];
float score[3];
float ave;
}stud[4];
void save()
{FILE *fp;
int i;
if((fp=fopen("score.txt","wb"))==NULL)
{
printf("cantnot open file\n");
return;
}
for(i=0;i<4;i++)
{
if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)
printf("file write error\n");
}
fclose(fp);
}
void display()
{
FILE *fp;
int i;
if((fp=fopen("score.txt", "rb"))==NULL)
{
printf("cantnot open file\n");
return;
}
printf("\n------------成绩---------------");
for(i=0;i<4;i++)
{
struct student_type stud;
fread(&stud,sizeof(struct student_type),1,fp);
printf("\n%6s\t%6s\t%6s\t%3.1f\t%3.1f\t%3.1f\t%3.1f\n",,stud.nu m,stud.classname,stud.score[0],stud.score[1],stud.score[2],stud.ave);
}
fclose(fp);
}
int main()
{
int i;
for (i=0;i<4;i++)
{
printf("\n--输入成绩[%i/4]--\n",i+1);
printf("输入学号:");scanf("%s",stud[i].num);
printf("输入姓名:");scanf("%s",stud[i].name);
printf("输入班级:");scanf("%s",stud[i].classname);
printf("输入成绩1:");scanf("%f",&stud[i].score[0]);
printf("输入成绩2:");scanf("%f",&stud[i].score[1]);
printf("输入成绩3:");scanf("%f",&stud[i].score[2]);
stud[i].ave=(stud[i].score[0]+stud[i].score[1]+stud[i].score[2])/3.0; }
printf("\n正在保存...");
save();
display();
return 0;
}
六、实验结果
七、实验体会
通过这次程序设计的练习我进一步熟悉了数组的使用方,对编程有了更深层次的认识,要学好c程序这门课程,就要多加练习,实践出真知。只有练习才能知道自己的不足。