1.实验目的与要求
⑴ 掌握用C语言编写查询统计程序。
⑵ 掌握C语言的文件读写的基本操作。
2.实验内容
⑴ 创建一个文本文件,含有若干班级的数据,要求其数据每个班级至少有50位同学,每位同学至少学习10门课程
⑵ 写出函数求出某一位的平均成绩
⑶ 写出函数求出每一位的平均成绩
⑷ 使用文件读入相关数据,将运行结果送到文件中
3.实验准备
-
首先应明确创建文本文件的格式,数据协议等。
-
其次根据文件读取数据,并将结果送入结果文件中。
-
参阅C语言教材中的有关内容。
4.实验方法
⑴ 用创建文本文件的格式创建数据。
⑵ 编制相应功能函数。
⑶ 读取文件的数据(注意可能有不同的个数的数据)。
⑷ 整合相关函数构成完整的应用程序。
⑸ 输出结果送入文件中。
代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 153
struct student_type
{
char name[20];//姓名
int num;//学号
int hhh;//班级
float score1;//数学
float score2;//英语
float score3;//物理
float score4;//电路
float score5;//马原
float score6;//离散
float score7;//Java
float score8;//c语言
float score9;//c++
float score10;//HTML
float score11;//CSS
} stud[SIZE];
void menu()
{
printf("\t\t\t ********************************\n");
printf("\t\t\t 学生成绩查询系统1.0 \n");
printf("\t\t\t [1] 查看所有学生信息 \n");
printf("\t\t\t [2] 查询学生个人成绩 \n");
printf("\t\t\t [0] 退出 \n ");
}
void re_menu()
{
char c1, c2;
printf("\n\n\n按回车键返回主菜单");
scanf("%c,%c", &c1, &c2);
menu();
}
int seek()
{
int i;
float ave[SIZE];
FILE *fp = fopen("input.txt", "rw+");
FILE *fpWrite = fopen("out.txt", "a+");
if (fpWrite == NULL && fp == NULL)
{
printf("cannot open file\n");
return 0;
}
for (i = 1; i <= SIZE; i++)
{
fscanf(fp, " %s %d %d %f %f %f %f %f %f %f %f %f %f %f\n", stud[i].name, &stud[i].hhh, &stud[i].num, &stud[i].score1, &stud[i].score2, &stud[i].score3, &stud[i].score4, &stud[i].score5, &stud[i].score6, &stud[i].score7, &stud[i].score8, &stud[i].score9, &stud[i].score10, &stud[i].score11);
}
printf("姓名\t班级\t学号\t数学\t英语\t物理\t电路\t马原\t离散\tJava\tc语言\tc++\tHTML\tcss\t平均分\n");
for (i = 1; i <= SIZE; i++)
{
ave[i] = (stud[i].score1 + stud[i].score2 + stud[i].score3 + stud[i].score4 + stud[i].score5 + stud[i].score6 + stud[i].score7 + stud[i].score8 + stud[i].score9 + stud[i].score10 + stud[i].score11) / 11;
printf(" %s\t%d\t %d\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\n ", stud[i].name, stud[i].hhh, stud[i].num, stud[i].score1, stud[i].score2, stud[i].score3, stud[i].score4, stud[i].score5, stud[i].score6, stud[i].score7, stud[i].score8, stud[i].score9, stud[i].score10, stud[i].score11,ave[i]);
fprintf(fpWrite, "%s"
"平均分:"
"%-7.1f\n",
stud[i].name, ave[i]);
}
fclose(fp);
fclose(fpWrite);
return 0;
}
int seek1()
{
FILE *fp;
int i;
float ave;
if ((fp = fopen("input.txt", "rw+")) == NULL)
{
printf("cannot open file\n");
return 0;
}
for (i = 1; i <= SIZE; i++)
{
fscanf(fp, " %s %d %d %f %f %f %f %f %f %f %f %f %f %f\n", stud[i].name,&stud[i].hhh, &stud[i].num, &stud[i].score1, &stud[i].score2, &stud[i].score3, &stud[i].score4, &stud[i].score5, &stud[i].score6, &stud[i].score7, &stud[i].score8, &stud[i].score9, &stud[i].score10, &stud[i].score11);
}
printf("请输入你要查找的学生的班级+空格+学号");
int nub1;
int nub2;
int size;
scanf("%d", &nub1);
scanf("%d", &nub2);
for (i = 1; i <= SIZE; i++)
{
if (stud[i].hhh == nub1 && stud[i].num == nub2)
{
size=i;
ave = (stud[i].score1 + stud[i].score2 + stud[i].score3 + stud[i].score4 + stud[i].score5 + stud[i].score6 + stud[i].score7 + stud[i].score8 + stud[i].score9 + stud[i].score10 + stud[i].score11) / 11;
printf("姓名\t班级\t学号\t数学\t英语\t物理\t电路\t马原\t离散\tJava\tc语言\tc++\tHTML\tcss\t平均分\n");
printf(" %s\t%d\t %d\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t %-2.1f\t ", stud[i].name, stud[i].hhh, stud[i].num, stud[i].score1, stud[i].score2, stud[i].score3, stud[i].score4, stud[i].score5, stud[i].score6, stud[i].score7, stud[i].score8, stud[i].score9, stud[i].score10, stud[i].score11,ave);
}
}
fclose(fp);
FILE *fpWrite = fopen("out.txt", "a+");
if (fpWrite == NULL)
{
return 0;
}
fprintf(fpWrite, "%s"
"平均分:"
"%-7.1f\n",
stud[size].name, ave);
fclose(fpWrite);
return 0;
}
int main()
{
menu();
int a;
while (1)
{
scanf("%d", &a);
switch (a)
{
case 1:
seek();
break;
case 2:
seek1();
break;
}
if (a == 0)
break;
re_menu();
}
return 0;
}