几个数据结构的题目,成绩分析问题。谢谢

 我来答
xoaxa
推荐于2016-05-26 · TA获得超过8613个赞
知道大有可为答主
回答量:6415
采纳率:72%
帮助的人:3934万
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct student {
unsigned num; // 学号
char name[20];
double score[5]; // [0]:数学,[1]:英语,[2]:计算机,[3]:平均成绩,[4]:总成绩
struct student *next;
}*LinkList,*pNode,Node;

LinkList getEmptyList() {
LinkList head = (pNode)malloc(sizeof(Node));
head->num = 0;
head->name[0] = 0;
head->next = 0;
return head;
}

int insertNode(LinkList head,Node node) {
pNode p = head;
pNode newnode = (pNode)malloc(sizeof(Node));
*newnode = node;
while(p->next) {
if(p->next->num == node.num) {
printf("重复的学号:%u,操作失败!\n",node.num);
free(newnode);
return 0;
}
if(p->next->num > node.num) {
newnode->next = p->next;
p->next = newnode;
return 1;
}
else p = p->next;
}
p->next = newnode;
newnode->next = 0;
return 1;
}

int mysort(LinkList head,int index) {
pNode p,pt,q,qt;
if(index < 0 || index > 4) {
printf("0:按数学成绩排序。\n");
printf("1:按英语成绩排序。\n");
printf("2:按计算机成绩排序。\n");
printf("3:按平均成绩排序。\n");
printf("4:按总成绩排序。\n");
return 0;
}
for(p = head; p->next; p = p->next) {
qt = p;
q = p->next;
while(q->next) {
if(qt->next->score[index] < q->next->score[index])
qt = q;
q = q->next;
}
if(p != qt) {
pt = p->next;
p->next = qt->next;
qt->next = p->next->next;
p->next->next = pt;
}
}
return 1;
}

void show(LinkList head) {
int i;
pNode p = head->next;
while(p) {
printf("%u\t",p->num);
printf("%s\t",p->name);
for(i = 0; i < 5; ++i)
printf("%.2lf\t",p->score[i]);
printf("\n");
p = p->next;
}
}

int statist(LinkList head,int index) {
int score0_59 = 0,score60_69 = 0;
int score70_79 = 0,score80_89 = 0;
int score90_100 = 0,n = 0;
double t,max = 0,min = 100,total = 0;
pNode p = head->next;
if(index < 0 || index > 3) {
printf("0:统计数学成绩。\n");
printf("1:统计英语成绩。\n");
printf("2:统计计算机成绩。\n");
printf("3:统计平均成绩。\n");
return 0;
}
while(p) {
t = p->score[index];
if(t >= 90) ++score90_100;
else if(t >= 80) ++score80_89;
else if(t >= 70) ++score70_79;
else if(t >= 60) ++score60_69;
else ++score0_59;
if(t > max) max = t;
if(t < min) min = t;
total += t;
++n;
p = p->next;
}
if(score0_59)  printf(" 0 --  59: %d\n",score0_59); 
if(score60_69) printf("60 --  69: %d\n",score60_69);
if(score70_79) printf("70 --  79: %d\n",score70_79);
if(score80_89) printf("80 --  89: %d\n",score80_89);
if(score90_100) printf("90 -- 100: %d\n",score90_100);
if(n) printf("平均成绩:%.2lf\n",total/n);
return 1;
}

void readFile(LinkList head,char filename[]) {
int i;
Node node;
FILE *infp = fopen(filename,"rt");
if(infp == NULL) {
printf("打开文件 %s 时失败!\n",filename);
return;
}
while(fscanf(infp,"%u%s%",&node.num,node.name) == 2) {
node.score[4] = 0;
for(i = 0; i < 3; ++i) {
fscanf(infp,"%lf",&node.score[i]);
node.score[4] += node.score[i];
}
node.score[3] = node.score[4]/3;
insertNode(head,node);
}
fflush(infp);
fclose(infp);
}

void writeFile(LinkList head,char filename[]) {
int i;
pNode p = head->next;
FILE *outfp = fopen(filename,"wt");
if(outfp == NULL) {
printf("打开文件 %s 时失败!\n",filename);
return;
}
while(p) {
fprintf(outfp,"%u %s ",p->num,p->name);
for(i = 0; i < 5; ++i)
fprintf(outfp,".2lf ",p->score[i]);
fprintf(outfp,"\n");
p = p->next;
}
fclose(outfp);
}

void inputData(LinkList head) {
int i;
Node node;
printf("学号 姓名 英语 数学 计算机:\n");
scanf("%u%s",&node.num,node.name);
node.score[4] = 0;
for(i = 0; i < 3; ++i) {
scanf("%lf",&node.score[i]);
node.score[4] += node.score[i];
}
node.score[3] = node.score[4]/3;
insertNode(head,node);
}

int main() {
char inFilename[] = "input.dat";
char outFile[][60] = {"outFileMath.dat","outFileEnglish.dat",
"outFileComputer.dat","outFileAverage.dat","outFileTotal.dat"};
char command[20];
int op,index,save;
LinkList head = getEmptyList();
do {
printf("*********************************\n");
printf("*       考试成绩分析系统        *\n");
printf("*********************************\n");
printf("*        1、输入信息            *\n");
printf("*        2、显示信息            *\n");
printf("*        3、按课程成绩排序      *\n");
printf("*        4、信息分析            *\n");
printf("*        5、读数据文件          *\n");
printf("*                               *\n");
printf("*        0、推出分析系统        *\n");
printf("*********************************\n");
printf("\n请选择:");
fgets(command,20,stdin);
fflush(stdin);
op = command[0] - '0';
switch(op) {
case 1 : inputData(head); break;
case 2 : show(head); break;
case 3 : 
printf("0:按数学成绩排序\n");
printf("1:按英语成绩排序\n");
printf("2:按计算机成绩排序\n");
printf("3:按平均成绩排序\n");
printf("4:按总成绩排序\n");
printf("\n请输入相应编号:");
scanf("%d",&index);
fflush(stdin);
mysort(head,index);
printf("排序完毕,存盘否(1存,0不存):");
scanf("%d",&save);
fflush(stdin);
if(save == 1) writeFile(head,outFile[index]);
break;
case 4 : 
printf("0:数学成绩分析\n");
printf("1:英语成绩分析\n");
printf("2:计算机成绩分析\n");
printf("3:平均成绩分析\n");
printf("请输入相应编号:");
scanf("%d",&index);
fflush(stdin);
mysort(head,index);
statist(head,index);
break;
case 5 : readFile(head,inFilename); break;
case 0 : break;
default : printf("错误的选择。\n"); break;
}
}while(op);
printf("END!!!!\n");
return 0;
}
追问
我要从文件中读数据怎么办呢
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式