//学生信息管理程序源代码(WinTC下运行通过)
#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  /*查询,排序方式,五种*/
{
No,  /*学号方式*/
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 ");
return;
}
if (num+1>maxsize)    /*当前记录个数大于初始化记录条数,追加空间*/
{
t =(student *)realloc(t,maxsize+incsize);  /*追加一个记录所需空间*/
if (!t)      /*内存不足,追加失败*/
{
printf("memory overflow! program abort.\n");
exit(0);
}
}
printf("i
nput the student''s no. , name, math score and program score that you want to add.\n");
if (scanf("%ld%s%d%d",&(),  /*输入*/
temp.name,
&(temp.math),
&(temp.program)))
{
if (findno() == -1)    /*查输入的学号是否与数据库的重复*/
{
t[ num ].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",);/*输入学号已经存在,添加记录失败*/
}
}
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 ");
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 ");
return;
}
switch (mode)  /*判断查询方式*/
{
case no:  /*按学号查询*/
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("inpu
t 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;    /*定义一个中间记录*/
< = t[ j ].no;  /*逐个交换记录的数据项*/
t[ j ].no = t[ i ].no;
t[ i ].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''\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 ");
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''\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 ");
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 \n");
submenu = getchar();
switch (submenu)
{
case ''1'':
stuselect(t,no);
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;
}
}
free(t);/*释放数据库所占空间*/
printf("end ************student information manage system*****\n");
printf("\t\t\t\t\t\tpress any key ");
fflush(stdin);
getch();  /*按任意键返回*/
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。