此学生成绩管理系统完成15项功能,是我经过很多次的修改得到的。花费时间不短,功能齐全,主要完成了:
输入,输出,插入,修改,删除,查询,读取文件,写入文件,复制文件,建立索引,显示各种情况下的信息,追加学生信息等等功能。一共15项。
代码如下:
// shujujiegou.cpp : 定义控制台应用程序的入口点。
//
/**本程序采用数组链表形式,原因:数组查询速度快,数组也能实现动态存储*/
#include "stdafx.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#define M 30
#define N 5/**学号长度*/
#define T 10
struct Student
{
char Stu_Name[T];/**学生姓名*/
char Stu_ID[N];/**学生学号*/
float math;
float chinese;
float english;
float computer;
float average;
float total;
};
struct Student student[M];
int Stu_num=0;//初始化实际学生人数
/**计算平均成绩*/
float Stu_avg(struct Student stu)
{
return (stu.chinese+stu.computer+stu.english+stu.math)/4;
}
/**计算总成绩*/
float Stu_total(struct Student stu)
{
return (stu.chinese+stu.computer+stu.english+stu.math);
}
/**通过学号返回数组下标,方便通过学号查询*/
int Stu_indexByNum(char stu_id[])
{
int i;
for(i=0;i {
if(strcmp(student[i].Stu_ID,stu_id)==0)
{
return i;
}
}
return -1;
}
/**通过姓名返回数组下标,方便通过姓名查询*/
int Stu_indexByName(char name[])
{
int i;
for(i=0;i {
if(strcmp(student[i].Stu_Name,name)==0)
return i;
}
return -1;
}
/**对学生输入成绩的合法性进行判断*/
/**显示单条学生记录*/
void Stu_displayBySingle(int index)
{
printf("显示查询学生个人信息:\n");
printf("%10s%10s%8s%8s%8s%8s%10s%10s\n","学号","姓名","数学","英语","语文","计算机","平均成绩","总分");
printf("------------------------------------------------------------------------------\n");
printf("%10s%10s%8.2f%8.2f%8.2f%8.2f%10.2f%10.2f\n",student[index].Stu_ID,student[index].Stu_Name,
student[index].math,student[index].english,student[index].chinese,
student[index].computer,student[index].average,student[index].total);
printf("------------------------------------------------------------------------------\n");
}
/**简略显示每条信息*/
void Stu_displaySingle(int index)
{
printf("------------------------------------------------------------------------------\n");
printf("%10s%10s%8.2f%8.2f%8.2f%8.2f%10.2f%10.2f\n",student[index].Stu_ID,student[index].Stu_Name,
student[index].math,student[index].english,student[index].chinese,
student[index].computer,student[index].average,student[index].total);
printf("------------------------------------------------------------------------------\n");
}
/**添加学生信息*/
void Stu_Add()
{
int c;
int index;
bool flag=false;
while(true)
{
printf("请输入学号:");
scanf("%s",&student[Stu_num].Stu_ID);
/**如果输入学号重复则重新输入*/
index=Stu_indexByNum(student[Stu_num].Stu_ID);
//printf("%d",Stu_num);
if(Stu_num>0)
{
while(true)
{
for(int i=0;i {
if(index==Stu_indexByNum(student[i].Stu_ID))
{
printf("学号已存在,请重新输入:");
scanf("%s",student[Stu_num].Stu_ID);
index=Stu_indexByNum(student[Stu_num].Stu_ID);
}
else{flag=true;}
}
if(flag)
{break;}
}
}
/**学号不重复继续添加*/
printf("请输入姓名:");
scanf("%s",&student[Stu_num].Stu_Name);
/**包含对成绩输入合法性验证*/
printf("输入学生各科成绩(0->100):\n");
printf("请输入数学成绩:");
scanf("%f",&student[Stu_num].math);
printf("请输入英语成绩:");
scanf("%f",&student[Stu_num].english);
printf("请输入语文成绩:");
scanf("%f",&student[Stu_num].chinese);
printf("请输入计算机:");
scanf("%f",&student[Stu_num].computer);
/**计算该学生平均成绩*/
student[Stu_num].average=Stu_avg(student[Stu_num]);
/**计算该学生总成绩*/
student[Stu_num].total=Stu_total(student[Stu_num]);
Stu_displayBySingle(Stu_num);
printf("添加成功!\n");
Stu_num++;
printf("是否继续添加:是(1)否(2):\n");
scanf("%d",&c);
if(c==2)
{
printf("学生信息添加结束!\n");
break;
}
}
}
/**修改学生信息*/
void Stu_Modify()
{
while(true)
{
char id[N];
int c;
int index;
printf("输入要修改学生学号:");
scanf("%s",&id);
//getchar();
index=Stu_indexByNum(id);
if(index==-1)
{
printf("输入学号不存在\n");
}
else
{
/**显示你要修改学生的当前信息*/
printf("请选择要修改学生的当前信息:");
Stu_displayBySingle(index);
printf("请输入要修改的信息:");
printf("请重新输入要修改学生的学号:");
scanf("%s",&student[index].Stu_ID);
printf("请输入姓名:");
scanf("%s",&student[index].Stu_Name);
printf("请输入数学成绩:");
scanf("%f",&student[index].math);
printf("请输入英语成绩:");
scanf("%f",&student[index].english);
printf("请输入语文成绩:");
scanf("%f",&student[index].chinese);
printf("请输入计算机成绩:");
scanf("%f",&student[index].computer);
//printf("输出平均成绩:");
student[index].average=Stu_avg(student[index]);//修改后的平均成绩
//printf("");
student[index].total=Stu_total(student[index]);//修改后的总成绩
printf("是否继续修改:是(1)否(2):\n");
scanf("%d",&c);
if(c==2)
{
printf("修改信息结束。\n");
break;
}
}
}
}
/**通过学号删除学生信息*/
/**通过姓名删除由于人名可能重复,所以学生的关键字为学号(不能重复)*/
void Stu_deleteByNum()
{
while(true)
{
char id[N];
int c;
int index;
printf("输入要删除学生的学号:");
scanf("%s",&id);
index=Stu_indexByNum(id);
if(index==-1)
{
printf("删除学生信息不存在!\n");
break;
}
else
{
printf("你要删除的当前学生信息:\n");
Stu_displayBySingle(index);
printf("是否真的要删除:是(1)否(2)\n");
scanf("%d",&c);
if(c==1)
{
for(int i=index;i {
student[i]=student[i+1];
}
Stu_num--;//删除一个,总人数减1
}
}
printf("是否继续删除:是(1)否(2)");
scanf("%d",&c);
if(c==2)
{
printf("信息删除成功!\n");
break;
}
}
}
/**按照姓名查询学生信息*/
void Stu_searchByName()
{
while(true)
{
int c;
int index;
char name[T];
printf("请输入要查找学生的姓名:");
scanf("%s",&name);
//getchar();
index=Stu_indexByName(name);
if(index==-1)
{
printf("该学生信息不存在!\n");
}
else
{
printf("你要查询的信息为:\n");
Stu_displayBySingle(index);//显示所要查询学生的信息
}
printf("是否继续查看:是(1)否(2)\n");
scanf("%d",&c);
if(c==2)
{
printf("学生信息查询结束!\n");
break;
}
}
}
/**按照学号查询*/
void Stu_searchByNum()
{
while(true)
{
int c;
int index;
char num[N];
printf("请输入要查询学生学号:");
scanf("%s",&num);
index=Stu_indexByNum(num);
if(index==-1)
{
printf("该学生信息不存在!\n");
}
else
{
printf("你要查询的信息为:\n");
Stu_displayBySingle(index);//显示查询学生的信息
}
printf("是否继续查询:是(1)否(2)\n");
scanf("%d",&c);
if(c==2)
{
printf("学生信息查询结束!\n");
break;
}
}
}
/**在学号内部插入(只适合内部存在学号中断如6,8之间缺少7号,插入7号)*/
void Stu_insertMiddle()
{
while(true)
{
char num[N];
int i;
bool flag=false;
int c;
printf("请输入插入学生学号:");
scanf("%s",num);
//index=Stu_indexByNum(num);/**获取下标确定插入位置*/
int index=atoi(num);
//printf("%d",index);
while(true)
{
for(i=0;i {
if(strcmp(num,student[i].Stu_ID)==0)
{
printf("学号重复,请重新输入:");
scanf("%s",&num);
index=atoi(num);/**获取下标确定插入位置*/
}
}
for(i=0;i<=Stu_num;i++)
{
if(atoi(student[i].Stu_ID)>index-1)/**查找第一个大于查找数的位置*/
break;
}
for(int j=Stu_num;j>index-1;j--)
{
student[j]=student[j-1];
}
flag=true;
if(flag)
{break;}
}
student[i]=student[index-1];/**将要插入的数据保存到该位置*/
printf("重新确认输入的学号信息(1-5位):");
scanf("%s",&student[i].Stu_ID);
printf("请输入要插入学生的姓名:");/**名字不能重复*/
scanf("%s",&student[i].Stu_Name);
/**分数输入合法性验证*/
printf("请输入各科成绩(0-->100):\n");
printf("请输入学生数学成绩:");
scanf("%f",&student[i].math);
printf("请输入学生英语成绩:");
scanf("%f",&student[i].english);
printf("请输入学生语文成绩:");
scanf("%f",&student[i].chinese);
printf("请输入学生计算机成绩:");
scanf("%f",&student[i].computer);
student[i].average=Stu_avg(student[i]);
student[i].total=Stu_total(student[i]);
//Stu_displayBySingle(i+1);
printf("插入成功!\n");
Stu_num++;/**r人数加1*/
printf("是否继续添加:是(1)否(2)");
scanf("%d",&c);
if(c==2)
{
printf("插入操作结束!\n");
break;
}
}
}
/**在学号末尾追加学生信息*/
void Stu_insertByNum()
{
while(true)
{
char num[N];
int i;
bool flag=false;
int c;
printf("请输入追加学生学号:");
scanf("%s",&num);
//index=Stu_indexByNum(num);/**获取下标确定插入位置*/
while(true)
{
for(i=0;i {
if(strcmp(num,student[i].Stu_ID)==0)
{
printf("学号重复,请重新输入:");
scanf("%s",&student[Stu_num].Stu_ID);
}else{flag=true;}
}
if(flag)
{break;}
}
printf("重新确认输入的学号信息:");
scanf("%s",&student[Stu_num].Stu_ID);
printf("请输入要追加学生的姓名:");/**名字不能重复*/
scanf("%s",&student[Stu_num].Stu_Name);
/**分数输入合法性验证*/
printf("请输入各科成绩(0-->100):\n");
printf("请输入学生数学成绩:");
scanf("%f",&student[Stu_num].math);
printf("请输入学生英语成绩:");
scanf("%f",&student[Stu_num].english);
printf("请输入学生语文成绩:");
scanf("%f",&student[Stu_num].chinese);
printf("请输入学生计算机成绩:");
scanf("%f",&student[Stu_num].computer);
student[Stu_num].average=Stu_avg(student[Stu_num]);
student[Stu_num].total=Stu_total(student[Stu_num]);
//Stu_displayBySingle(i+1);
printf("插入成功!\n");
Stu_num++;/**r人数加1*/
printf("是否继续添加:是(1)否(2)");
scanf("%d",&c);
if(c==2)
{
printf("追加操作结束!\n");
break;
}
}
}
/**显示不及格学生成绩*/
void Stu_notPass()
{
int count=0;/**用于显示不及格人数*/
int pass;
printf("%10s%10s%8s%8s%8s%8s%10s%10s\n","学号","姓名","数学","英语","语文","计算机","平均成绩","总分");
for(int i=0;i {
if(student[i].chinese<60||student[i].math<60||student[i].english<60||student[i].computer<60)
{
count++;
Stu_displaySingle(i);/**显示不及格学生信息*/
}
}
pass=Stu_num-count;/**显示及格人数*/
printf("%d个人有成绩不及格记录\n",count);
printf("%d个人全部及格\n",pass);
}
/**显示总分大于XXX的学生人数和信息*/
void Stu_overScore()
{
int count=0;/**记录分数超过XXX的人数*/
int overScore;/**查询的分数线*/
printf("请输入你想查找的超过XXX分的人信息:\n\n");
printf("请输入你想输入的分数线:\n");
scanf("%d",&overScore);
printf("%10s%10s%8s%8s%8s%8s%10s%10s\n","学号","姓名","数学","英语","语文","计算机","平均成绩","总分");
for(int i=0;i {
if(student[i].total>=overScore)
{
count++;
//printf("%s",student[i].Stu_Name);
Stu_displaySingle(i);/**显示查询学生信息*/
}
}
printf("分数超过%d的人数为%d人\n",overScore,count);
}
/**对学号排序(初始化学生信息)*/
void Stu_sortNum()
{
struct Student temp;
/** (对学号)冒泡排序*/
for(int i=0;i {
for(int j=i+1;j {
if(strcmp(student[i].Stu_ID,student[j].Stu_ID)>0)
{
temp=student[i];
student[i]=student[j];
student[j]=temp;
}
}
}
printf("初始化完成!\n");
}
/**按照总成绩排序,与上面类似*/
void Stu_sortByTotal()
{
struct Student temp;
for(int i=0;i<=Stu_num;i++)
{
for(int j=1;j if(student[j-1].total {
temp=student[j-1];
student[j-1]=student[j];
student[j]=temp;
}
}
printf("按照总成绩排序成功!\n");
}
/**将文件从文本中读出来*/
void Stu_ioRead()
{
FILE *fp;
int count=0;
int i;
fflush(stdin);
if((fp=fopen("Database.txt","r"))==NULL)
{
printf("文件为空,不能打开。\n");
exit(0);
}
Stu_num=0;
for( i=0;i {
fscanf(fp,"%s\t%s\t%f\t%f\t%f\t%f\t%f\t%f\n",&student[i].Stu_ID,&student[i].Stu_Name,
&student[i].math,&student[i].english,&student[i].chinese,
&student[i].computer,&student[i].average,&student[i].total);
Stu_num++;
}
printf("文件读取成功\n");
printf("%10s%10s%8s%8s%8s%8s%10s%10s\n","学号","姓名","数学","英语","语文","计算机","平均成绩","总分");
printf("------------------------------------------------------------------------------\n");
for( i=0;i {
printf("%10s%10s%8.2f%8.2f%8.2f%8.2f%10.2f%10.2f\n",student[i].Stu_ID,student[i].Stu_Name,
student[i].math,student[i].english,student[i].chinese,student[i].computer,student[i].average,student[i].total);
}
printf("------------------------------------------------------------------------------\n");
fclose(fp);
}
/**将文件写入文本*/
void Stu_ioWrite()
{
FILE *fp;
fflush(stdin);
if((fp=fopen("Data.txt","wb+"))==NULL)
{
printf("文件保存失败 \n");
exit (0);
}
for(int i=0;i {
student[i].average=Stu_avg(student[i]);
student[i].total=Stu_total(student[i]);
fprintf(fp,"%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n",student[i].Stu_ID,
student[i].Stu_Name,student[i].math,student[i].english,student[i].chinese,student[i].computer,
student[i].average,student[i].total);
}
printf("文件保存成功\n");
fclose(fp);
}
/**将文件复制到其他文本中*/
void Stu_ioCopy()
{
FILE *fp1,*fp2;
char ch;
if ((fp1 = fopen("Database.txt", "rb")) == NULL)
{
printf("无法打开源文件!\n");
exit(0);
}
if ((fp2 = fopen("F:\\data.txt", "wb")) == NULL)
{
printf("无法打开目标文件!\n");
fclose(fp1);
exit(0);
}
while (!feof(fp1))
{
ch = fgetc(fp1);
if (ch != EOF)
fputc(ch, fp2);
}
printf("拷贝成功!\n");
fclose(fp1);
fclose(fp2);
}
/**各科成绩分类合计*/
void Stu_fenleiTotal()
{
printf("对所以科目进行分类合计:\n");
//struct Student stu;
/**对分类合计数据进行初始化*/
float fenleiTotalMath=0;/**分类合计数学成绩*/
float fenleiTotalEnglish=0;/**合计英语成绩*/
float fenleiTotalComputer=0;/**合计计算机成绩*/
float fenleiTotalChinese=0;/**合计语文成绩*/
for(int i=0;i<=Stu_num;i++)
{
fenleiTotalChinese+=student[i].chinese;
fenleiTotalComputer+=student[i].computer;
fenleiTotalEnglish+=student[i].english;
fenleiTotalMath+=student[i].math;
}
printf("%10s%10s%10s%12s\n","数学合计","英语合计","语文合计","计算机合计");
printf("-------------------------------------------------------------\n");
printf("%10.2f%10.2f%10.2f%12.2f\n",fenleiTotalMath,fenleiTotalEnglish,fenleiTotalChinese,fenleiTotalComputer);
printf("-------------------------------------------------------------\n");
}
/**显示所有学生信息*/
void Stu_displayByAll()
{
int i;
printf("%10s%10s%8s%8s%8s%8s%10s%10s\n","学号","姓名","数学","英语","语文","计算机","平均成绩","总分");
printf("------------------------------------------------------------------------------\n");
for(i=0;i {
//Stu_displayBySingle(i);/**调用显示单条记录信息函数*/
printf("%10s%10s%8.2f%8.2f%8.2f%8.2f%10.2f%10.2f\n",student[i].Stu_ID,student[i].Stu_Name,
student[i].math,student[i].english,student[i].chinese,student[i].computer,student[i].average,student[i].total);
}
printf("-----------------------------------------------------------------------------\n");
}
/**建立索引方便查找信息如:通过索引你可以了解到某学生的某科课程成绩或者他的总分*/
void Stu_createIndex()
{
/**一般通过学号建立索引(索引要求唯一,而学号唯一)*/
char num[N];
int index;
int c;
while(true)
{
printf("请输入你想了解的学生的学号:");
scanf("%s",&num);
index=Stu_indexByNum(num);
int create;
if(index==-1)
{
printf("该学生不存在。");
}
else
{
printf("---------------------------------------------------------------------\n");
printf("\t\t\t< 1>,输出你要找的学生姓名:\n");
printf("\t\t\t< 2>,输出你要找的学生数学成绩:\n");
printf("\t\t\t< 3>,输出你要找的学生英语成绩:\n");
printf("\t\t\t< 4>,输出你要找的学生语文成绩:\n");
printf("\t\t\t< 5>,输出你要找的学生计算机成绩:\n");
printf("\t\t\t< 6>,输出你要找的学生平均成绩:\n");
printf("\t\t\t< 7>,输出你要找的学生总成绩:\n");
printf("---------------------------------------------------------------------\n");
printf("请输入你要了解的信息输入<1--7>如:(查询该学生的计算机成绩输入(5))");
scanf("%d",&create);
switch(create)/**通过数组建立索引很有优势*/
{
case 1:{
printf("姓名:");
printf("%s\n",student[index].Stu_Name);
};break;
case 2:{
printf("%s号数学成绩:",num);
printf("%0.2f\n",student[index].math);
}break;
case 3:{
printf("%s号英语成绩:",num);
printf("%0.2f\n",student[index].english);
};break;
case 4:{
printf("%s号语文成绩:",num);
printf("%0.2f\n",student[index].chinese);
};break;
case 5:{
printf("%s号计算机成绩:",num);
printf("%0.2f\n",student[index].computer);
};break;
case 6:{
printf("%s号平均成绩:",num);
printf("%0.2f\n",student[index].average);
};break;
case 7:{
printf("%s号总成绩:",num);
printf("%0.2f\n",student[index].total);
};break;
}
printf("查询成功。\n");
}
printf("是否继续:是(1)否(2)");
scanf("%d",&c);
if(c==2)
{
printf("详细查询结束。\n");
break;
}
}
}
/**退出使用功能*/
void Stu_Exit()
{
int c;
printf("你确定要退出此应用程序?是(1)否(2)");
scanf("%d",&c);
if(c==1)
{
printf("退出此程序,谢谢使用……");
exit(0);
}
}
/**主体程序*/
int _tmain(int argc, _TCHAR* argv[])
{
int choice;
int c;
while(true)
{ /**显示界面*/
printf("*********************欢迎使用学生成绩管理系统*****************************\n");
printf("********************* made by li yuanjie *****************************\n");
printf("请选择你需要的服务(以回车键结束):\n");
printf("\t\t\t< 0>,读取学生已保存文件信息:\n");
printf("\t\t\t< 1>,输入学生信息服务:\n");
printf("\t\t\t< 2>,查询学生信息服务:\n");
printf("\t\t\t< 3>,修改学生信息服务:\n");
printf("\t\t\t< 4>,删除学生信息服务:\n");
printf("\t\t\t< 5>,输出全部学生信息:\n");
printf("\t\t\t< 6>,初始化学生信息:\n");
printf("\t\t\t< 7>,保存学生个人信息:\n");
printf("\t\t\t< 8>,拷贝学生信息服务:\n");
printf("\t\t\t< 9>,分类合计信息服务:\n");
printf("\t\t\t<10>,对学生的成绩排序:\n");
printf("\t\t\t<11>,显示有不及格记录学生以及\n");
printf("\t\t\t 根据分数线查找超过XXX分的学生信息:\n");
printf("\t\t\t<12>,通过索引查找详细信息:\n");
printf("\t\t\t<13>,追加新学生信息:\n");
printf("\t\t\t<14>,插入学生成绩信息\n");
printf("\t\t\t<15>,退出学生管理系统:\n");
printf("*******************************e n d***********************************\n");
printf("你需要的服务为<0---15>:");
scanf("%d",&choice);
/**通过开关语句控制操作*/
switch(choice)
{
case 0:Stu_ioRead();break;
case 1:Stu_Add();break;
case 2:{
printf("按姓名查询还是按学号查询:姓名输入(1),学号输入(2)");
scanf("%d",&c);
if(c==1)
{
printf("按照姓名查找学生信息:\n");
Stu_searchByName();
}
else if(c==2)
{
printf("按照学号查找学生信息:\n");
Stu_searchByNum();
}
};break;
case 3:Stu_Modify();break;
case 4:Stu_deleteByNum();break;
case 5:Stu_displayByAll();break;
case 6:Stu_sortNum();break;
case 7:Stu_ioWrite();break;
case 8:Stu_ioCopy();break;
case 9:Stu_fenleiTotal();break;
case 10:Stu_sortByTotal();
;break;
case 11:{
printf("显示不及格学生信息还是查询分数超过XXX的学生信息:不及格学生(1)查分数超过XXX分的学生(2)");
scanf("%d",&c);
if(c==1)
{
printf("显示不及格学生信息\n");
Stu_notPass();
}else if(c==2)
{
Stu_overScore();
}
};break;
case 12:Stu_createIndex();break;
case 13:Stu_insertByNum();break;
case 14:Stu_insertMiddle();break;
case 15:Stu_Exit();break;
}
}
return 0;
}