解析:
最近帮朋友做的一个相当于课程设计的学生成绩管理系统,她们要求是用c语言写,对于一个用惯c++的孩子来说真是感觉写起来的各种限制真心感觉到无语,不说了直接上代码
#include "student.h" void main() { char ch='0'; int index=0; char id[ID_SIZE]; student students; student phead; FILE *output=fopen("output.txt","w+"); FILE *input=fopen("input.txt","r"); if(input==NULL) { printf("打开失败input\n"); exit(1); } if(input==NULL) { printf("打开失败output\n"); exit(1); } phead.next=NULL; InitStudent(&students); while(1) { ShowList(); printf("请选择:"); ch=getchar(); switch(ch) { case '1': Student_Insert(&students); break; case '2': Student_Display(&students); break; case '3': { printf("请输入需要查找多少个学生:"); scanf("%d",&index); Student_DisplaySingle(&students,index); } break; case '4': { printf("请输入需要删除的学号:"); scanf("%s",id); Student_Delete(&students,id); } break; case '5': Student_Modify(&students); break; case '6': { printf("请输入需要查找学生姓名:"); scanf("%s",id); Student_Select(&students,id); } break; case '7': Student_SortByAverage(&students,&phead); break; case '8': { IO_ReadInfo(&students,output); fclose(output); } break; case '9': { IO_WriteInfo(&students,output); fclose(output); } break; case '0': { printf("欢迎使用学生管理成绩系统\n"); exit(1); } default: break; } getchar(); } }
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <assert.h> #include <string.h> #define ID_SIZE 128 #define NAME_SIZE 128 #define SIZE 125 struct student { char Id[ID_SIZE]; //学生学号 char Name[NAME_SIZE]; //学生姓名 float Math; //数学 float English; //英语 float Chinese; //语文 float TotalScore; //总分 float AverageScore; //平均分 int PassStudent; //及格人数 struct student *next; }; typedef struct student student; static float PassRate=0.0; //及格率 //初始化学生信息 void InitStudent(student *head) { strcpy(head->Id,""); strcpy(head->Name,""); head->Chinese=0.0; head->English=0.0; head->Math=0.0; head->PassStudent=0; head->TotalScore=0; head->next=NULL; } //插入学生成绩 void Student_Insert(student *head) { int flag=0; student *cur=head; student *tmp=(student *)malloc(sizeof(student)); tmp->next=NULL; assert(tmp !=NULL); printf("***开始录入学生信息***\n"); printf("请输入学号:"); scanf("%s",tmp->Id); printf("请输入姓名:"); scanf("%s",tmp->Name); printf("请输入数学成绩:"); scanf("%f",&tmp->Math); printf("请输入英语成绩:"); scanf("%f",&tmp->English); printf("请输入语文成绩:"); scanf("%f",&tmp->Chinese); tmp->TotalScore=tmp->Chinese+tmp->English+tmp->Math; tmp->AverageScore=tmp->TotalScore/3; tmp->PassStudent=0; if(tmp->Chinese >=60) tmp->PassStudent++; if(tmp->English >=60) tmp->PassStudent++; if(tmp->Math >=60) tmp->PassStudent++; if(tmp->PassStudent==3) flag++; tmp->AverageScore=tmp->TotalScore/3; printf("学生及格人数:%d\n",flag); while(cur->next !=NULL) cur=cur->next; cur->next=tmp; } /*删除学生信息*/ void Student_Delete(student *head,char *id) { student *del=NULL; student *cur=head; while(cur->next !=NULL) { if(strcmp(cur->next->Id,id)==0) { del=cur->next; cur->next=del->next; free(del); printf("删除成功\n"); return; } cur=cur->next; } printf("删除失败\n"); } /*修改学生信息*/ void Student_Modify(student *head) { char number[ID_SIZE]; int flag=0; student *cur=head->next; assert(cur !=NULL); printf("请输入学生学号:"); scanf("%s",number); printf("开始查找\n"); while(cur !=NULL) { if(strcmp(cur->Id,number)==0) { printf("请输入准备修改的语文成绩:"); scanf("%f",&cur->Chinese); printf("请输入准备修改的数学成绩:"); scanf("%f",&cur->Math); printf("请输入准备修改的英语成绩:"); scanf("%f",&cur->English); flag=1; break; } cur->next; } if(flag) printf("修改成功\n"); else printf("修改失败\n"); } /*按姓名查询*/ void Student_Select(student *head,char *name) { student *cur=head->next; while(cur!=NULL) { if(strcmp(cur->Name,name)==0) { printf("存在这个学生\n"); return; } cur=cur->next; } printf("按姓名查找失败\n"); } /*显示单条学生记录*/ void Student_DisplaySingle(student *head,int index) { int count=1; student *cur=head->next; while(count <index && cur !=NULL) { cur=cur->next; count++; } if(count >index) printf("没有这个学生\n"); else { printf("%s\t%s\t%.3f\t%.3f\t%.3f\t",cur->Id,cur->Name,cur->Chinese,cur->Math,cur->English); printf("%.3f\t%.3f\n",cur->AverageScore,cur->TotalScore); } } //显示所有学生成绩 void Student_Display(student *head) { int count=0; int number=0; student *cur=head->next; printf("学号\t姓名\t语文\t数学\t英语\t平均分\t总分\n"); while(cur !=NULL) { printf("%s\t%s\t%.3f\t%.3f\t%.3f\t",cur->Id,cur->Name,cur->Chinese,cur->Math,cur->English); printf("%.3f\t%.3f\n",cur->AverageScore,cur->TotalScore); if(cur->Chinese >=60 && cur->English>=60 && cur->Math>=60) count++; cur=cur->next; number++; } PassRate=(float)count/number*100; printf("及格率:%.1f%%\n",PassRate); } /*按平均值排序*/ void Student_SortByAverage(student *head,student *phead) { student *pcur=NULL; student *tmp=NULL; student *ptmp=NULL; student *cur=head->next; while(cur !=NULL) { pcur=cur; while(pcur !=NULL && pcur->AverageScore ==INT_MAX) pcur=pcur->next; if(pcur==NULL) break; tmp=pcur; while(pcur !=NULL) { if(pcur->AverageScore > tmp->AverageScore && pcur->AverageScore !=INT_MAX) { tmp=pcur; } pcur=pcur->next; } if(tmp->AverageScore !=INT_MAX) { ptmp=(student *)malloc(sizeof(student)); assert(ptmp !=NULL); strcpy(ptmp->Id,tmp->Id); strcpy(ptmp->Name,tmp->Name); ptmp->Chinese=tmp->Chinese; ptmp->English=tmp->English; ptmp->Math=tmp->Math; ptmp->AverageScore=tmp->AverageScore; ptmp->PassStudent=tmp->PassStudent; ptmp->TotalScore=tmp->TotalScore; ptmp->next=phead->next; phead->next=ptmp; tmp->AverageScore=INT_MAX; } } Student_Display(phead); } /*将学生信息从文件读出*/ void IO_ReadInfo(student *head,FILE *path) { student *cur=head->next; while(cur !=NULL) { if(fread(cur,sizeof(student),1,path) < 0) { printf("fread failed\n"); break; } cur=cur->next; } printf("读入成功\n"); } /*将学生信息写入文件*/ void IO_WriteInfo(student *head,FILE *path) { student *cur=head->next; while(cur !=NULL) { fprintf(path,"%s %s %f %f %f %f %f %d %s",\ cur->Id,cur->Name,cur->Math,cur->English,cur->Chinese,\ cur->TotalScore,cur->AverageScore,cur->PassStudent,"\n"); cur=cur->next; } printf("写入数据成功\n"); } //显示列表 void ShowList() { printf("**********************************************\n"); printf("**【1】插入学生成绩 【2】显示全部学生信息 **\n"); printf("**【3】显示单个学生 【4】删除学生信息 **\n"); printf("**【5】修改学生信息 【6】按姓名查询 **\n"); printf("**【7】按平均值排序 【8】从文件读出学生信息 **\n"); printf("**【9】写入文件 【0】退出学生管理系统 **\n"); printf("**********************************************\n"); }