吉林市龙潭山简介:c语言编程成绩管理

来源:百度文库 编辑:杭州交通信息网 时间:2024/03/29 06:25:44

#include <stdio.h> /*标准输入输出库*/
#include <string.h> /*字符串操作库*/
#include <conio.h> /*控制台函数库*/
#include <malloc.h> /*内存分配库*/
#include <process.h> /*进程库*/

#define INITSIZE 100 /*初始化学生记录的条数*/
#define INCSIZE sizeof(student) /*初始化空间不足时,增加存储空间的字节数*/

typedef struct
{
long no; /*学生序号*/
int math; /*数学成绩*/
int program; /*程序设计成绩*/
int amount; /*总分*/
char name[30]; /*学生姓名*/
}student; /*学生结构体*/

int maxsize = INITSIZE; /*初始化记录条数*/
int num =0; /*当前学生记录条数*/
int dbnull = 1; /*数据库是否存在的标志*/

enum /*查询,排序方式,五种*/
{
Num, /*学号方式*/
Name, /*姓名方式*/
Math, /*数学成绩方式*/
Program, /*程序设计成绩方式*/
Amount /*总分方式*/
};

/*以下为所有函数的声明*/
int createset(student **t);
void addnew(student *t);
void deletestu(student *t);
void stuselect(student *t,int mode);
void scoresort(student *t,int mode);
int findno(student *t,int no);
int findmath(student *t,int math);
int findprogram(student *t,int program);
int findamount(student *t,int amount);
int findname(student *t,const char *name);
void display(student *t,int no);
void mathsort(student *t);
void programsort(student *t);
void amountsort(student *t);
void swap(student *t, int i,int j);

/*以下为函数实现*/
int createset(student **t)/*创建数据记录集*/

{
char ask ;

if (num!=0) /*存在学生记录*/
{
printf("Exsist a data base ,recover it?(Y/N)?");
ask =getch(); /*是否覆盖数据库*/
if (ask == 'y'||ask=='Y')
{
free(*t); /*若选择覆盖,则释放现存存储空间,学生记录个数清零*/
num =0;
}
else
{
return 0; /*不选择覆盖,则退出*/
}
}

*t =(student *)malloc(INITSIZE*sizeof(student)); /*分配INITSIZE个学生记录所需空间*/

if (!t)
{
printf("Memory overflow program abort."); /*内存不足,退出程序*/
exit(0);
}

else
{
printf("New database have been created.\n"); /*分配成功,成功创建数据库*/
dbnull = 0; /*数据库存在标志设为0(表示存在)*/
return 1;
}

}

void addnew(student *t) /*向数据库插入学生记录*/
{
student temp;

if (dbnull) /*数据库存在标志不为0,即无数据库,操作失败*/
{
printf("Not exsist database select menu 1 to create database...");
return;
}
if (num+1>maxsize) /*当前记录个数大于初始化记录条数,追加空间*/
{
t =(student *)realloc(t,maxsize+INCSIZE); /*追加一个记录所需空间*/
if (!t) /*内存不足,追加失败*/
{
printf("Memory overflow! program abort.\n");
exit(0);
}
}

printf("Input the student's No. , name, math score and program score that you want to add.\n");

if (scanf("%ld%s%d%d",&(temp.no), /*输入学生数据*/
temp.name,
&(temp.math),
&(temp.program)))
{
if (findno(t,temp.no) == -1) /*查找输入的学号是否与数据库的重复*/
{
t[num].no = temp.no; /*学号不冲突,则把输入的记录存放到数据库末端*/
strcpy(t[num].name,temp.name);

t[num].math = temp.math;
t[num].program = temp.program;

t[num].amount = t[num].math + t[num].program;
num++; /*当前记录数加一*/

printf("Add sucess!\n");
}

else
{
printf("Exsist the student whom NO. is %d,add fail.\n",temp.no);/*输入学号已经存在,添加记录失败*/
}
}

else
{
printf("Data format error,add fail.\n"); /*输入函数出错,表示输入格式错误,添加失败*/
}

}

void deletestu(student *t) /*从数据库删除某条学生记录*/
{

long delno =0;

int index;

int i =0;

if (dbnull)
{
printf("Not exsist database select menu 1 to create database...");
return;
}

printf("Input the student NO. that you want to delete :\n");

scanf("%ld",&delno); /*输入要删除的学生的学号*/

index = findno(t,delno); /*按学号方式查找该学生是否存在*/

if (index != -1) /*该学生存在,则删除他*/
{

for (i = index+1; i<= num; i++) /*数据库记录前移,完成'删除'操作*/
{

t[i-1].no = t[i].no;
strcpy(t[i-1].name,t[i].name);
t[i-1].math = t[i].math;
t[i-1].program = t[i].program;
t[i-1].amount = t[i].amount;

}
num--; /*当前记录数减一*/

printf("Delete success!\n");

}

else
{
printf("The NO. that you input not exsist delete fail\n"); /*无该学号的学生,删除失败*/
}

}

void stuselect(student *t,int mode) /*搜索数据库*/
{
long tempno =0;
char tempname[30];
int tempmath;
int tempprogram;
int tempamount;
int count =0;

if (dbnull)
{
printf("Not exsist database select menu 1 to create database...");
return;
}

switch (mode) /*判断查询方式*/
{
case Num: /*按学号查询*/
printf("Input the student NO. that you want to search.\n");
scanf("%ld",&tempno); /*输入学号*/
tempno =findno(t,tempno); /*查找该学生*/

if ( tempno!= -1 )
{
printf("Search sucess!.\n");/*查询成功,打印之*/
display(t,tempno);
}
else
{
printf("The NO. that you input not exsist search fail.\n"); /*查找失败*/
}
break;
case Name: /*按姓名查询*/
printf("Input the student name that you want to search.:\n");
*tempname ='\0';
scanf("%s",tempname);
count = findname(t,tempname); /*返回查询姓名为name的学生记录个数*/
printf("There are %d student have been searched.\n",count);
break;
case Math: /*按数学成绩查询*/
printf("Input the a score, program will search students which math scores are higher than it.\n");
scanf("%d",&tempmath);
count = findmath(t,tempmath);
printf("There are %d student have been searched.\n",count);
break;

case Program: /*按程序设计成绩查询*/

printf("Input the a score, program will search students which programming scores are higher than it.\n");
scanf("%d",&tempprogram);
count = findprogram(t,tempprogram);
printf("There are %d student have been searched.\n",count);
break;

case Amount: /*按总分查询*/
printf("Input the a score, program will search students which sum scores are higher than it\n");
scanf("%d",&tempamount);
count = findamount(t,tempamount);
printf("There are %d student have been searched.\n",count);
break;
default:
break;

}

}

void scoresort(student *t,int mode) /*学生记录排序*/
{
int count =0;

switch (mode) /*选择不同排序方式进行成绩排序*/
{
case Math:
mathsort(t); /*按数学成绩排序*/
break;
case Program: /*按程序设计成绩排序*/
programsort(t);
break;
case Amount: /*按总分排序*/
amountsort(t);
break;
}

printf("Sorting have been finished .flowing is the result:\n");
for (count =0;count< num; count++) /*排序完成后输出排序结果*/
{
display(t,count);
}
}

int findno(student *t,int no)/*按学号查找学生记录*/
{
int count =0;
for (count =0; count<num; count++)
{
if ((t+count)->no == no) /*逐个搜索,若该学生记录的学号等于需要查找的学号,则返回该学号*/
{
return count;
}
}
return -1; /*搜索完毕,仍没有匹配学号,则返回-1*/
}

int findmath(student *t,int math)
{
int count =0; /*按数学成绩查找,这里查找的结果是大于指定数学分数的所有学生记录*/
int i =0;
for (count =0; count<num; count++)
{
if ((t+count)->math > math)
{
display (t,count); /*显示查找结果*/
i++;
}
}

return i; /*返回符合查询条件的学生记录数目*/
}

int findprogram(student *t,int program)/*按程序设计成绩查找学生记录,算法类似上面的模块*/
{
int count =0;
int i =0;
for (count =0; count<num; count++)
{
if ((t+count)->program > program)
{
display(t,count);
i++;
}
}

return i;
}

int findamount(student *t,int amount)/*类似上面的模块*/
{
int count =0;
int i =0;
for (count =0; count<num; count++)
{
if ((t+count)->amount > amount)
{
display(t,count);
i++;
}
}

return i;
}

int findname(student *t,const char *name) /*类似上面的模块*/
{
int count =0;
int i =0;
for (count =0; count<num; count++)
{
if (!strcmp((t+count)->name,name))
{
display(t,count);
i++;
}
}

return i;
}

void display(student *t,int no) /*打印指定学生记录*/
{
printf("NO.: %2ld Name:%10s Math : %2d Programing: %2d Sum: %3d .\n",
t[no].no,
t[no].name,
t[no].math,
t[no].program,
t[no].amount);
}

void mathsort(student *t) /*数学成绩排序,使用选择排序算法*/
{

int i;
int j;

for ( i =0; i< num-1; i++)
for ( j =i+1; j<num; j++)
{
if ( t[j].math > t[i].math )
{
swap(t,j,i);
}
}
}

void programsort(student *t) /*类似数学成绩排序*/
{

int i;
int j;

for ( i =0; i< num-1; i++)
for ( j =i+1; j<num; j++)
{
if ( t[j].program > t[i].program )
{
swap(t,j,i);
}
}
}

void amountsort(student *t) /*类似数学成绩排序*/
{

int i;
int j;

for ( i =0; i< num-1; i++)
for ( j =i+1; j<num; j++)
{
if ( t[j].amount > t[i].amount )
{
swap(t,j,i);
}
}
}

void swap(student *t, int i,int j) /*交换两个学生的记录内容*/
{
student temp; /*定义一个中间记录*/

temp.no = t[j].no; /*逐个交换记录的数据项*/
t[j].no = t[i].no;
t[i].no = temp.no;

strcpy(temp.name , t[j].name);
strcpy(t[j].name , t[i].name);
strcpy(t[i].name , temp.name);

temp.math = t[j].math;
t[j].math = t[i].math;
t[i].math = temp.math;

temp.program = t[j].program;
t[j].program = t[i].program;
t[i].program = temp.program;

temp.amount = t[j].amount;
t[j].amount = t[i].amount;
t[i].amount = temp.amount;
}

void main() /*Main module 主控模块*/
{

student *t; /*定义整个程序学生记录数据块,用指针t标识*/

int Menu =0,submenu =0;/*表示菜单项,主菜单,子菜单*/

printf("\n\t\t********Students information manage system.********\n");
while ( Menu!= '6' ) /*选择菜单若为'6':(退出项),则退出菜单选择*/
{

fflush(stdin); /*清除输入缓冲区*/
submenu =0; /*重置子菜单的选中项*/
printf("\
\n\
1>.New database.\n\
2>.Add data record.\n\
3>.Delete data record.\n\
4>.Sort.\n\
5>.Search.\n\
6>.Exit\n");

printf("\nInput the menu's command...\n");
Menu = getchar(); /*选择菜单*/

switch (Menu) /*按选择的菜单项,执行相应模块*/
{
case '1':
createset(&t);
break;
case '2':
addnew(t);
break;
case '3':
deletestu(t);
break;
case '4':
if (dbnull) /*数据库不存在,不予以处理*/
{
printf("Not exsist database select menu 1 to create database...");
break;
}

while (submenu != '4' )/*进入排序方式的子菜单*/
{
fflush(stdin);
printf("\t****Score sort****\n\
1>.Math score sort.\n\
2>.Programming score sort.\n\
3>.Sum score sort.\n\
4>.Return to main menu.\n");
printf("\n\tInput the menu's command...\n");
submenu = getchar();

switch ( submenu )
{

case '1':
scoresort(t,Math);
break;
case '2':
scoresort(t,Program);
break;
case '3':
scoresort(t,Amount);
break;
case '4':
break;
default:
break;
}

}
break;
case '5':

if (dbnull)
{
printf("Not exsist database select menu 1 to create database...");
break;
}
while (submenu != '6') /*进入查询子菜单*/
{
fflush(stdin);

printf("\t****Student search.*****\n\
1>NO. search.\n\
2>Name search.\n\
3>Math score search.\n\
4>Programming score search.\n\
5>Sum score search.\n\
6>Return to main menu.\n");

printf("\n\tInput the menu command...\n");
submenu = getchar();

switch (submenu)
{
case '1':
stuselect(t,Num);
break;
case '2':
stuselect(t,Name);
break;
case '3':
stuselect(t,Math);
break;
case '4':
stuselect(t,Program);
break;
case '5':
stuselect(t,Amount);
break;
case '6':
break;
default:
break;
}

}

case '6':
break;
default:
break;
}

}

printf("End ************Student information manage system*****\n");

printf("\t\t\t\t\t\tPress any key to exit...");
fflush(stdin);
getch(); /*按任意键返回*/
}