#include <stdio.h>windows
#include <windows.h>函數
#include <malloc.h>優化
#include <conio.h>spa
FILE *sco;ip
FILE *bsc;get
void print_(){ //修飾
for( int i=0; i<110; i++ )
{
printf("*");it
Sleep(10); //間隔輸出
}
printf("\n"); io
}file
void home_page() //主界面
{
system ( "color 5b" ); //背景顏色gc
print_(); //調用修飾函數
printf("%48c 學生管理系統\n",' '); //%45c ,' '輸入空格
printf("%45c 1 查找\n",' ');
printf("%45c 2 添加\n",' ');
printf("%45c 3 刪除\n",' ');
printf("%45c 4 修改\n",' ');
printf("%45c 5 瀏覽成績\n",' ');
printf("%45c 6 瀏覽信息\n",' ');
printf("%45c 7 保存\n",' ');
printf("%45c 8 文件讀取\n",' ');
printf("%45c 9 退出\n",' ');
print_();
printf("請選擇:");
}
typedef char Str[100];
typedef struct Student_message //學生信息結構體
{
Str Name;
//char Name[20]; //姓名
Str Number;
//char Number[8]; //學號
Str Phone;
//char Phone[12]; //電話
Str Scholarship;
//char Scholarship[7]; //獎學金狀況
struct Student_message *Next;
}Student_message,*_message;
typedef struct Student_score //學生成績結構體
{
Str name; //姓名
Str Adv;
//char Adv[4]; //高數
Str Lin;
//char Lin[4]; //線代
Str Eng;
//char Eng[4]; //大英
Str Pro;
//char Pro[4]; //程序
struct Student_score *Next;
}Student_score,*_score;
// struct Student_score *Sco=0;
char *Filename1="學生信息.txt";
char *Filename2="學生成績.txt";
_message Read_file1(char *filename1)
{
_message stu_temp;
_message stu_head = NULL;
bsc=fopen(filename1,"r");
if(!bsc)
{
printf("%s文件不存在!!!\n",filename1);
return stu_head;
}
while(!feof(bsc))
{
stu_temp = (_message)calloc(1,sizeof(Student_message));
fscanf(bsc,"%s %s %s %s\n",stu_temp->Name,stu_temp->Number,stu_temp->Phone,stu_temp->Scholarship);
stu_temp->Next=stu_head;
stu_head = stu_temp;
}
fclose(bsc); //關閉文件
printf("讀取完畢\n");
return stu_head;
}
_score Read_file2(char *filename2)
{
_score stu_temp;
_score stu_head = NULL;
sco=fopen(filename2,"r");
if(!sco)
{
printf("%s文件不存在!!!\n",filename2);
return stu_head;
}
while(!feof(sco))
{
stu_temp = (_score)calloc(1,sizeof(Student_score));
fscanf(sco,"%s %s %s %s %s\n",stu_temp->name,stu_temp->Adv,stu_temp->Eng,stu_temp->Lin,stu_temp->Pro);
stu_temp->Next=stu_head;
stu_head = stu_temp;
}
fclose(sco); //關閉文件
printf("讀取完畢\n");
return stu_head;
}
void Save_file1(char *filename1, _message mes_head) //保存信息至文件
{
_message stu_cur = mes_head;
if(!mes_head)
{
return;
}
if(!(bsc = fopen(filename1,"w")))
{
bsc = fopen(filename1,"a+");
}
while(stu_cur)
{
fprintf(bsc,"%s %s %s %s\n",stu_cur->Name,stu_cur->Number,stu_cur->Phone,stu_cur->Scholarship);
stu_cur = stu_cur->Next;
}
fclose(bsc);
printf("保存完畢\n");
}
void Save_file2(char *filename2, _score sco_head) //保存信息至文件
{
_score stu_cur = sco_head;
if(!sco_head)
{
return;
}
if(!(sco = fopen(filename2,"w")))
{
sco = fopen(filename2,"a+");
}
while(stu_cur)
{
fprintf(sco,"%s %s %s %s %s\n",stu_cur->name,stu_cur->Adv,stu_cur->Eng,stu_cur->Lin,stu_cur->Pro);
stu_cur = stu_cur->Next;
}
fclose(sco);
printf("保存完畢\n");
}
void Search_message(_message mes_head) //查找學生信息
{
_message stu_cur = mes_head;
Str msg;
if(!mes_head)
{
printf("\n當前無任何信息記錄!!!\n");
return;
}
printf("\n輸入要查找學生姓名: "),scanf("%s",msg);
while(stu_cur)
{
if(!strcmp(stu_cur->Name,msg))
{
printf("姓名: %s\n",stu_cur->Name);
printf("學號: %s\n",stu_cur->Number);
printf("電話: %s\n",stu_cur->Phone);
printf("獎學金信息: %s\n",stu_cur->Scholarship);
return;
}
stu_cur = stu_cur->Next;
}
printf("輸入的信息不存在!!!\n");
}
void Search_score(_score sco_head) //查找學生信息
{
_score stu_cur = sco_head;
Str sco;
if(!sco_head)
{
printf("\n當前無任何信息記錄!!!\n");
return;
}
printf("\n輸入要查找學生姓名: "),scanf("%s",sco);
while(stu_cur)
{
if(!strcmp(stu_cur->name,sco))
{
printf("姓名: %s\n",stu_cur->name);
printf("高數: %s\n",stu_cur->Adv);
printf("大英: %s\n",stu_cur->Eng);
printf("線代: %s\n",stu_cur->Lin);
printf("程序: %s\n",stu_cur->Pro);
return;
}
stu_cur = stu_cur->Next;
}
printf("輸入的信息不存在!!!\n");
}
_message Add_message(_message mes_head) //添加學生信息
{
_message stu_temp = (_message)calloc(1,sizeof(Student_message));
printf("\n姓名: "),scanf("%s",stu_temp->Name);
printf("學號: "),scanf("%s",stu_temp->Number);
printf("電話: "),scanf("%s",stu_temp->Phone);
printf("獎學金信息(若無請輸入\"無\"): "),scanf("%s",stu_temp->Scholarship);
stu_temp->Next = mes_head;
return stu_temp;
}
_score Add_score(_score sco_head) //添加學生信息
{
_score stu_temp = (_score)calloc(1,sizeof(Student_score));
printf("\n姓名: "),scanf("%s",stu_temp->name);
printf("高數: "),scanf("%s",stu_temp->Adv);
printf("大英: "),scanf("%s",stu_temp->Eng);
printf("線代: "),scanf("%s",stu_temp->Lin);
printf("程序: "),scanf("%s",stu_temp->Pro);
stu_temp->Next = sco_head;
return stu_temp;
}
_message Delete_message(_message mes_head) //刪除學生信息
{
_message stu_temp,stu_cur = mes_head;
Str msg;
if(!mes_head)
{
printf("\n當前無任何信息記錄!!!\n");
return mes_head;
}
printf("\n請輸入要刪除的學生姓名: "),scanf("%s",msg);
if(!strcmp(mes_head->Name,msg))
{
stu_temp = mes_head;
mes_head = stu_temp->Next;
free(stu_temp);
printf("刪除成功!!!\n");
return mes_head;
}
while(stu_cur->Next)
{
if(!strcmp(stu_cur->Next->Name,msg))
{
stu_temp = stu_cur->Next;
stu_cur->Next = stu_temp->Next;
free(stu_temp);
printf("刪除成功!!!\n");
return mes_head;
}
stu_cur = stu_cur->Next;
}
printf("輸入的信息不存在!!!\n");
return mes_head;
}
_score Delete_score(_score sco_head) //刪除學生信息
{
_score stu_temp,stu_cur = sco_head;
Str sco;
if(!sco_head)
{
printf("\n當前無任何信息記錄!!!\n");
return sco_head;
}
printf("\n請輸入要刪除的學生姓名: "),scanf("%s",sco);
if(!strcmp(sco_head->name,sco))
{
stu_temp = sco_head;
sco_head = stu_temp->Next;
free(stu_temp);
printf("刪除成功!!!\n");
return sco_head;
}
while(stu_cur->Next)
{
if(!strcmp(stu_cur->Next->name,sco))
{
stu_temp = stu_cur->Next;
stu_cur->Next = stu_temp->Next;
free(stu_temp);
printf("刪除成功!!!\n");
return sco_head;
}
stu_cur = stu_cur->Next;
}
printf("輸入的信息不存在!!!\n");
return sco_head;
}
void Modify_message(_message mes_head) //修改學生信息
{
_message stu_cur = mes_head;
Str msg;
if(!mes_head)
{
printf("\n當前無任何信息記錄!!!\n");
return;
}
printf("\n請輸入要修改的學生姓名: "),scanf("%s",msg);
while(stu_cur)
{
if(!strcmp(stu_cur->Name,msg))
{
printf("請輸入信息替換:\n\n");
printf("\n姓名: "),scanf("%s",stu_cur->Name);
printf("學號: "),scanf("%s",stu_cur->Number);
printf("電話: "),scanf("%s",stu_cur->Phone);
printf("獎學金信息(若無請輸入\"無\"): "),scanf("%s",stu_cur->Scholarship);
return;
}
stu_cur = stu_cur->Next;
}
printf("輸入的信息不存在!!!\n");
return;
}
void Modify_score(_score sco_head) //修改學生信息
{
_score stu_cur = sco_head;
Str sco;
if(!sco_head)
{
printf("\n當前無任何信息記錄!!!\n");
return;
}
printf("\n請輸入要修改的學生姓名: "),scanf("%s",sco);
while(stu_cur)
{
if(!strcmp(stu_cur->name,sco))
{
printf("請輸入信息替換:\n\n");
printf("\n姓名: "),scanf("%s",stu_cur->name);
printf("高數: "),scanf("%s",stu_cur->Adv);
printf("大英: "),scanf("%s",stu_cur->Eng);
printf("線代: "),scanf("%s",stu_cur->Lin);
printf("程序: "),scanf("%s",stu_cur->Pro);
return;
}
stu_cur = stu_cur->Next;
}
printf("輸入的信息不存在!!!\n");
return;
}
void Display_message(_message mes_head) //顯示學生信息
{
_message mes_cur = mes_head;
if(!mes_head)
{
printf("\n當前無任何信息記錄!!!\n");
return;
}
while(mes_cur)
{
printf("\n姓名: %s\n",mes_cur->Name);
printf("學號: %s\n",mes_cur->Number);
printf("電話: %s\n",mes_cur->Phone);
printf("獎學金信息: %s\n",mes_cur->Scholarship);
mes_cur = mes_cur->Next;
}
}
void Display_score(_score sco_head) //顯示學生信息
{
_score sco_cur = sco_head;
if(!sco_head)
{
printf("\n當前無任何信息記錄!!!\n");
return;
}
while(sco_cur)
{
printf("\n姓名: %s\n",sco_cur->name);
printf("高數: %s\n",sco_cur->Adv);
printf("大英: %s\n",sco_cur->Eng);
printf("線代: %s\n",sco_cur->Lin);
printf("程序: %s\n",sco_cur->Pro);
sco_cur = sco_cur->Next;
}
}
int main( int argc, char const *argv[] )
{
char choice;
_message mes_head = NULL;
_score sco_head = NULL;
while(1)
{
home_page(); //進入主頁面
choice = getch();
system("cls");
switch(choice) //進行選擇
{
case '1': Search_message(mes_head);
Search_score(sco_head);
break;
case '2': mes_head = Add_message(mes_head);
sco_head = Add_score(sco_head);
break;
case '3': mes_head = Delete_message(mes_head);
sco_head = Delete_score(sco_head);
break;
case '4': Modify_message(mes_head);
Modify_score(sco_head);
break;
case '5': Display_score(sco_head);
break;
case '6': Display_message(mes_head);
break;
case '7': Save_file1(Filename1,mes_head);
Save_file2(Filename2,sco_head);
break;
case '8': mes_head = Read_file1(Filename1);
sco_head = Read_file2(Filename2);
break;
case '9': exit(0);
break;
default: break;
}
}
return 0;
}
/*
#include <stdio.h>
#include <windows.h>
int main ( int argc, char const *argv[] )
{
char a1[7]="hello ",a2[7]="world!";
char *p1=&a1[0], *p2=&a2[0];
for(int i=0 ; i<7 ; i++)
{
printf( "%c",*p1++ );
Sleep(100);
}
for( i=0 ; i<7 ; i++)
{
printf( "%c",*p2++ );
Sleep(100);
}
printf( "\n" );
return 0;
}
*/
讀完這個項目,功能已標註,我認爲還能夠優化一下幾點
1:可增長更多學生信息項目
2:可更改查找功能的查找方式 僅須要查詢一次便可顯示所有信息
3:查詢成績功能稍顯多餘
4:優化界面