⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 student.cpp

📁 一个简单的学生信息管理系统,但比较经典.适合学习C/C++入门者作为实战演练,提高自己的编程水平.
💻 CPP
字号:
#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 english;
  int amount; 
  int avg;
  char name[30];
}student; 

int maxsize = INITSIZE; 
int num =0; 
int dbnull = 1; 

enum 
{
Num, 
Name,
Math,
Program,
English,
Amount,
Avg 
}; 


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 findenglish(student *t,int english);
int findamount(student *t,int amount);
int findavg(student *t,int avg);
int findname(student *t,const char *name);
void display(student *t,int no);
void mathsort(student *t);
void programsort(student *t);
void englishsort(student *t);
void amountsort(student *t);
void avgsort(student *t);
void swap(student *t, int i,int j);
void avg(student *t);


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 , program score and english score that you want to add.\n");

if (scanf("%ld%s%d%d%d",&(temp.no),&(temp.name),&(temp.math),&(temp.program),&(temp.english)))
{
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].english = temp.english;

t[num].amount = t[num].math + t[num].program + t[num].english;
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].english = t[i].english;
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 tempenglish;
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); 
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 English: 
printf("Input the a score, program will search students which math scores are higher than it.\n");
scanf("%d",&tempenglish);
count = findenglish(t,tempenglish);
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 English:
englishsort(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; 
}


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 findenglish(student *t,int english)
{
int count =0; 
int i =0;
for (count =0; count<num; count++)
{
if ((t+count)->english > english)
{
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 English: %2d Sum: %3d .\n",
t[no].no,
t[no].name,
t[no].math,
t[no].program,
t[no].english,
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 englishsort(student *t) 
{


int i;
int j;


for ( i =0; i< num-1; i++)
for ( j =i+1; j<num; j++)
{
if ( t[j].english > t[i].english )
{
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.english = t[j].english;
t[j].english = t[i].english;
t[i].english = temp.english;

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

void avg(student *t)
{
	int i,mathmax,mathmin,programmax,programmin,englishmax,englishmin;
	mathmax=mathmin=t[0].math;
	programmax=programmin=t[0].program;
	englishmax=englishmin=t[0].english;
	float avg_math=0,avg_program=0,avg_english=0;
	if(num==0)
		return ;
	for(i=0;i<num;i++)
	{
		avg_math+=t[i].math;
		avg_program+=t[i].program;
		avg_english+=t[i].english;
		if(t[i].math>mathmax)
			mathmax=t[i].math;
		if(t[i].math<mathmin)
			mathmin=t[i].math;

		if(t[i].program>programmax)
			programmax=t[i].program;
		if(t[i].program<programmin)
			programmin=t[i].program;

		if(t[i].english>englishmax)
			englishmax=t[i].english;
		if(t[i].english<englishmin)
			englishmin=t[i].english;
	}
		avg_math/=num;
		avg_program/=num;
		avg_english/=num;
	printf("avg_math\tavg_program\tavg_english \n");
	printf("%f\t%f\t%f\n",avg_math,avg_program,avg_english);
	printf("mathmax\tmathmin\tprogrammax\tprogrammin\tenglishmax\tenglishmin\n");
	printf("%d\t%d\t%d\t\t%d\t\t%d\t\t%d\n",mathmax,mathmin,programmax,programmin,englishmax,englishmin);
}

void main() 
{

student *t; 

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

printf("\n\t\t********Students information manage system.********\n");
while ( Menu!= '7' ) /*选择菜单若为'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>.Avg.\n\
 7>.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,English);
  break;
  case '4':
  scoresort(t,Amount);
  break;
  case '5':
  break;
  default:
  break;
 }

 }
 break;
 case '5':
 if (dbnull)
 {
 printf("Not exsist database select menu 1 to create database...");
 break;
 }
while (submenu != '7') /*进入查询子菜单*/
{
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>English score search.\n\
6>Sum score search.\n\
7>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,English);
break;
case '6':
stuselect(t,Amount);
break;
case '7':
break;
default:
break;
}

}
case '6':
 avg(t);
 break;

case '7':
break;
default:
break;
}


}

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

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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -