源文件内容:
黄少天 通信 100 85 100
喻文州 通信 100 95 88
郑轩 通信 85 85 85
徐景熙 通信 89 66 82
卢瀚文 通信 61 92 98
宋晓 通信 88 97 68
叶修 物联网 100 100 100
魏琛 物联网 25 95 98
唐柔 物联网 100 98 96
安文逸 物联网 98 76 85
包子 物联网 22 78 65
罗辑 物联网 86 100 100
陈果 物联网 85 86 75
苏沐橙 物联网 98 86 98
周泽楷 网络 100 100 100
江波涛 网络 98 76 85
孙翔 网络 56 85 98
杜明 网络 76 85 78
方明华 网络 89 98 52
吕泊远 网络 85 28 78
吴启 网络 76 89 85
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define judgecom(str) (strcmp(str,"通信"))
#define judgeint(str) (strcmp(str,"网络"))
#define judgenet(str) (strcmp(str,"物联网"))
int ReadStudentInfo(const char*filename,struct student**pStudents);
void Count(struct student pStudents[],int num);
int comper=0,comav,comsum=0;
int intper=0,intav,intsum=0;
int netper=0,netav,netsum=0;
struct student
{
char name[20];
char major[20];
int eng_sco;
int xd_sco;
int c_sco;
int sum;
};
int main()
{
int zongshu;
int a;
struct student *list;
zongshu=ReadStudentInfo("C:\\Users\\pc\\Desktop\\Stuinfor.txt",&list);
printf("学生信息:\n");
printf("-------------------------------------------------------\n");
printf("序号 姓名 专业名称 英语成绩 线性代数成绩 c语言成绩 总成绩\n");
for(a=1;a<=zongshu;a++)
{
printf("%4d %6s %6s %4d %4d %4d %4d\n",a,(list+a-1)->name,(list+a-1)->major,(list+a-1)->eng_sco,(list+a-1)->xd_sco,(list+a-1)->c_sco,(list+a-1)->sum);
}
Count(list,zongshu);
printf("统计信息:\n");
printf("-------------------------------------------------------\n");
printf("序号 专业名称 学生人数 平均成绩\n");
printf(" 1 通信 %d %d\n",comper,comav);
printf(" 2 网络 %d %d\n",intper,intav);
printf(" 3 物联网 %d %d\n",netper,netav);
return 0;
}
int ReadStudentInfo(const char*filename,struct student**pStudents)
{
FILE *fp;
char ch;
int stu_num=1,i;
fp=fopen(filename,"r");
if(fp==NULL)
{
printf("cann't open the file");
exit(-1);
}
ch=fgetc(fp);
while(ch!=EOF)
{
if(ch=='\n')
{
stu_num++;
}
ch=fgetc(fp);
}
*pStudents=(struct student*)malloc(sizeof(**pStudents)*stu_num);
fseek(fp,0,SEEK_SET);
for(i=0;i<stu_num;i++)
{
fscanf(fp,"%s %s %d %d %d",(*pStudents+i)->name,(*pStudents+i)->major,&(*pStudents+i)->eng_sco,&(*pStudents+i)->xd_sco,&(*pStudents+i)->c_sco);
(*pStudents+i)->sum=((*pStudents+i)->eng_sco+(*pStudents+i)->xd_sco+(*pStudents+i)->c_sco);
}
fclose(fp);
return stu_num;
}
void Count(struct student pStudents[],int num)
{
int j;
for(j=0;j<num;j++)
{
if(judgecom((pStudents+j)->major)==0)
{
comper++;
comsum+=(pStudents+j)->sum;
comav=comsum/comper;
}
if(judgeint((pStudents+j)->major)==0)
{
intper++;
intsum+=(pStudents+j)->sum;
intav=intsum/intper;
}
if(judgenet((pStudents+j)->major)==0)
{
netper++;
netsum+=(pStudents+j)->sum;
netav=netsum/netper;
}
}
free(pStudents);
}