/*
C/C++ Program Source file.
Program name : Student information manage system.
#include
#include
#include
#include
#include
#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));
if (!t)
{
printf("Memory overflow program abort.");
exit(0);
}
else
{
printf("New database have been created.\n");
dbnull = 0;
return 1;
}
}
void addnew(student *t)
{
student temp;
if (dbnull)
{
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.no;
strcpy(t[i-1].name,t.name);
t[i-1].math = t.math;
t[i-1].program = t.program;
t[i-1].amount = t.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 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);
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
if ((t+count)->no == no)
{
return count;
}
}
return -1;
}
int findmath(student *t,int math)
{
int count =0;
int i =0;
for (count =0; 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
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
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
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
if ( t[j].math > t.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
if ( t[j].program > t.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
if ( t[j].amount > t.amount )
{
swap(t,j,i);
}
}
}
void swap(student *t, int i,int j)
{
student temp;
temp.no = t[j].no;
t[j].no = t.no;
t.no = temp.no;
strcpy(temp.name , t[j].name);
strcpy(t[j].name , t.name);
strcpy(t.name , temp.name);
temp.math = t[j].math;
t[j].math = t.math;
t.math = temp.math;
temp.program = t[j].program;
t[j].program = t.program;
t.program = temp.program;
temp.amount = t[j].amount;
t[j].amount = t.amount;
t.amount = temp.amount;
}
void main() /*Main module*/
{
student *t;
int Menu =0,submenu =0;
printf("\n\t\t********Students information manage system.********\n");
while ( Menu!= '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,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;
}
}
printf("End ************Student information manage system*****\n");
printf("\t\t\t\t\t\tPress any key to exit...");
fflush(stdin);
getch();
}