?? xueshengxinxiguanlixitong.txt
字號:
#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)); /*分配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 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("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; /*搜索完畢,仍沒有匹配學號,則返回-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[ 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 {
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 {
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,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 to exit...");
fflush(stdin);
getch(); /*按任意鍵返回*/
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -