📄 student_managerment_system.cpp
字号:
#include "stdio.h"
#include "math.h"
#include "string.h"
#include "stdlib.h"
#include "malloc.h"
#include "time.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define NULL 0
#define MAX 100 //设置信息库最大的容量。可根据情况修改。
FILE * fp;
char ch;
int NUM; //用全局变量NUM记录以填入的记录的数目
struct student_type //结构体定义学生信息存储体
{
int num;
char name[10];
float score;
}student[MAX],temp;
int from_keyboard() //从键盘输入信息的子函数
{
int i=0;
// printf("standard format :(number-name-score)one line per student.Please do follow the standerd format\n");
while(student[i].num!=0)
i++; //找到当前第一个空记录的位置
new_record: printf("please input the number of this student:\n");
scanf("%d",&student[i].num);
printf("please input the name of this student:\n");
scanf("%s",&student[i].name);
printf("please input the score of this student:\n");
scanf("%f",&student[i].score);
printf("A new record has been successfully put in.\npress 'c' to put in another record,'s' to stop and return to menu\n");
retry: ch=getchar();
if (ch=='c')
{
i++;
goto new_record;
}
else if(ch=='s')
return TRUE;
else goto retry;
printf("------------------------------------------------------\n");
}
int from_file() // 从文件读入信息的子函数
{
int i=0,j=0;
char FILENAME[30];
try_again: printf("please input the name of the source file(with path)\n");
scanf("%s",FILENAME);
if((fp=fopen("FILENAME","r"))==NULL)
{
printf("cann't open the file.please check your input \n");
printf("press 'c' to try again or 's' to get back to the menu\n");
retry_2:ch=getchar();
if(ch=='s')
return FALSE;
else if(ch=='c')
goto try_again;
else goto retry_2;
}
while(ch=getc(fp)!=EOF)
{
student[i].num=getw(fp);
fgets(student[i].name,10,fp);
fscanf(fp,"%f",&student[i].score);
}
return TRUE;
printf("------------------------------------------------------\n");
}
void num_man_and_boy_sort() //按学号由大到小排序子函数
{
int i=0,j=0;
struct student_type temp;
while(student[i].num!=0)
i++; //找到当前第一个空记录的位置
NUM=i;
for(i=0;i<NUM;i++)
for(j=i;j<NUM;j++)
{
if(student[i].num<student[j].num)
{
temp.num=student[i].num;
strcpy(temp.name,student[i].name);
temp.score=student[i].score;
student[i].num=student[j].num;
strcpy(student[i].name,student[j].name);
student[i].score=student[j].score;
student[j].num=temp.num;
strcpy(student[j].name,temp.name);
student[j].score=temp.score;
}
}
printf("排序完毕。下面是经过排序后的数据:\n学号\t姓名\t加权平均成绩\n");
for(i=0;i<NUM;i++)
printf("%d\t%s\t%f\n",&student[i].num,student[i].name,student[i].score);
printf("------------------------------------------------------\n");
}
void name_man_and_boy_sort() //按名字由大到小排序子函数
{
int i=0,j=0;
struct student_type temp;
while(student[i].num!=0)
i++; //找到当前第一个空记录的位置
NUM=i;
for(i=0;i<NUM;i++)
for(j=i;j<NUM;j++)
{
if(strcmp(student[i].name,student[j].name)<0)
{
temp.num=student[i].num;
strcpy(temp.name,student[i].name);
temp.score=student[i].score;
student[i].num=student[j].num;
strcpy(student[i].name,student[j].name);
student[i].score=student[j].score;
student[j].num=temp.num;
strcpy(student[j].name,temp.name);
student[j].score=temp.score;
}
}
printf("排序完毕。下面是经过排序后的数据:\n学号\t姓名\t加权平均成绩\n");
for(i=0;i<NUM;i++)
printf("%d\t%s\t%f\n",&student[i].num,student[i].name,student[i].score);
printf("------------------------------------------------------\n");
}
void score_man_and_boy_sort() //按加权平均成绩由大到小排序子函数
{
int i=0,j=0;
struct student_type temp;
while(student[i].num!=0)
i++; //找到当前第一个空记录的位置
NUM=i;
for(i=0;i<NUM;i++)
for(j=i;j<NUM;j++)
{
if(student[i].score<student[j].score)
{
temp.num=student[i].num;
strcpy(temp.name,student[i].name);
temp.score=student[i].score;
student[i].num=student[j].num;
strcpy(student[i].name,student[j].name);
student[i].score=student[j].score;
student[j].num=temp.num;
strcpy(student[j].name,temp.name);
student[j].score=temp.score;
}
}
printf("排序完毕。下面是经过排序后的数据:\n学号\t姓名\t加权平均成绩\n");
for(i=0;i<NUM;i++)
printf("%d\t%s\t%f\n",&student[i].num,student[i].name,student[i].score);
printf("------------------------------------------------------\n");
}
void num_retrorse_sort() //按学号由小到大反序排序子函数
{
int i=0,j=0;
struct student_type temp;
while(student[i].num!=0)
i++; //找到当前第一个空记录的位置
NUM=i;
for(i=0;i<NUM;i++)
for(j=i;j<NUM;j++)
{
if(student[i].num>student[j].num)
{
temp.num=student[i].num;
strcpy(temp.name,student[i].name);
temp.score=student[i].score;
student[i].num=student[j].num;
strcpy(student[i].name,student[j].name);
student[i].score=student[j].score;
student[j].num=temp.num;
strcpy(student[j].name,temp.name);
student[j].score=temp.score;
}
}
printf("排序完毕。下面是经过排序后的数据:\n学号\t姓名\t加权平均成绩\n");
for(i=0;i<NUM;i++)
printf("%d\t%s\t%f\n",&student[i].num,student[i].name,student[i].score);
printf("------------------------------------------------------\n");
}
void name_retrorse_sort() //按名字由小到大反序排序子函数
{
int i=0,j=0;
struct student_type temp;
while(student[i].num!=0)
i++; //找到当前第一个空记录的位置
NUM=i;
for(i=0;i<NUM;i++)
for(j=i;j<NUM;j++)
{
if(strcmp(student[i].name,student[j].name)>0)
{
temp.num=student[i].num;
strcpy(temp.name,student[i].name);
temp.score=student[i].score;
student[i].num=student[j].num;
strcpy(student[i].name,student[j].name);
student[i].score=student[j].score;
student[j].num=temp.num;
strcpy(student[j].name,temp.name);
student[j].score=temp.score;
}
}
printf("排序完毕。下面是经过排序后的数据:\n学号\t姓名\t加权平均成绩\n");
for(i=0;i<NUM;i++)
printf("%d\t%s\t%f\n",&student[i].num,student[i].name,student[i].score);
printf("------------------------------------------------------\n");
}
void score_retrorse_sort() //按加权平均成绩由小到大反序排序子函数
{
int i=0,j=0;
struct student_type temp;
while(student[i].num!=0)
i++; //找到当前第一个空记录的位置
NUM=i;
for(i=0;i<NUM;i++)
for(j=i;j<NUM;j++)
{
if(student[i].score>student[j].score)
{
temp.num=student[i].num;
strcpy(temp.name,student[i].name);
temp.score=student[i].score;
student[i].num=student[j].num;
strcpy(student[i].name,student[j].name);
student[i].score=student[j].score;
student[j].num=temp.num;
strcpy(student[j].name,temp.name);
student[j].score=temp.score;
}
}
printf("排序完毕。下面是经过排序后的数据:\n学号\t姓名\t加权平均成绩\n");
for(i=0;i<NUM;i++)
printf("%d\t%s\t%f\n",&student[i].num,student[i].name,student[i].score);
printf("------------------------------------------------------\n");
}
void main_menu() //主菜单
{
printf("\t学生成绩管理信息系统\n");
printf("Please key_in the corresponding number(1-9) to start work:\n");
printf("1.学生信息输入\n2.按学号排序\n3.按姓名排序\n4.按加权平均成绩排序\n5.按学号查询\n6.按姓名查询\n7.按加权平均成绩查询\n8.保存学生信息\n9.退出本系统\n");
printf("------------------------------------------------------\n");
}
void input() //1.学生信息输入
{
int sub_menu;
printf("select the source of the datas\n");
label_1: printf("1.从键盘输入\n2.从文本文件读入\n3.返回主菜单\n");
scanf("%d",&sub_menu);
switch (sub_menu)
{
case 1: from_keyboard();break;
case 2: from_file();break;
case 3: return;break;
default:printf("illegal input!please re_enter the number\n");goto label_1;
}
printf("------------------------------------------------------\n");
}
void number_sort() //2.按学号排序
{
int sub_menu;
printf("select the source of the datas\n");
label_2: printf("1.从小到大排序\n2.从大到小排序\n3.返回子菜单\n");
scanf("%d",&sub_menu);
switch (sub_menu)
{
case 1: num_man_and_boy_sort();break;
case 2: num_retrorse_sort();break;
case 3: return;break;
default:printf("illegal input!please re_enter the number\n");goto label_2;
}
printf("------------------------------------------------------\n");
}
void name_sort() //3.按姓名排序
{
int sub_menu;
printf("select the source of the datas\n");
label_3: printf("1.从小到大排序\n2.从大到小排序\n3.返回子菜单\n");
scanf("%d",&sub_menu);
switch (sub_menu)
{
case 1:name_man_and_boy_sort();break;
case 2:name_retrorse_sort();break;
case 3:return;break;
default:printf("illegal input!please re_enter the number\n");goto label_3;
}
printf("------------------------------------------------------\n");
}
void score_sort() //4.按加权平均成绩排序
{
int sub_menu;
printf("select the source of the datas\n");
label_4: printf("1.从小到大排序\n2.从大到小排序\n3.返回子菜单\n");
scanf("%d",&sub_menu);
switch (sub_menu)
{
case 1:score_man_and_boy_sort();break;
case 2:score_retrorse_sort();break;
case 3:return;break;
default:printf("illegal input!please re_enter the number\n");goto label_4;
}
printf("------------------------------------------------------\n");
}
int number_inquire() //5.按学号查询
{
int i=0,j=0,k;
int m;
while(student[i].num!=0)
i++;
num_man_and_boy_sort(); //查询前先排序
printf("please input the number of the student you want to inquire:\n");
scanf("%d",&m);
while(i>j)
{
k=(i+j)/2;
if(m==student[k].num)
{
printf("data found successfully.list as bellow:\n学号\t姓名\t加权平均成绩\n");
printf("%d\t%s\t%f\n",&student[k].num,student[k].name,student[k].score);
return TRUE;
}
else if(m<student[k].num)
j=k+1;
else
i=k-1;
}
printf("item not found.please check your input and try again\n");
return FALSE;
printf("------------------------------------------------------\n");
}
int name_inquire() //6.按姓名查询
{
int i=0,j=0,k;
char N[10];
while(student[i].num!=0)
i++;
name_man_and_boy_sort(); //查询前先排序
printf("please input the name of the student you want to inquire:\n");
scanf("%S",N);
while(i>j)
{
k=(i+j)/2;
if(strcmp(N,student[k].name)==0)
{
printf("data found successfully.list as bellow:\n学号\t姓名\t加权平均成绩\n");
printf("%d\t%s\t%f\n",&student[k].num,student[k].name,student[k].score);
return TRUE;
}
else if(strcmp(N,student[k].name)<0)
j=k+1;
else
i=k-1;
}
printf("item not found.please check your input and try again\n");
return FALSE;
printf("------------------------------------------------------\n");
}
int score_inquire() //7.按加权平均成绩查询
{
int i=0,j=0,k;
int m;
while(student[i].num!=0)
i++;
score_man_and_boy_sort(); //查询前先排序
printf("please input the score of the student you want to inquire:\n");
scanf("%f",&m);
while(i>j)
{
k=(i+j)/2;
if(m==student[k].score)
{
printf("data found successfully.list as bellow:\n学号\t姓名\t加权平均成绩\n");
printf("%d\t%s\t%f\n",&student[k].num,student[k].name,student[k].score);
return TRUE;
}
else if(m<student[k].score)
j=k+1;
else
i=k-1;
}
printf("item not found.please check your input and try again\n");
return FALSE;
printf("------------------------------------------------------\n");
}
void save() //8.保存学生信息
{
int i=0,j=0;
printf("To save this file,please input a new name for this file\n");
char FILENAME[20];
scanf("%s",FILENAME);
fp=fopen("FILENAME","w+");
while(student[i].num!=0)
i++;
for(j=0;j<i;j++)
{
putw(student[j].num,fp);
fputs(student[j].name,fp);
fprintf(fp,"%f",student[j].score);
}
fclose(fp);
printf("------------------------------------------------------\n");
}
void main(void)
{
int menu,i;
for(i=0;i<MAX;i++)
student[i].num=0; //初始化,将学号全初始化为0,以作为标志位区分是否已填入数据
label: main_menu();
scanf("%d",&menu);
switch(menu)
{
case 1: input();break;
case 2: number_sort();break;
case 3: name_sort();break;
case 4: score_sort();break;
case 5: number_inquire();break;
case 6: name_inquire();break;
case 7: score_inquire();break;
case 8: save();break;
case 9: exit(1);break;
default:printf("illegal input!please re_enter the number\n");goto label;
}
printf("------------------------------------------------------\n");
goto label;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -