学生成绩管理程序
请选择您的身份:a.学生、b.教师、c.管理员
学生
输入学号
查询个人成绩
教师
a) 打印成绩单:姓名、三科成绩、平均分、总分,按总分降序
b) 查询学生信息
c) 不及格学生信息、成绩
管理员
a) 录入学生信息
b) 修改学生信息
c) 添加学生
d) 删除学生
e) 录入学生成绩
f) 修改学生成绩
g) 打印学生信息
h) 打印成绩单
接下来就直接上代码啦~
#include
#include
#include
#define N 60
typedef struct student
{
char id[6];
char name[40];
char sex;
char TEL[11];
struct student *next;
}sti;
typedef struct studentsc
{
char id[6];
char name[40];
float score[3];
float all;
}sts;
sts scs[N];
int icount=0;
struct student *creat_inf() //尾插法建立链表
{
sti *head,*r,*stu;
int i=0;
char choice;
head=(sti *)malloc(sizeof(sti)); //创建头结点
head->next=NULL;
r=head;
printf("\t************************************\n");
printf("\t | 请录入学生信息 |\n");
printf("\t************************************\n");
do
{
stu=(sti *)malloc(sizeof(sti));
printf("\t************************************\n");
printf("\n\t 第%d个人的信息为:\n",i+1);
printf("\t************************************\n");
printf("\t学号:");
scanf("%s",stu->id);
getchar();
printf("\t姓名:");
scanf("%s",stu->name);
getchar();
printf("\t性别:");
scanf("%c",&stu->sex);
getchar();
printf("\tTEL:");
scanf("%s",stu->TEL);
getchar();
r->next=stu; //尾插新结点
r=stu; //指向尾结点
printf("Continue?(Y/N):");
scanf("%c",&choice);
i++;
icount++;
}while(choice=='Y'||choice=='y');
r->next=NULL;
return head;
}
void save_inf(sti *h) // 将学生信息保存到文件中
{
sti *stu;
FILE *fp;
fp=fopen("information.txt","wt");
if(fp==NULL)
{
printf("\t文件录入出错\n");
exit(1);
}
for(stu=h->next;stu!=NULL;stu=stu->next)
fprintf(fp,"%s %s %c %s ",stu->id,stu->name,stu->sex,stu->TEL);
printf("\t文件录入成功\n");
fclose(fp);
}
sti sc[N];
void firead() //打印学生信息
{
FILE *fp;
int i;
fp=fopen("information.txt","rt");
if(fp==NULL)
{
printf("\t文件打开失败,退出程序\n");
exit(1);
}
i=0;
printf("\t--------------------------------------------------------------\n");
printf("\t---------------------------- ----------------------------\n");
printf("\t| 学生信息如下 |\n");
printf("\t---------------------------- ----------------------------\n");
printf("\t|\t*学号*\t\t*姓名*\t\t*性别*\t\t*联系方式*\t|\n");
while(fscanf(fp,"%s %s %c %s ",sc[i].id,sc[i].name,&sc[i].sex,sc[i].TEL)!=EOF)
{
printf("\t|\t%s\t\t%s\t\t\t%c\t\t\t%s\t \n",sc[i].id,sc[i].name,sc[i].sex,sc[i].TEL);
i++;
}
printf("\t=======================================================================\n");
fclose(fp);
}
void reget_inf() //将学生信息存入文件之结构体数组
{
int i=0;
FILE *fp;
fp=fopen("information.txt","wt");
if(fp==NULL)
{
printf("\t文件打开失败,退出程序\n");
exit(1);
}
while(sc[i].sex!=0)
{
fprintf(fp,"%s %s %c %s ",sc[i].id,sc[i].name,sc[i].sex,sc[i].TEL);
i++;
}
fclose(fp);
}
int change_inf (char id[6]) //修改学生信息
{
printf("\t********** 请再次确认学生信息 **********\n");
firead();
int i;
for(i=0;i<N;i++)
{
if(strcmp(sc[i].id,id)==0)
{
printf("\t***********************************\n");
printf("\t 请输入学生信息 \n");
printf("\t***********************************\n");
printf("\t 学号:");
scanf("%s",sc[i].id);
getchar();
printf("\t 姓名:");
scanf("%s",sc[i].name);
getchar();
printf("\t 性别:");
scanf("%c",&sc[i].sex);
getchar();
printf("\t TEL:");
scanf("%s",sc[i].TEL);
return 1;
}
}
return 0;
}
void add() // 在文件尾部添加一名学生
{
sti ad;
char choice;
FILE *fp;
printf("\t************************************\n");
printf("\t| 请输入学生信息 |\n");
printf("\t************************************\n");
fp=fopen("information.txt","at+");
if(fp==NULL)
{
printf("\t文件录入出错\n");
exit(1);
}
do
{
printf("\t学号:");
scanf("%s",ad.id);
getchar();
printf("\t姓名:");
scanf("%s",ad.name);
getchar();
printf("\t性别:");
scanf("%c",&ad.sex);
getchar();
printf("\tTEL:");
scanf("%s",ad.TEL);
getchar();
fprintf(fp,"%s %s %c %s ",ad.id,ad.name,ad.sex,ad.TEL);
printf("\t************************************\n");
printf("Countine?(Y/N):");
scanf("%c",&choice);
icount++;
}while(choice=='y'||choice=='Y');
fclose(fp);
}
int delete(char id[6]) //删除学生,即用后一个数组覆盖前一个数组
{
int i,j;
for(i=0;i<N;i++)
{
if(strcmp(sc[i].id,id)==0)
{
for(j=i;j<(N-1);j++)
{
sc[j]=sc[j+1];
}
icount--;
return 1;
}
}
return 0;
}
void infile_sc() //将学生成绩录入文件
{
int i=0;
FILE *fp;
fp=fopen("score.txt","wt");
if(fp==NULL)
{
printf("文件打开失败\n");
exit(1);
}
while(scs[i].all!=0)
{
fprintf(fp,"%s %s %.2f %.2f %.2f %.2f ",
scs[i].id,scs[i].name,scs[i].score[0],scs[i].score[1],scs[i].score[2],scs[i].all);
i++;
}
fclose(fp);
}
void sort() // 按总分降序排序
{
int i,j,k;
sts p;
for(i=0;i<N-1;i++)
{
k=i;
for(j=i+1;j<N;j++)
if(scs[k].all<scs[j].all) k=j;
if(k!=i)
{
p=scs[i];
scs[i]=scs[j];
scs[j]=p;
}
}
}
void print_score() //打印学生成绩
{
sts p;
int i=0;
FILE *fp;
fp=fopen("score.txt","rt");
if(fp==NULL)
{
printf("文件打开失败\n");
exit(1);
}
printf("\t----------------------------------------------------------------------\n");
printf("\t-------------------------------- --------------------------------\n");
printf("\t| 本次测试成绩如下 |\n");
printf("\t-------------------------------- --------------------------------\n");
printf("\t| \t*学号*\t \t*姓名*\t \t*语文*\t \t*数学*\t \t*英语*\t \t*总分*|\n");
while(fscanf(fp,"%s %s %f %f %f %f ",
p.id,p.name,&p.score[0],&p.score[1],&p.score[2],&p.all)!=EOF)
{
printf("\t| \t%s\t \t %s\t \t%.2f\t \t%.2f\t \t%.2f\t \t%.2f\t\n",
scs[i].id,scs[i].name,scs[i].score[0],scs[i].score[1],scs[i].score[2],scs[i].all);
i++;
}
fclose(fp);
printf("\t=======================================================================\n");
}
void input_sc() //输入学生成绩
{
int i=0;
FILE *fp;
fp=fopen("score.txt","wt");
if(fp==NULL)
{
printf("文件打开失败\n");
exit(1);
}
for(i=0;sc[i].sex!=0;i++)
{
strcpy(scs[i].id,sc[i].id);
strcpy(scs[i].name,sc[i].name);
printf("\n\n\t********** 请输入该学生的三科成绩 **********\n");
printf("\t| \t*学号*\t \t*姓名*\t \t|\n");
printf("\t|\t%s\t \t%s\n",scs[i].id,scs[i].name);
printf("\t 语文:");
scanf("%f",&scs[i].score[0]);
printf("\t 数学:");
scanf("%f",&scs[i].score[1]);
printf("\t 英语:");
scanf("%f",&scs[i].score[2]);
scs[i].all=scs[i].score[0]+scs[i].score[1]+scs[i].score[2];
printf("\t***********************************\n\n\n");
fprintf(fp,"%s %s %.2f %.2f %.2f %.2f ",
scs[i].id,scs[i].name,scs[i].score[0],scs[i].score[1],scs[i].score[2],scs[i].all);
}
fclose(fp);
}
int change_sc(char id[6]) //更该学生成绩
{
int i;
for(i=0;i<N;i++)
{
if(strcmp(scs[i].id,id)==0)
{
printf("\t***********************************\n");
printf("\t 请输入学生信息 \n");
printf("\t***********************************\n");
printf("\t 学号:%s\n",scs[i].id);
printf("\t 姓名:%s\n",scs[i].name);
printf("\t 语文:");
scanf("%f",&scs[i].score[0]);
printf("\t 数学:");
scanf("%f",&scs[i].score[1]);
printf("\t 英语:");
scanf("%f",&scs[i].score[2]);
printf("\t***********************************\n\n\n");
return 1; //更该成功
}
}
if(i==N) return 0; //未找到该学生
return 2;
}
void Creatstr() //向一个结构体数组中录入学生成绩
{
sts p;
FILE *fp;
int i=0,j;
fp=fopen("score.txt","rt");
if(fp==NULL)
{
printf("\t系统暂无学生信息,请稍后再来查询\n");
exit(1);
}
while(fscanf(fp,"%s %s %f %f %f %f ",
p.id,p.name,&p.score[0],&p.score[1],&p.score[2],&p.all)!=EOF)
{
strcpy(scs[i].id,p.id);
strcpy(scs[i].name,p.name);
for(j=0;j<3;j++) scs[i].score[j]=p.score[j];
scs[i].all=p.all;
i++;
}
fclose(fp);
}
void print_sc() //由结构体数组打印学生信息 方便排序
{
int i=0;
printf("\t=======================================================================\n");
printf("\t| \t*学号*\t \t*姓名*\t \t*语文*\t \t*数学*\t \t*英语*\t \t*总分*|\n");
printf("\t=======================================================================\n");
while(scs[i].all!=0)
{
printf("\t %s\t\t %s\t %.2f\t\t %.2f\t \t%.2f\t \t%.2f \n",
scs[i].id,scs[i].name,scs[i].score[0],scs[i].score[1],scs[i].score[2],scs[i].all);
i++;
}
printf("\t=======================================================================\n");
}
void print_fail() //打印不及格学生名单和信息
{
int i=0;
printf("\t********** 不及格学生信息及成绩如下 **********\n");
printf("\t----------------------------------------------------------------------\n");
printf("\t-------------------------------- --------------------------------\n");
printf("\t| 不及格学生成绩如下 |\n");
printf("\t-------------------------------- --------------------------------\n");
printf("\t| \t*学号*\t \t*姓名*\t \t*语文*\t \t*数学*\t \t*英语*\t \t*总分*|\n");
while(scs[i].all!=0)
{
if(scs[i].all<=180)
printf("\t%s %s %.2f %.2f %.2f %.2f \n",
scs[i].id,scs[i].name,scs[i].score[0],scs[i].score[1],scs[i].score[2],scs[i].all);
i++;
}
printf("\t=======================================================================\n");
}
void student_sc (char ID[6])
{
int i=0;
printf("\t| \t*学号*\t \t*姓名*\t \t*语文*\t \t*数学*\t \t*英语*\t \t*总分*\t|\n");
while(strcmp(scs[i].id,ID)!=0) i++;
printf("\t| \t%s\t \t%s\t \t%.2f\t \t%.2f\t \t%.2f\t\t%.2f|\n",
scs[i].id,scs[i].name,scs[i].score[0],scs[i].score[1],scs[i].score[2],scs[i].all);
printf("\t=======================================================================\n");
printf("\t表现不错哦,继续加油@_@\n");
}
void Administrator() //管理员模块
{
int i,j;
char ID[6],m;
sti *p;
do
{
printf("\t你好!亲爱的管理员\n");
printf("\t======================================================================\n");
printf("\t-------------------------------- --------------------------------\n");
printf("\t| 欢迎使用成绩管理系统 |\n");
printf("\t-------------------------------- --------------------------------\n");
printf("\t----------------------------------------------------------------------\n\n");
printf("\t| 1.录入学生信息 2.修改学生信息 |\n");
printf("\t| 3.添加学生信息 4.删除学生信息 |\n");
printf("\t| 5.录入学生成绩 6.修改学生成绩 |\n");
printf("\t| 7.打印学生信息 8.打印学生成绩 |\n");
printf("\t| 0.退出程序 |\n");
printf("\t======================================================================\n");
printf("\t-------------------------------- --------------------------------\n");
printf("\t======================================================================\n");
printf("\t请输入您的选择(1-9):");
scanf("%d",&i);
switch(i)
{
case 1:
{
p=creat_inf();
save_inf(p);
break;
}
case 2:
{
printf("\t请输入你要修改学生的学号:\n");
scanf("%s",ID);
j=change_inf(ID);
if(j!=0)
{
printf("\t********** 修改学生信息成功 **********\n");
reget_inf();
}
else printf("\t********** 未查询到该学生信息,请检查输入**********\n");
break;
}
case 3:
{
add();
break;
}
case 4:
{
printf(" 请输入你要删除学生的学号:\n");
scanf("%s",ID);
printf("\t确认删除学号为%s的学生?\n",ID);
scanf("%c",&m);
while(m=='y'||m=='Y')
{
j=delete(ID);
if(j!=0)
{
printf("\t********** 删除学生信息成功 **********\n");
reget_inf();
}
else printf("\t********** 未查询到该学生信息,请检查输入**********\n");
}
break;
}
case 5:
{
printf("\t********** 请再次确认学生信息 **********\n");
firead();
input_sc();
break;
}
case 6:
{
printf("\t********** 请再次确认学生信息 **********\n");
firead();
printf(" 请输入你要修改成绩的学生学号:\n");
scanf("%s",ID);
j=change_sc(ID);
if(j==1)
{
sort();
infile_sc();
}
if(j==0) printf("\t********** 未查询到该学生信息,请检查输入**********\n");
break;
}
case 7:
{
firead();
break;
}
case 8:
{
print_score();
break;
}
}
}while(i!=0);
}
void Teacher() //教师模块
{
int i;
do
{
printf("\t您好,亲爱的老师:\n");
printf("\t======================================================================\n");
printf("\t-------------------------------- --------------------------------\n");
printf("\t| 欢迎使用成绩管理系统 |\n");
printf("\t-------------------------------- --------------------------------\n");
printf("\t| 1.打印成绩单 2.打印学生信息 |\n");
printf("\t| 3.不及格学生信 0.退出程序 |\n");
printf("\t-------------------------------- --------------------------------\n");
printf("\t======================================================================\n");
printf("\t请输入您的选择(0-3):");
scanf("%d",&i);
switch(i)
{
case 1:
{
Creatstr();
sort();
print_sc();
break;
}
case 2:
{
firead();
break;
}
case 3:
{
Creatstr();
print_fail();
break;
}
}
}while(i!=0);
}
void Student() //学生模块
{
char id[6];
printf("\t您好,亲爱的同学:\n");
printf("\t================================================================\n");
printf("\t----------------------------- -----------------------------\n");
printf("\t| 欢迎使用成绩管理系统 |\n");
printf("\t----------------------------- -----------------------------\n");
printf("\t\t 请输入您的学号:");
scanf("%s",id);
printf("\t================================================================\n");
printf("\t----------------------------- ----------------------------\n");
printf("\t| 本次测试成绩如下 |\n");
printf("\t----------------------------- ----------------------------\n");
Creatstr();
student_sc(id);
}
int main(void) //通过不同身份选择相映功能
{
int i;
printf("\t您好,请选择您的身份:\n");
printf("\t================================================================\n");
printf("\t----------------------------- -----------------------------\n");
printf("\t| 欢迎使用成绩管理系统 |\n");
printf("\t----------------------------- -----------------------------\n");
printf("\t| 1.教师 |\n");
printf("\t| 2.学生 |\n");
printf("\t| 3.管理员 |\n");
printf("\t----------------------------- -----------------------------\n");
printf("\t================================================================\n");
printf("\t请输入您的选择(1-3):");
scanf("%d",&i);
switch(i)
{
case 1:
{
Teacher();
break;
}
case 2:
{
Student();
break;
}
case 3:
{
Administrator();
break;
}
}
}