學生成績管理系統源程序文件
Ⅰ 學生成績管理系統的源代碼 但是其中缺少了 排序,插入,保存文件和讀文件的函數 求大神幫忙補充!!!!
STUDENT *sort(STUDENT *head)
{int i=0; /*保存名次*/
STUDENT *p1,*p2,*t,*temp; /*定義臨時指針*/
temp=head->next; /*將原表的頭指針所指的下一個結點作頭指針*/
head->next=NULL; /*第一個結點為新表的頭結點*/
while(temp!=NULL) /*當原表不為空時,進行排序*/
{
t=temp; /*取原表的頭結點*/
temp=temp->next; /*原表頭結點指針後移*/
p1=head; /*設定移動指針p1,從頭指針開始*/
p2=head; /*設定移動指針p2做為p1的前驅,初值為頭指針*/
while(t->average<p1->average&&p1!=NULL) /*作成績平均分比較*/
{
p2=p1; /*待排序點值小,則新表指針後移*/
p1=p1->next;
}
if(p1==p2) /*p1==p2,說明待排序點值大,應排在首位*/
{
t->next=p1; /*待排序點的後繼為p*/
head=t; /*新頭結點為待排序點*/
}
else /*待排序點應插入在中間某個位置p2和p1之間,如p為空則是尾部*/
{
t->next=p1; /*t的後繼是p1*/
p2->next=t; /*p2的後繼是t*/
}
}
p1=head; /*已排好序的頭指針賦給p1,准備填寫名次*/
while(p1!=NULL) /*當p1不為空時,進行下列操作*/
{
i++; /*結點序號*/
p1->order=i; /*將結點序號賦值給名次*/
p1=p1->next; /*指針後移*/
}
printf("排序成功 Sorting is sucessful.\n"); /*排序成功*/
return (head);
}
/*插入記錄函數*/
STUDENT *insert(STUDENT *head,STUDENT *mynew)
{STUDENT *p0,*p1,*p2;
int n,sum1,i;
p1=head; /*使p1指向第一個結點*/
p0=mynew; /*p0指向要插入的結點*/
printf("\nPlease enter a mynew record.\n"); /*提示輸入記錄信息*/
printf("輸入學號Enter the num:");
scanf("%s",mynew->num);
printf("輸入名字Enter the name:");
scanf("%s",mynew->name);
printf("Please enter the %d scores.\n",3);
sum1=0; /*保存新記錄的總分,初值為0*/
for(i=0;i<3;i++)
{
do{
printf("成績score%d:",i+1);
scanf("%d",&mynew->score[i]);
if(mynew->score[i]>100||mynew->score[i]<0)
printf("數據錯誤Data error,please enter again.\n");
}while(mynew->score[i]>100||mynew->score[i]<0);
sum1=sum1+mynew->score[i]; /*累加各門成績*/
}
mynew->sum=sum1; /*將總分存入新記錄中*/
mynew->average=(float)sum1/3;
mynew->order=0;
if(head==NULL) /*原來的鏈表是空表*/
{head=p0;p0->next=NULL;} /*使p0指向的結點作為頭結點*/
else
{while((p0->average<p1->average)&&(p1->next!=NULL))
{p2=p1; /*使p2指向剛才p1指向的結點*/
p1=p1->next; /*p1後移一個結點*/
}
if(p0->average>=p1->average)
{if(head==p1)head=p0; /*插到原來第一個結點之前*/
else p2->next=p0; /*插到p2指向的結點之後*/
p0->next=p1;}
else
{p1->next=p0;p0->next=NULL;} /*插到最後的結點之後*/
}
n=n+1; /*結點數加1*/
head=sort(head); /*調用排序的函數,將學生成績重新排序*/
printf("\n學生Student %s 已被更新have been inserted.\n",mynew->name);
printf("不要忘了保存Don't forget to save the mynewfile.\n");
return(head);
}
/*保存數據到文件函數*/
void save(STUDENT *head)
{FILE *fp; /*定義指向文件的指針*/
STUDENT *p; /* 定義移動指針*/
char outfile[10];
printf("輸出文件例如:c:\\score Enter outfile name,forexample c:\\score\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"w"))==NULL) /*為輸出打開一個二進制文件,為只寫方式*/
{
printf("打不開文件Cannot open the file\n");
return; /*若打不開則返回菜單*/
}
printf("\n保存中...Saving the file......\n");
p=head; /*移動指針從頭指針開始*/
while(p!=NULL) /*如p不為空*/
{
fwrite(p,LEN,1,fp); /*寫入一條記錄*/
p=p->next; /*指針後移*/
}
fclose(fp); /*關閉文件*/
printf("保存成功....Save the filesuccessfully!\n");
}
/* 從文件讀數據函數*/
STUDENT *load()
{STUDENT *p1,*p2,*head=NULL; /*定義記錄指針變數*/
FILE *fp; /* 定義指向文件的指針*/
char infile[10];
printf("倒入文件例如:c:\\score Enter infile name,for examplec:\\score\n");
scanf("%s",infile);
if((fp=fopen(infile,"r"))==NULL) /*打開一個二進制文件,為只讀方式*/
{
printf("打不開文件Can not open the file.\n");
return(head);
}
printf("\n尋找文件...Loading the file!\n");
p1=(STUDENT *)malloc(LEN); /*開辟一個新單元*/
if(!p1)
{
printf("內存溢出!Out of memory!\n");
return(head);
}
head=p1; /*申請到空間,將其作為頭指針*/
while(!feof(fp)) /*循環讀數據直到文件尾結束*/
{
if(fread(p1,LEN,1,fp)!=1) break; /*如果沒讀到數據,跳出循環*/
p1->next=(STUDENT *)malloc(LEN); /*為下一個結點開辟空間*/
if(!p1->next)
{
printf("Out of memory!\n");
return (head);
}
p2=p1; /*使p2指向剛才p1指向的結點*/
p1=p1->next; /*指針後移,新讀入數據鏈到當前表尾*/
}
p2->next=NULL; /*最後一個結點的後繼指針為空*/
fclose(fp);
printf("\n你成功的從文件中讀取了數據!\nYou have success to read data fromthe file!\n");
return (head);
}
Ⅱ 學生成績管理系統,C++源代碼
呵呵,好像每個學校的課程設計都差不多
LZ的比我們當時的難一些
Ⅲ 學生信息管理系統最簡單源代碼。
方法一:
1、創建抄一個c語言項目。然後右鍵頭文件,創建一個Stu的頭文件。
Ⅳ c語言編寫一個學生成績管理系統 源碼
/*菜單函數,返回值為整數*/
menu_select()
{
char *menu[]={"***************MENU***************", /*定義菜單字元串數組*/
" 0. init list", /*初始化*/
" 1. Enter list", /*輸入記錄*/
" 2. Delete a record from list", /*從表中刪除記錄*/
" 3. print list ", /*顯示單鏈表中所有記錄*/
" 4. Search record on name", /*按照姓名查找記錄*/
" 5. Save the file", /*將單鏈表中記錄保存到文件中*/
" 6. Load the file", /*從文件中讀入記錄*/
" 7. compute the score", /*計算所有學生的總分和均分*/
" 8. insert record to list ", /*插入記錄到表中*/
" 9. the file to new file", /*復制文件*/
" 10. sort to make new file", /*排序*/
" 11. append record to file", /*追加記錄到文件中*/
" 12. index on nomber", /*索引*/
" 13. total on nomber", /*分類合計*/
" 14. Quit"}; /*退出*/
char s[3]; /*以字元形式保存選擇號*/
int c,i; /*定義整形變數*/
gotoxy(1,25); /*移動游標*/
printf("press any key enter menu......\n"); /*壓任一鍵進入主菜單*/
getch(); /*輸入任一鍵*/
clrscr(); /*清屏幕*/
gotoxy(1,1); /*移動游標*/
textcolor(YELLOW); /*設置文本顯示顏色為黃色*/
textbackground(BLUE); /*設置背景顏色為藍色*/
gotoxy(10,2); /*移動游標*/
putch(0xc9); /*輸出左上角邊框┏*/
for(i=1;i<44;i++)
putch(0xcd); /*輸出上邊框水平線*/
putch(0xbb); /*輸出右上角邊框 ┓*/
for(i=3;i<20;i++)
{
gotoxy(10,i);putch(0xba); /*輸出左垂直線*/
gotoxy(54,i);putch(0xba);
} /*輸出右垂直線*/
gotoxy(10,20);putch(0xc8); /*輸出左上角邊框┗*/
for(i=1;i<44;i++)
putch(0xcd); /*輸出下邊框水平線*/
putch(0xbc); /*輸出右下角邊框┛*/
window(11,3,53,19); /* 製作顯示菜單的窗口,大小根據菜單條數設計*/
clrscr(); /*清屏*/
for(i=0;i<16;i++) /*輸出主菜單數組*/
{
gotoxy(10,i+1);
cprintf("%s",menu[i]);
}
textbackground(BLACK); /*設置背景顏色為黑色*/
window(1,1,80,25); /*恢復原窗口大小*/
gotoxy(10,21); /*移動游標*/
do{
printf("\n Enter you choice(0~14):"); /*在菜單窗口外顯示提示信息*/
scanf("%s",s); /*輸入選擇項*/
c=atoi(s); /*將輸入的字元串轉化為整形數*/
}while(c<0||c>14); /*選擇項不在0~14之間重輸*/
return c; /*返回選擇項,主程序根據該數調用相應的函數*/
}
STUDENT *init()
{
return NULL;
}
/*創建鏈表*/
STUDENT *create()
{
int i; int s;
STUDENT *h=NULL,*info; /* STUDENT指向結構體的指針*/
for(;;)
{
info=(STUDENT *)malloc(sizeof(STUDENT)); /*申請空間*/
if(!info) /*如果指針info為空*/
{
printf("\nout of memory"); /*輸出內存溢出*/
return NULL; /*返回空指針*/
}
inputs("enter no:",info->no,11); /*輸入學號並校驗*/
if(info->no[0]=='@') break; /*如果學號首字元為@則結束輸入*/
inputs("enter name:",info->name,15); /*輸入姓名,並進行校驗*/
printf("please input %d score \n",N); /*提示開始輸入成績*/
s=0; /*計算每個學生的總分,初值為0*/
for(i=0;i<N;i++) /*N門課程循環N次*/
{
do{
printf("score%d:",i+1); /*提示輸入第幾門課程*/
scanf("%d",&info->score[i]); /*輸入成績*/
if(info->score[i]>100||info->score[i]<0) /*確保成績在0~100之間*/
printf("bad data,repeat input\n"); /*出錯提示信息*/
}while(info->score[i]>100||info->score[i]<0);
s=s+info->score[i]; /*累加各門課程成績*/
}
info->sum=s; /*將總分保存*/
info->average=(float)s/N; /*求出平均值*/
info->order=0; /*未排序前此值為0*/
info->next=h; /*將頭結點做為新輸入結點的後繼結點*/
h=info; /*新輸入結點為新的頭結點*/
}
return(h); /*返回頭指針*/
}
/*輸入字元串,並進行長度驗證*/
inputs(char *prompt, char *s, int count)
{
char p[255];
do{
printf(prompt); /*顯示提示信息*/
scanf("%s",p); /*輸入字元串*/
if(strlen(p)>count)printf("\n too long! \n"); /*進行長度校驗,超過count值重輸入*/
}while(strlen(p)>count);
strcpy(s,p); /*將輸入的字元串拷貝到字元串s中*/
}
/*輸出鏈表中結點信息*/
void print(STUDENT *h)
{
int i=0; /* 統計記錄條數*/
STUDENT *p; /*移動指針*/
clrscr(); /*清屏*/
p=h; /*初值為頭指針*/
printf("\n\n\n****************************STUDENT********************************\n");
printf("|rec|nO | name | sc1| sc2| sc3| sum | ave |order|\n");
printf("|---|----------|---------------|----|----|----|--------|-------|-----|\n");
while(p!=NULL)
{
i++;
printf("|%3d |%-10s|%-15s|%4d|%4d|%4d| %4.2f | %4.2f | %3d |\n", i, p->no,p->name,p->score[0],p->score[1],
p->score[2],p->sum,p->average,p->order);
p=p->next;
}
printf("**********************************end*********************************\n");
}
/*刪除記錄*/
STUDENT *delete(STUDENT *h)
{
STUDENT *p,*q; /*p為查找到要刪除的結點指針,q為其前驅指針*/
char s[11]; /*存放學號*/
clrscr(); /*清屏*/
printf("please deleted no\n"); /*顯示提示信息*/
scanf("%s",s); /*輸入要刪除記錄的學號*/
q=p=h; /*給q和p賦初值頭指針*/
while(strcmp(p->no,s)&&p!=NULL) /*當記錄的學號不是要找的,或指針不為空時*/
{
q=p; /*將p指針值賦給q作為p的前驅指針*/
p=p->next; /*將p指針指向下一條記錄*/
}
if(p==NULL) /*如果p為空,說明鏈表中沒有該結點*/
printf("\nlist no %s student\n",s);
else /*p不為空,顯示找到的記錄信息*/
{
printf("*****************************have found***************************\n");
printf("|no | name | sc1| sc2| sc3| sum | ave |order|\n");
printf("|----------|---------------|----|----|----|--------|-------|-----|\n");
printf("|%-10s|%-15s|%4d|%4d|%4d| %4.2f | %4.2f | %3d |\n", p->no,
p->name,p->score[0],p->score[1],p->score[2],p->sum,
p->average,p->order);
printf("********************************end*******************************\n");
getch(); /*壓任一鍵後,開始刪除*/
if(p==h) /*如果p==h,說明被刪結點是頭結點*/
h=p->next; /*修改頭指針指向下一條記錄*/
else
q->next=p->next; /*不是頭指針,將p的後繼結點作為q的後繼結點*/
free(p); /*釋放p所指結點空間*/
printf("\n have deleted No %s student\n",s);
printf("Don't forget save\n");/*提示刪除後不要忘記保存文件*/
}
return(h); /*返回頭指針*/
}
/*查找記錄*/
void search(STUDENT *h)
{
STUDENT *p; /* 移動指針*/
char s[15]; /*存放姓名的字元數組*/
clrscr(); /*清屏幕*/
printf("please enter name for search\n");
scanf("%s",s); /*輸入姓名*/
p=h; /*將頭指針賦給p*/
while(strcmp(p->name,s)&&p!=NULL) /*當記錄的姓名不是要找的,或指針不為空時*/
p=p->next; /*移動指針,指向下一結點*/
if(p==NULL) /*如果指針為空*/
printf("\nlist no %s student\n",s); /*顯示沒有該學生*/
else /*顯示找到的記錄信息*/
{
printf("\n\n*****************************havefound***************************\n");
printf("|nO | name | sc1| sc2| sc3| sum | ave |order|\n");
printf("|----------|---------------|----|----|----|--------|-------|-----|\n");
printf("|%-10s|%-15s|%4d|%4d|%4d| %4.2f | %4.2f | %3d |\n", p->no,
p->name,p->score[0],p->score[1],p->score[2],p->sum,p->average,p->order);
printf("********************************end*******************************\n");
}
}
/*插入記錄*/
STUDENT *insert(STUDENT *h)
{
STUDENT *p,*q,*info; /*p指向插入位置,q是其前驅,info指新插入記錄*/
char s[11]; /*保存插入點位置的學號*/
int s1,i;
printf("please enter location before the no\n");
scanf("%s",s); /*輸入插入點學號*/
printf("\nplease new record\n"); /*提示輸入記錄信息*/
info=(STUDENT *)malloc(sizeof(STUDENT)); /*申請空間*/
if(!info)
{
printf("\nout of memory"); /*如沒有申請到,內存溢出*/
return NULL; /*返回空指針*/
}
inputs("enter no:",info->no,11); /*輸入學號*/
inputs("enter name:",info->name,15); /*輸入姓名*/
printf("please input %d score \n",N); /*提示輸入分數*/
s1=0; /*保存新記錄的總分,初值為0*/
for(i=0;i<N;i++) /*N門課程循環N次輸入成績*/
{
do{ /*對數據進行驗證,保證在0~100之間*/
printf("score%d:",i+1);
scanf("%d",&info->score[i]);
if(info->score[i]>100||info->score[i]<0)
printf("bad data,repeat input\n");
}while(info->score[i]>100||info->score[i]<0);
s1=s1+info->score[i]; /*計算總分*/
}
info->sum=s1; /*將總分存入新記錄中*/
info->average=(float)s1/N; /*計算均分*/
info->order=0; /*名次賦值0*/
info->next=NULL; /*設後繼指針為空*/
p=h; /*將指針賦值給p*/
q=h; /*將指針賦值給q*/
while(strcmp(p->no,s)&&p!=NULL) /*查找插入位置*/
{
q=p; /*保存指針p,作為下一個p的前驅*/
p=p->next; /*將指針p後移*/
}
if(p==NULL) /*如果p指針為空,說明沒有指定結點*/
if(p==h) /*同時p等於h,說明鏈表為空*/
h=info; /*新記錄則為頭結點*/
else
q->next=info; /*p為空,但p不等於h,將新結點插在表尾*/
else
if(p==h) /*p不為空,則找到了指定結點*/
{
info->next=p; /*如果p等於h,則新結點插入在第一個結點之前*/
h=info; /*新結點為新的頭結點*/
}
else
{
info->next=p; /*不是頭結點,則是中間某個位置,新結點的後繼為p*/
q->next=info; /*新結點作為q的後繼結點*/
}
printf("\n ----have inserted %s student----\n",info->name); printf("---Don't forget save---\n"); /*提示存檔*/
return(h); /*返回頭指針*/
}
/*保存數據到文件*/
void save(STUDENT *h)
{
FILE *fp; /*定義指向文件的指針*/
STUDENT *p; /* 定義移動指針*/
char outfile[10]; /*保存輸出文件名*/
printf("Enter outfile name,for example c:\\f1\\te.txt:\n"); /*提示文件名格式信息*/
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL) /*為輸出打開一個二進制文件,如沒有則建立*/
{
printf("can not open file\n");
exit(1);
}
printf("\nSaving file......\n"); /*打開文件,提示正在保存*/
p=h; /*移動指針從頭指針開始*/
while(p!=NULL) /*如p不為空*/
{
fwrite(p,sizeof(STUDENT),1,fp);/*寫入一條記錄*/
p=p->next; /*指針後移*/
}
fclose(fp); /*關閉文件*/
printf("-----save success!!-----\n"); /*顯示保存成功*/
}
/* 從文件讀數據*/
STUDENT *load()
{
STUDENT *p,*q,*h=NULL; /*定義記錄指針變數*/
FILE *fp; /* 定義指向文件的指針*/
char infile[10]; /*保存文件名*/
printf("Enter infile name,for example c:\\f1\\te.txt:\n"); scanf("%s",infile); /*輸入文件名*/
if((fp=fopen(infile,"rb"))==NULL) /*打開一個二進制文件,為讀方式*/
{
printf("can not open file\n"); /*如不能打開,則結束程序*/
exit(1);
}
printf("\n -----Loading file!-----\n");
p=(STUDENT *)malloc(sizeof(STUDENT)); /*申請空間*/
if(!p)
{
printf("out of memory!\n"); /*如沒有申請到,則內存溢出*/
return h; /*返回空頭指針*/
}
h=p; /*申請到空間,將其作為頭指針*/
while(!feof(fp)) /*循環讀數據直到文件尾結束*/
{
if(1!=fread(p,sizeof(STUDENT),1,fp))
break; /*如果沒讀到數據,跳出循環*/
p->next=(STUDENT *)malloc(sizeof(STUDENT)); /*為下一個結點申請空間*/
if(!p->next)
{
printf("out of memory!\n"); /*如沒有申請到,則內存溢出*/
return h;
}
q=p; /*保存當前結點的指針,作為下一結點的前驅*/
p=p->next; /*指針後移,新讀入數據鏈到當前表尾*/
}
q->next=NULL; /*最後一個結點的後繼指針為空*/
fclose(fp); /*關閉文件*/
printf("---You have success read data from file!!!---\n");
return h; /*返回頭指針*/
}
/*追加記錄到文件*/
void append()
{
FILE *fp; /*定義指向文件的指針*/
STUDENT *info; /*新記錄指針*/
int s1,i;
char infile[10]; /*保存文件名*/
printf("\nplease new record\n");
info=(STUDENT *)malloc(sizeof(STUDENT)); /*申請空間*/
if(!info)
{
printf("\nout of memory"); /*沒有申請到,內存溢出本函數結束*/
return ;
}
inputs("enter no:",info->no,11); /*調用inputs輸入學號*/
inputs("enter name:",info->name,15); /*調用inputs輸入姓名*/
printf("please input %d score \n",N); /*提示輸入成績*/
s1=0;
for(i=0;i<N;i++)
{
do{
printf("score%d:",i+1);
scanf("%d",&info->score[i]); /*輸入成績*/
if(info->score[i]>100||info->score[i]<0)printf("bad data,repeat input\n");
}while(info->score[i]>100||info->score[i]<0); /*成績數據驗證*/
s1=s1+info->score[i]; /*求總分*/
}
info->sum=s1; /*保存總分*/
info->average=(float)s1/N; /*求均分*/
info->order=0; /*名次初始值為0*/
info->next=NULL; /*將新記錄後繼指針賦值為空*/
printf("Enter infile name,for example c:\\f1\\te.txt:\n"); scanf("%s",infile); /*輸入文件名*/
if((fp=fopen(infile,"ab"))==NULL) /*向二進制文件尾增加數據方式打開文件*/
{
printf("can not open file\n"); /*顯示不能打開*/
exit(1); /*退出程序*/
}
printf("\n -----Appending record!-----\n");
if(1!=fwrite(info,sizeof(STUDENT),1,fp)) /*寫文件操作*/
{
printf("-----file write error!-----\n");
return; /*返回*/
}
printf("-----append sucess!!----\n");
fclose(fp); /*關閉文件*/
}
/*文件拷貝*/
void ()
{
char outfile[10],infile[10];
FILE *sfp,*tfp; /*源和目標文件指針*/
STUDENT *p=NULL; /*移動指針*/
clrscr(); /*清屏*/
printf("Enter infile name,for example c:\\f1\\te.txt:\n");
scanf("%s",infile); /*輸入源文件名*/
if((sfp=fopen(infile,"rb"))==NULL) /*二進制讀方式打開源文件*/
{
printf("can not open input file\n");
exit(0);
}
printf("Enter outfile name,for example c:\\f1\\te.txt:\n"); /*提示輸入目標文件名*/
scanf("%s",outfile); /*輸入目標文件名*/
if((tfp=fopen(outfile,"wb"))==NULL) /*二進制寫方式打開目標文件*/
{
printf("can not open output file \n");
exit(0);
}
while(!feof(sfp)) /*讀文件直到文件尾*/
{
if(1!=fread(p,sizeof(STUDENT),1,sfp))
break; /*塊讀*/
fwrite(p,sizeof(STUDENT),1,tfp); /*塊寫*/
}
fclose(sfp); /*關閉源文件*/
fclose(tfp); /*關閉目標文件*/
printf("you have success file!!!\n"); /*顯示成功拷貝*/
}
/*排序*/
STUDENT *sort(STUDENT *h)
{
int i=0; /*保存名次*/
STUDENT *p,*q,*t,*h1; /*定義臨時指針*/
h1=h->next; /*將原表的頭指針所指的下一個結點作頭指針*/
h->next=NULL; /*第一個結點為新表的頭結點*/
while(h1!=NULL) /*當原表不為空時,進行排序*/
{
t=h1; /*取原表的頭結點*/
h1=h1->next; /*原表頭結點指針後移*/
p=h; /*設定移動指針p,從頭指針開始*/
q=h; /*設定移動指針q做為p的前驅,初值為頭指針*/
while(t->sum<p->sum&&p!=NULL) /*作總分比較*/
{
q=p; /*待排序點值小,則新表指針後移*/
p=p->next;
}
if(p==q) /*p==q,說明待排序點值大,應排在首位*/
{
t->next=p; /*待排序點的後繼為p*/
h=t; /*新頭結點為待排序點*/
}
else /*待排序點應插入在中間某個位置q和p之間,如p為空則是尾部*/
{
t->next=p; /*t的後繼是p*/
q->next=t; /*q的後繼是t*/
}
}
p=h; /*已排好序的頭指針賦給p,准備填寫名次*/
while(p!=NULL) /*當p不為空時,進行下列操作*/
{
i++; /*結點序號*/
p->order=i; /*將名次賦值*/
p=p->next; /*指針後移*/
}
printf("sort sucess!!!\n"); /*排序成功*/
return h; /*返回頭指針*/
}
/*計算總分和均值*/
void computer(STUDENT *h)
{
STUDENT *p; /*定義移動指針*/
int i=0; /*保存記錄條數初值為0*/
long s=0; /*總分初值為0*/
float average=0; /*均分初值為0*/
p=h; /*從頭指針開始*/
while(p!=NULL) /*當p不為空時處理*/
{
s+=p->sum; /*累加總分*/
i++; /*統計記錄條數*/
p=p->next; /*指針後移*/
}
average=(float)s/i;/* 求均分,均分為浮點數,總分為整數,所以做類型轉換*/
printf("\n--All students sum score is:%ld average is %5.2f\n",s,average);
}
/*索引*/
STUDENT *index(STUDENT *h)
{
STUDENT *p,*q,*t,*h1; /*定義臨時指針*/
h1=h->next; /*將原表的頭指針所指的下一個結點作頭指針*/
h->next=NULL; /*第一個結點為新表的頭結點*/
while(h1!=NULL) /*當原表不為空時,進行排序*/
{
t=h1; /*取原表的頭結點*/
h1=h1->next; /*原表頭結點指針後移*/
p=h; /*設定移動指針p,從頭指針開始*/
q=h; /*設定移動指針q做為p的前驅,初值為頭指針*/
while(strcmp(t->no,p->no)>0&&p!=NULL) /*作學號比較*/
{
q=p; /*待排序點值大,應往後插,所以新表指針後移*/
p=p->next;
}
if(p==q) /*p==q,說明待排序點值小,應排在首位*/
{
t->next=p; /*待排序點的後繼為p*/
h=t; /*新頭結點為待排序點*/
}
else /*待排序點應插入在中間某個位置q和p之間,如p為空則是尾部*/
{
t->next=p; /*t的後繼是p*/
q->next=t; /*q的後繼是t*/
}
}
printf("index sucess!!!\n"); /*索引排序成功*/
return h; /*返回頭指針*/
}
/*分類合計*/
void total(STUDENT *h)
{
STUDENT *p,*q; /*定義臨時指針變數*/
char sno[9],qno[9],*ptr; /*保存班級號的*/
float s1,ave; /*保存總分和均分*/
int i; /*保存班級人數*/
clrscr(); /*清屏*/
printf("\n\n *******************Total*****************\n");
printf("---class---------sum--------------average----\n");
p=h; /*從頭指針開始*/
while(p!=NULL) /*當p不為空時做下面的處理*/
{
memcpy(sno,p->no,8); /*從學號中取出班級號*/
sno[8]='\0'; /*做字元串結束標記*/
q=p->next; /*將指針指向待比較的記錄*/
s1=p->sum; /*當前班級的總分初值為該班級的第一條記錄總分*/
ave=p->average; /*當前班級的均分初值為該班級的第一條記錄均分*/
i=1; /*統計當前班級人數*/
while(q!=NULL) /*內循環開始*/
{
memcpy(qno,q->no,8); /*讀取班級號*/
qno[8]='\0'; /*做字元串結束標記*/
if(strcmp(qno,sno)==0) /*比較班級號*/
{
s1+=q->sum; /*累加總分*/
ave+=q->average; /*累加均分*/
i++; /*累加班級人數*/
q=q->next; /*指針指向下一條記錄*/
}
else
break; /*不是一個班級的結束本次內循環*/
}
printf("%s %10.2f %5.2f\n",sno,s1,ave/i);
if(q==NULL)
break; /*如果當前指針為空,外循環結束,程序結束*/
else
p=q; /*否則,將當前記錄作為新的班級的第一條記錄開始新的比較*/
}
printf("---------------------------------------------\n");
}
Ⅳ 求學生成績管理系統的源代碼
#include<stdio.h>
#include<stdlib.h>
#defineFILENAME"student.dat"
typedefenum{MAN,WOMAN}SEX;
typedefstructtagStudent
{
intnum; //學生的編號
charname[20]; //學生的姓名
SEX sex; //學生的性別
intage; //學生的年齡
charmajor[20]; //學生的專業
structtagStudent*next;//下一個節點的指針
}STUDENT,*PSTUDENT;
STUDENTg_head; //頭節點
//1.顯示菜單
voidShowMenu();
//2.獲取用戶選擇的菜單的編號
intGetMenuChoose();
//3.創建一個節點,它會返回一個新創建的學生信息節點的指針
PSTUDENTCreateStudent();
//4.把學生信息節點加入到鏈表中
intAddStudent(PSTUDENTpstu);
//5.返回指定編號學生節點的上一個節點的指針
PSTUDENTGetPrevAddr(intnum);
//6.顯示所有學生信息
voidShowAll();
//7.顯示信息數量
intShowStudentCount();
//8.修改學生信息,參數為要修改的學生的編號
voidModityStudent(intnum);
//9.獲取用戶的選擇
intQuestion(constchar*pstr);
//10.獲取用戶輸入的學生的編號
intGetInputNum();
//11.刪除編號為num的學生信息
voidDelStudent(intnum);
//12.刪除所有的學生信息
voidDelAll();
//13.把學生信息保存到文件當中
voidSaveToFile();
//14.從文件中讀取學生信息
voidLoadFromFile();
intmain()
{
intrunning=1;
while(running)
{
switch(GetMenuChoose())
{
case0:
running=0;
break;
case1:
// printf("你選擇了菜單1 ");
AddStudent(CreateStudent());
break;
case2:
// printf("你選擇了菜單2 ");
DelStudent(GetInputNum());
break;
case3:
printf("你選擇了菜單3 ");
break;
case4:
// printf("你選擇了菜單4 ");
ModityStudent(GetInputNum());
break;
case5:
// printf("你選擇了菜單5 ");
DelAll();
break;
case6:
// printf("你選擇了菜單6 ");
ShowAll();
break;
case7:
// printf("你選擇了菜單7 ");
ShowStudentCount();
break;
case8:
// printf("你選擇了菜單8 ");
LoadFromFile();
break;
case9:
// printf("你選擇了菜單9 ");
SaveToFile();
break;
}
system("pause");
}
return0;
}
//1.顯示菜單
voidShowMenu()
{
system("cls");
printf("-----------------------------學生管理系統-------------------------------- ");
printf(" 1.添加學生信息2.刪除某個學生信息3.顯示某個學生信息 ");
printf(" 4.修改學生信息5.刪除所有學生信息6.顯示所有學生信息 ");
printf(" 7.顯示信息數量8.讀取文件學生信息9.保存學生信息至文件 ");
printf(" 0.退出系統 ");
printf(" ------------------------------------------------------------------------- ");
}
//2.獲取用戶選擇的菜單的編號
intGetMenuChoose()
{
intnum;//保存用戶選擇的菜單編號
ShowMenu();
printf("請選擇菜單(0~9):");
while(1!=scanf("%d",&num)||num<0||num>9)
{
ShowMenu();
printf("選擇菜單錯誤,請重新選擇(0~9):");
fflush(stdin);//清空輸入緩沖區
}
returnnum;
}
//3.創建一個節點,它會返回一個新創建的學生信息節點的指針
PSTUDENTCreateStudent()
{
intsex;
PSTUDENTpstu=(PSTUDENT)malloc(sizeof(STUDENT));//在堆內存申請空間,存儲學生信息
if(!pstu)
{
printf("申請內存空間失敗! ");
returnNULL;
}
printf("請輸入學生的編號(整型):");
while(1!=scanf("%d",&pstu->num)||GetPrevAddr(pstu->num))
{
printf("學生編號輸入錯誤或已經存在,請重新輸入學生的編號(整型):");
fflush(stdin);
}
printf("請輸入學生的姓名(小於20字元):");
scanf("%20s",pstu->name);//(*pstu).name
printf("請選擇學生的性別(1.男2.女):");
while(1!=scanf("%d",&sex)||sex<1||sex>2)
{
printf("性別選擇錯誤,請重新選擇學生的性別(1.男2.女):");
fflush(stdin);
}
if(1==sex)
pstu->sex=MAN;
else
pstu->sex=WOMAN;
printf("請輸入學生的年齡(10~40):");
while(1!=scanf("%d",&pstu->age)||pstu->age<10||pstu->age>40)
{
printf("年齡輸入錯誤!請重新輸入學生的年齡(10~40):");
fflush(stdin);
}
printf("請輸入學生的專業(小於20字元):");
scanf("%20s",pstu->major);
pstu->next=NULL;
returnpstu;
}
//4.把學生信息節點加入到鏈表中
intAddStudent(PSTUDENTpstu)
{
PSTUDENTps=&g_head;
if(!pstu)
{
return0;
}
//判斷一下該學生信息是不是已經存在
if(GetPrevAddr(pstu->num))
{
printf("編號為%d的學生信息已經存在! ",pstu->num);
free(pstu);//釋放該節點內存空間
return0;
}
//while循環的作用是找到當前鏈表的最後一個節點
while(ps->next)
ps=ps->next;
//把新節點加入到最後那個節點的後面
ps->next=pstu;
pstu->next=NULL;
return1;
}
//5.返回指定編號學生節點的上一個節點的指針
PSTUDENTGetPrevAddr(intnum)
{
PSTUDENTpstu=&g_head;
while(pstu->next)
{
if(pstu->next->num==num)
returnpstu;
pstu=pstu->next;
}
returnNULL;
}
//6.顯示所有學生信息
voidShowAll()
{
PSTUDENTpstu=&g_head;
printf("-------------------------------------------------------------------- ");
printf("編號姓名性別年齡專業 ");
printf("-------------------------------------------------------------------- ");
while(pstu->next)
{
printf("%-8d",pstu->next->num);
printf("%-20s",pstu->next->name);
printf("%-6s",pstu->next->sex==MAN?"男":"女");
printf("%4d",pstu->next->age);
printf("%20s ",pstu->next->major);
pstu=pstu->next;//讓指針指向下一個節點
}
printf("-------------------------------------------------------------------- ");
}
//7.顯示信息數量
intShowStudentCount()
{
intcount=0;
PSTUDENTpstu=&g_head;
while(pstu->next)
{
++count;
pstu=pstu->next;
}
printf(" 當前共有%d位學生信息。 ",count);
returncount;
}
//8.修改學生信息,參數為要修改的學生的編號
voidModityStudent(intnum)
{
PSTUDENTpstu=GetPrevAddr(num);//獲取要修改的學生節點的上一個節點
intchoose;
if(!pstu)
{
printf("沒有編號為%d的學生信息。 ",num);
return;
}
pstu=pstu->next;//將要修改的學員節點的指針改為指向自己的
printf("當前學生的姓名為%s,",pstu->name);
if(Question("確定要修改嗎?"))
{
printf("請輸入學生的姓名(小於20字元):");
scanf("%20s",pstu->name);
}
printf("當前學生的性別為%s,",pstu->sex==MAN?"男":"女");
if(Question("確定要修改嗎?"))
{
printf("請輸入學生的性別(1.男2.女):");
while(1!=scanf("%d",&choose)||choose<1||choose>2)
{
printf("輸入錯誤,請重新輸入學生的性別(1.男2.女):");
fflush(stdin);
}
if(1==choose)
pstu->sex=MAN;
else
pstu->sex=WOMAN;
}
printf("當前學生的年齡為%d,",pstu->age);
if(Question("確定要修改嗎?"))
{
printf("請輸入學生的年齡(10~40):");
while(1!=scanf("%d",&pstu->age)||pstu->age<10||pstu->age>40)
{
printf("年齡輸入錯誤!請重新輸入學生的年齡(10~40):");
fflush(stdin);
}
}
printf("當前學生的專業為%s,",pstu->major);
if(Question("確定要修改嗎?"))
{
printf("請輸入學生的專業(小於20字元):");
scanf("%20s",pstu->major);
}
printf("修改完畢! ");
}
//9.獲取用戶的選擇
intQuestion(constchar*pstr)
{
charanswer;
printf("%s請選擇(yorn):",pstr);
while(1!=scanf("%c",&answer)||(answer!='y'&&answer!='n'))
{
printf("輸入錯誤!%s請重新選擇(yorn):",pstr);
fflush(stdin);//清空輸入緩沖區,C庫函數
}
if('y'==answer)
return1;
else
return0;
}
//10.獲取用戶輸入的學生的編號
intGetInputNum()
{
intnum;
printf("請輸入學生的編號(整型):");
while(1!=scanf("%d",&num))
{
printf("編號輸入錯誤!請重新輸入學生的編號(整型):");
fflush(stdin);
}
returnnum;
}
//11.刪除編號為num的學生信息
voidDelStudent(intnum)
{
PSTUDENTpstu,ptmp;
if(pstu=GetPrevAddr(num))
{
if(!Question("確定要刪除該學生信息嗎?"))
{
return;
}
ptmp=pstu->next;
pstu->next=ptmp->next;
free(ptmp);
printf("刪除了編號為%d的學生信息。 ",num);
}
else
{
printf("沒有找到編號為%d的學生信息。 ",num);
}
}
//12.刪除所有的學生信息
voidDelAll()
{
PSTUDENTpstu=g_head.next,ptmp;
intcount=0;
if(!Question("確定要刪除當前所有的學生信息嗎?"))
{
return;
}
while(pstu)
{
ptmp=pstu;
pstu=pstu->next;
free(ptmp);
++count;
}
printf("共刪除了%d位學生信息。 ",count);
g_head.next=NULL;
}
//13.把學生信息保存到文件當中
voidSaveToFile()
{
FILE*pf=fopen(FILENAME,"wb");
PSTUDENTpstu=&g_head;
inti=0,count=ShowStudentCount();
if(!pf)
{
printf("打開待寫入的文件失敗! ");
return;
}
if(!Question("確定要將當前學生信息保存到文件中嗎?"))
{
fclose(pf);
return;
}
fwrite(&count,1,sizeof(count),pf);//把學生信息的數量先寫入到文件頭
while(pstu->next)
{
fwrite(pstu->next,1,sizeof(STUDENT),pf);//把每位學生信息寫入文件
++i;
pstu=pstu->next;
}
fclose(pf);
if(i==count)
{
printf("成功的寫入了%d條學生信息。 ",count);
}
else
{
printf("應寫入%d條學生信息,實際寫入%d條學生信息。 ",count,i);
}
}
//14.從文件中讀取學生信息
voidLoadFromFile()
{
inti,count=0,repeat=0;
FILE*pf;
PSTUDENTpstu;
printf("提示:從文件中讀取學生信息會詢問是否清空當前學生信息(不清空表示合並所有信息)。 ");
if((pf=fopen(FILENAME,"rb"))==NULL)
{
printf("文件還沒有創建,請手工輸入學生信息並保存吧! ");
return;
}
DelAll();//刪除之前的所有學生信息,然後從文件中讀取
fread(&count,1,sizeofcount,pf);//獲取學生信息的數量
for(i=0;i<count;++i)
{
pstu=(PSTUDENT)malloc(sizeof(STUDENT));
fread(pstu,1,sizeof(STUDENT),pf);
if(!AddStudent(pstu))
{
++repeat;//保持有多少個和當前鏈表中相重復的學生信息
}
}
fclose(pf);
printf("文件讀取完畢!新增學生信息%d條。 ",count-repeat);
}
這個累死我了,我要財富值。。。為了這個不容易啊
Ⅵ 如何用VB編寫學生成績管理系統的源程序
暈.. 你這個系統 說大 不大 說小 不小..
1. 資料庫 你准備用什麽? access嗎?
2.你都沒有給分 誰給版你做這個系統 又不是一個小需求權..
3. 你是學生吧.. 自己好好的學習vb 不要 把你的考試專案 問別人..
Ⅶ C語言學生課程管理系統的源程序
c語言課程設計_學生成績管理系統
/*學生成績管理*/
/*Borland C下編譯通過,調試過程中不要輸入大數字*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int INDEX[32];
struct STUDENT
{
int id,age,chinese,math,english;
char name[21];
}students[32];
void page_title(char *menu_item)
{
clrscr();
printf(">>> 學 生 管 理 系 統 <<<\n\n- %s -\n\n",menu_item);
}
void return_confirm(void)
{
printf("\n按任意鍵返回……\n");
getch();
}
void student_new(void)
{
int n;
page_title("錄入學生基本信息");
for(n=0;n<32;n++)
if(students[n].id==0) break;
printf("學號:");
scanf("%d",&students[n].id);
printf("姓名:");
scanf("%s",&students[n].name);
printf("年齡:");
scanf("%d",&students[n].age);
return_confirm();
}
int search_id(void)
{
int n,i;
printf("請輸入學生學號:");
scanf("%d",&i);
for(n=0;n<32;n++)
{
if(students[n].id==i&&students[n].id!=0)
{
printf("學號:%d\n",students[n].id);
printf("姓名:%s\n",students[n].name);
printf("年齡:%d\n",students[n].age);
return n;
}
}
printf("\n輸入錯誤或學號不存在.\n");
return -1;
}
void student_del(void)
{
int n;
page_title("注銷學生基本信息");
if((n=search_id())!=-1) students[n].id=0;
printf("\n!該學生已注銷.\n");
return_confirm();
}
void student_edit(void)
{
int n;
page_title("編輯學生基本信息");
if((n=search_id())!=-1)
{
printf("\n請重新輸入新信息:\n學號:");
scanf("%d",&students[n].id);
printf("姓名:");
scanf("%s",&students[n].name);
printf("年齡:");
scanf("%d",&students[n].age);
}
return_confirm();
}
void score_input(void)
{
int s,n,t;
page_title("錄入成績");
printf("\n請用數字鍵選擇科目\n1-語文 2-數學 3-英語\n");
scanf("%d",&s);
for(n=0;n<32;n++)
{
if(students[n].id!=0)
{
printf("學號:%d 姓名:%s 成績:",students[n].id,students[n].name);
scanf("%d",&t);
switch(s)
{
case 1 : students[n].chinese=t;break;
case 2 : students[n].math=t;break;
case 3 : students[n].english=t;break;
}
}
}
return_confirm();
}
void score_edit(void)
{
int n;
page_title("修改成績");
if((n=search_id())!=-1)
{
printf("語文:%d 數學:%d 英語:%d\n",students[n].chinese,students[n].math,students[n].english);
printf("\n請重新輸入成績:\n語文:");
scanf("%d",&students[n].chinese);
printf("數學:");
scanf("%s",&students[n].math);
printf("英語:");
scanf("%d",&students[n].english);
}
return_confirm();
}
void sort_it(char nn)
{
int n,m,p,x,t[32];
for(n=0;n<32;n++)
{
switch(nn)
{
case '1' : t[n]=students[n].id;break;
case '2' : t[n]=students[n].chinese;break;
case '3' : t[n]=students[n].math;break;
case '4' : t[n]=students[n].english;break;
case '5' : t[n]=students[n].chinese+students[n].math+students[n].english;break;
}
}
for(n=0;n<32;n++)
{
x=0;
p=-1;
for(m=0;m<32;m++)
{
if(t[m]>x)
{
x=t[m];
p=m;
}
}
t[p]=-1;
INDEX[n]=p;
}
}
void browser(void)
{
int n,x;
char k;
sort_it('1');
while(1)
{
page_title("瀏覽");
printf("按數字鍵選擇排序方式或按 0 返回\n");
printf("學號-1\t姓名\t\t年齡\t語文-2\t數學-3\t英語-4\t總分-5\n");
for(n=0;n<32;n++)
{
if(INDEX[n]!=-1)
{
x=INDEX[n];
printf("%d\t%s\t",students[x].id,students[x].name);
printf("%d\t%d\t",students[x].age,students[x].chinese);
printf("%d\t%d\t",students[x].math,students[x].english);
printf("%d\n",students[x].chinese+students[x].math+students[x].english);
}
}
k=getch();
if(k<'6'&&k>'0') sort_it(k);
else if(k=='0') break;
else continue;
}
}
void main(void)
{
menu: page_title("操作選單");
printf("請用數字鍵選擇操作\n\n");
printf("1 錄入學生基本信息\n2 修改學生基本信息\n");
printf("3 注銷學生基本信息\n\n4 錄入成績\n5 修改成績\n\n");
printf("6 瀏覽...\n \n0 退出\n");
switch(getch())
{
case '1' : student_new();break;
case '2' : student_edit();break;
case '3' : student_del();break;
case '4' : score_input();break;
case '5' : score_edit();break;
case '6' : browser();break;
case '0' : exit(0);
}
goto menu;
}
Ⅷ 求「學生成績管理系統」源代碼
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 50 /*定義符號常量,代表學生人數最大值*/
int count=0; /*全局變數,用於記錄數組的當前位置*/
struct student /*定義結構體類型,代表學生信息*/
{
char name[9]; /*學生姓名*/
char number[10]; /*學生學號*/
float math; /*數學成績*/
float english; /*英語成績*/
};
void input(struct student *arr); /*函數聲明,輸入學生信息*/
void sort(struct student *arr); /*函數聲明,按總分排序*/
void find(struct student *arr); /*函數聲明,查找某位學生信息*/
void main()
{
char selmenu; /*定義局部變數,用於記錄用戶選擇的功能編號*/
struct student stud[N]; /*定義結構體數組,用於記錄學生信息*/
do /*利用循環顯示界面*/
{
system("cls"); /*清屏函數*/
printf("\n\n");
printf("\t\t\t\t 學生成績管理系統 \n\n");
printf("\t\t\t\t1.輸入學生成績\n");
printf("\t\t\t\t2.按總分排榜\n");
printf("\t\t\t\t3.查找學生信息\n");
printf("\t\t\t\t4.退出\n\n");
printf("\t\t\t請輸入功能選擇編號(1-4):");
fflush(stdin); /*清空輸入緩沖區*/
scanf("%c",&selmenu); /*接受用戶選擇的功能編號*/
switch(selmenu) /*輸入不同功能編號調用相應函數來完成功能*/
{
case'1':
input(stud); /*調用輸入學生信息函數,參數為學生信息數組名*/
break;
case'2':
sort(stud); /*調用按總分排榜函數,參數是數組名*/
break;
case'3':
find(stud); /*調用按學號和姓名查找學生信息函數*/
break;
}
}while(selmenu!='4'); /*判斷是否退出*/
printf("\n\n\t\t\t----------------謝謝使用----------------\n\n"); /*輸出告別詞*/
}
void input(struct student *arr) /*定義輸入學生信息函數*/
{
char ch; /*用於接收用戶指令*/
do /*利用循環,由用戶控制輸入學生成績*/
{
system("cls");
printf("\n輸入第%d個學生的信息:\n輸入學生姓名:",count+1);
scanf("%s",arr[count].name);
printf("\n輸入學生學號:");
scanf("%s",arr[count].number);
printf("\n輸入數學成績:");
scanf("%f",&arr[count].math);
printf("\n輸入英語成績:");
scanf("%f",&arr[count].english);
count++; /*記錄當前輸入的學生資料的個數*/
printf("\n是否繼續輸入(y/n)\n");
fflush(stdin); /*清空輸入緩沖區*/
ch=getchar();
}while(ch=='y'||ch=='Y'); /*判斷用戶指令,是否繼續輸入*/
}
void sort(struct student *arr) /*定義按總分排榜函數*/
{
int i,j,k; /*定義循環變數i,j,最小數位置k*/
struct student t; /*定義交換臨時變數*/
if(count==0) /*判斷數組中學生成績是否存在*/
printf("無學生成績,請先錄入學生信息\n");
else
{
for(i=0;i<count-1;i++) /*用選擇法按總分對學生成績排序*/
{
k=i;
for(j=i+1;j<count;j++) /*k為arr[i]到arr[count-1]中最小數的位置*/
if((arr[k].math+arr[k].english)<(arr[j].math+arr[j].english))
k=j;
if(k!=i) /*如果最小數不是比較數列中第一個,對調*/
{
t=arr[k];
arr[k]=arr[i];
arr[i]=t;
}
}
printf("\n總分排榜:\n\n");
printf("\t姓名\t學號\t數學\t英語\t總分\t名次\n");
for(j=0;j<count;j++) /*輸出按總分排榜後的學生成績和名次*/
printf("\t%-6s\t%-4s\t%-6.1f\t%-6.1f\t%-6.1f\t%-5d\n",
arr[j].name,arr[j].number,arr[j].math,arr[j].english,arr[j].math+arr[j].english,
j+1);
}
printf("\n回車鍵返回主程序\n");
fflush(stdin);
getchar();
}
void find(struct student *arr) /*定義查找函數*/
{
int j;
char name[9]; /*用於接收用戶輸入的學生姓名*/
char number[10]; /*用於接收用戶輸入的學生學號*/
system("cls"); /*清屏函數*/
printf("\n請輸入要查找的學生資料:\n");
printf("\n學生姓名:");
scanf("%s",name);
printf("\n學生學號:");
scanf("%s",number);
for(j=0;j<count;j++) /*使用字元串比較函數查找兵書出符合條件的學生成績*/
{
if((strcmp(name,arr[j].name)==0)&&(strcmp(number,arr[j].number)==0))
{
printf("\n\t姓名\t學號\t數學\t英語\t總分\n");
printf("\t%-6s\t%-4s\t%-6.1f\t%-6.1f\t%-6.1f\n",
arr[j].name,arr[j].number,arr[j].math,arr[j].english,arr[j].math+arr[j].english);
break;
}
}
if(j>=count) /*如果沒找到,會從循環條件退出*/
printf("\n未找到該學生紀錄,請核對後重新查找!\n");
fflush(stdin); /*清空輸入緩沖區*/
printf("\n回車鍵返回上一功能菜單\n");
getchar();
}
Ⅸ c語言程序學生成績管理系統源代碼
頭文件:::
#ifndef H_STUDENT_HH
#define H_STUDENT_HH
#include "stdio.h"
#include "string.h"
#include "malloc.h"
#define LEN sizeof(struct message_student) /*一個結構體數組元素的長度*/
#define numsubs 5 /*學科數目*/
typedef struct message_student /*結構體定義*/
{
char number[6];
char name[20];
char sex[4];
float subject[numsubs];
float score;
float average;
int index;
}student;
extern int numstus; /*學生數目*/
extern student *pointer; /*指向結構體數組*/
extern int lens;
int menu_select(); /*函數聲明*/
int openfile(student stu[]);
int findrecord(student stud[]);
int writetotext(student stud[]);
void welcome();
void display1();
void showtable();
void sort(student stu[]);
void deleterecord(student stu[],int i);
void addrecord(student stud[]);
void display(student stud[],int n1,int n2);
void amendrecord(student stud[]);
void count(student stud[]);
void sortnum(student stud[]);
void sortnum2(student stud[]);
void sortname(student stud[]);
void sortname2(student stud[]);
void sortcount(student stud[]);
void sortcount2(student stud[]);
void statistic(student stud[]);
void display1();
#endif
#include "head.h"
int menu_select()
{
char c;
printf("\n\n");
printf(" | 1. 增加學生記錄 5.統計信息 |\n");
printf(" | 2. 查詢學生記錄 6.打開文件 |\n");
printf(" | 3. 修改學生記錄 7.保存文件 |\n");
printf(" | 4. 學生紀錄排序 8.顯示記錄 |\n");
printf(" | 0.退出系統 |\n");
printf("\n\n");
printf("請選擇(0-8):");
c=getchar();
getchar();
return (c-'0');
}
#include "head.h"
int findrecord(student stud[]) /*查找信息*/
{
char str[2];
int i,num;
if(numstus==0)
{
printf("沒有可被查找的記錄\n");
return -1;
}
else
{
printf("以何種方式查找?\n1.學號\t2.姓名\t3.名次\n");
gets(str);
if(str[0]=='1') /*按學號查找*/
{
printf("請輸入學號:");
gets(str);
for(i=0;i<=numstus;i++)
if(strcmp(str,stud[i].number)==0)
{
display(stud,i,i);
break;
}
else continue;
}
else if(str[0]=='2') /*按姓名查找*/
{
printf("請輸入姓名:");
gets(str);
for(i=0;i<=numstus;i++)
if(strcmp(str,stud[i].name)==0)
{
display(stud,i,i);
break;
}
else continue;
}
else if(str[0]=='3') /*按名次查找*/
{
printf("請輸入名次:");
scanf("%d",&num);
getchar();
for(i=0;i<=numstus;i++)
if(num==stud[i].index)
{
display(stud,i,i);
break;
}
else continue;
}
if(i>numstus)
{
printf("沒有查找所要的信息。\n");
return -1;
}
return i;
}
}
#include"head.h"
int openfile(student stu[])
{
int i=0,j;
FILE *fp;
char filename[20],str[2];
if(numstus!=0)
{
printf("已經有記錄存在,是否保存?(y/n)");
gets(str);
if(str[0]=='y'||str[0]=='Y')
writetotext(stu);
}
printf("請輸入文件名:");
gets(filename);
numstus=0;
if((fp=fopen(filename,"rb+"))==NULL)
{
printf("無法打開該文件\n");
return(-1);
}
fscanf(fp,"%d",&numstus);
fgetc(fp);
while(i<numstus)
{
fscanf(fp,"%s",stu[i].number);
fscanf(fp,"%s",stu[i].name);
fscanf(fp,"%s",stu[i].sex);
for(j=0;j<numsubs;j++)
fscanf(fp,"%f",&stu[i].subject[j]);
fscanf(fp,"%f",&stu[i].score);
fscanf(fp,"%f",&stu[i].average);
fscanf(fp,"%d",&stu[i].index);
i++;
}
fclose(fp);
printf("文件讀取成功\n");
printf("是否顯示紀錄?(y/n)");
gets(str);
if(str[0]=='y'||str[0]=='Y')
display(stu,0,numstus-1);
return(0);
}
#include "head.h"
void sort(student stud[])
{
int i,j=0;
char str[5];
student *p;
p=stud;
if(numstus==0)
{
printf("沒有可供查詢的記錄!");
}
while(1)
{
for(i=0;;i++)
{
printf(" 請輸入查詢方式:");
printf("(直接輸入回車則結束查詢操作)\n");
printf("1.按照學號\t");
printf("2.按照姓名\t");
printf("3.按照名次\n");
gets(str);
if(strlen(str)==0) break;
if(str[0]=='1')
{
printf("請輸入排序次序:\n");
printf("1.升序排列\t");
printf("2.降序排列\n");
gets(str);
if(str[0]=='1')
sortnum2(p);
else
sortnum(p);
display(stud,0,numstus-1);
}
else if(str[0]=='2')
{
printf("請輸入排序次序:\n");
printf("1.升序排列\t");
printf("2.降序排列\n");
gets(str);
if(str[0]=='1')
sortname2(p);
else
sortname(p);
display(stud,0,numstus-1);
}
else if(str[0]=='3')
{
printf("請輸入排序次序:\n");
printf("1.升序排列\t");
printf("2.降序排列\n");
gets(str);
if(str[0]=='1')
sortcount2(p);
else
sortcount(p);
display(stud,0,numstus-1);
}
else printf("請輸入1~3");
printf("是否退出排序?(y/n)");
gets(str);
if(str[0]=='y'||str[0]=='Y') break;
}
return;
}
}
void sortnum(student stud[])
{
int i,j;
student temp;
student *p;
p=stud;
for(i=0;i<numstus;i++)
for(j=0;j<numstus-i-1;j++)
{
if(strcmp(stud[j+1].number,stud[j].number)>0)
{
temp=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=temp;
}
}
}
void sortnum2(student stud[])
{
int i,j;
student temp;
student *p;
p=stud;
for(i=0;i<numstus;i++)
for(j=0;j<numstus-i-1;j++)
{
if(strcmp(stud[j].number,stud[j+1].number)>0)
{
temp=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=temp;
}
}
}
void sortname(student stud[])
{
int i,j;
student temp;
student *p;
p=stud;
for(i=0;i<numstus;i++)
for(j=0;j<numstus-i-1;j++)
{
if(strcmp(stud[j+1].name,stud[j].name)>0)
{
temp=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=temp;
}
}
}
void sortname2(student stud[])
{
int i,j;
student temp;
student *p;
p=stud;
for(i=0;i<numstus;i++)
for(j=0;j<numstus-i-1;j++)
{
if(strcmp(stud[j].name,stud[j+1].name)>0)
{
temp=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=temp;
}
}
}
void sortcount(student stud[])
{
int i,j;
student temp;
student *p;
p=stud;
for(i=0;i<numstus;i++)
for(j=0;j<numstus-i-1;j++)
{
if(stud[j+1].index>stud[j].index)
{
temp=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=temp;
}
}
}
void sortcount2(student stud[])
{
int i,j;
student temp;
student *p;
p=stud;
for(i=0;i<numstus;i++)
for(j=0;j<numstus-i-1;j++)
{
if(stud[j].index>stud[j+1].index)
{
temp=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=temp;
}
}
}
#include"head.h"
void statistic(student stud[]) /*新增功能,輸出統計信息*/
{
int i,j=0,k=0;
char c1,str[2];
float average[numsubs],sum=0;
if(numstus==0)
printf("沒有可被查找的記錄\n");
else
{
while(1)
{
printf("下面將統計考試成績\n");
printf("請選擇你要統計哪科的成績 1.A\t2.B\t3.C\t4.D\t5.E\n");
c1=getchar();
printf("\t一共有個%d記錄\n",numstus); /*總共記錄數*/
switch(c1)
{
case '1':
for(i=0;i<numstus;i++) /*循環輸入判斷*/
{
sum+=stud[i].subject[0];
if(stud[k].subject[0]>stud[i].subject[0]) k=i;
if(stud[j].subject[0]<stud[i].subject[0]) j=i;
}
average[0]=sum/numstus;
printf("\t科目A的最高分:\n"); /*最高分*/
printf("\t\t學號:%s 姓名:%s 分數:%.2f\n",stud[j].number,stud[j].name,stud[j].subject[0]);
printf("\t科目A的最低分是:\n"); /*最低分*/
printf("\t\t學號:%s 姓名:%s 分數:%.2f\n",stud[k].number,stud[k].name,stud[k].subject[0]);
printf("\t科目A的平均分是 %5.2f\n",average[0]); /*平均分*/
break;
case '2':
for(i=0;i<numstus;i++) /*循環輸入判斷*/
{
sum+=stud[i].subject[1];
if(stud[k].subject[1]>stud[i].subject[1]) k=i;
if(stud[j].subject[1]<stud[i].subject[1]) j=i;
}
average[1]=sum/numstus;
printf("\t科目B的最高分:\n"); /*最高分*/
printf("\t\t學號:%s 姓名:%s 分數:%.2f\n",stud[j].number,stud[j].name,stud[j].subject[1]);
printf("\t科目B的最低分是:\n"); /*最低分*/
printf("\t\t學號:%s 姓名:%s 分數:%.2f\n",stud[k].number,stud[k].name,stud[k].subject[1]);
printf("\t科目B的平均分是 %5.2f\n",average[1]); /*平均分*/
break;
case '3':
for(i=0;i<numstus;i++) /*循環輸入判斷*/
{
sum+=stud[i].subject[2];
if(stud[k].subject[2]>stud[i].subject[2]) k=i;
if(stud[j].subject[2]<stud[i].subject[2]) j=i;
}
average[2]=sum/numstus;
printf("\t科目C的最高分:\n"); /*最高分*/
printf("\t\t學號:%s 姓名:%s 分數:%.2f\n",stud[j].number,stud[j].name,stud[j].subject[2]);
printf("\t科目C的最低分是:\n"); /*最低分*/
printf("\t\t學號:%s 姓名:%s 分數:%.2f\n",stud[k].number,stud[k].name,stud[k].subject[2]);
printf("\t科目C的平均分是 %5.2f\n",average[2]); /*平均分*/
break;
case '4':
for(i=0;i<numstus;i++) /*循環輸入判斷*/
{
sum+=stud[i].subject[3];
if(stud[k].subject[3]>stud[i].subject[3]) k=i;
if(stud[j].subject[3]<stud[i].subject[3]) j=i;
}
average[3]=sum/numstus;
printf("\t科目D的最高分:\n"); /*最高分*/
printf("\t\t學號:%s 姓名:%s 分數:%.2f\n",stud[j].number,stud[j].name,stud[j].subject[3]);
printf("\t科目D的最低分是:\n"); /*最低分*/
printf("\t\t學號:%s 姓名:%s 分數:%.2f\n",stud[k].number,stud[k].name,stud[k].subject[3]);
printf("\t科目D的平均分是 %5.2f\n",average[3]); /*平均分*/
break;
case '5':
for(i=0;i<numstus;i++) /*循環輸入判斷*/
{
sum+=stud[i].subject[4];
if(stud[k].subject[4]>stud[i].subject[4]) k=i;
if(stud[j].subject[4]<stud[i].subject[4]) j=i;
}
average[4]=sum/numstus;
printf("\t科目E的最高分:\n"); /*最高分*/
printf("\t\t學號:%s 姓名:%s 分數:%.2f\n",stud[j].number,stud[j].name,stud[j].subject[4]);
printf("\t科目E的最低分是:\n"); /*最低分*/
printf("\t\t學號:%s 姓名:%s 分數:%.2f\n",stud[k].number,stud[k].name,stud[k].subject[4]);
printf("\t科目E的平均分是 %5.2f\n",average[4]); /*平均分*/
break;
default:printf("輸入錯誤!請輸入1~5之間的數\n");
}
sum=0;
getchar();
printf("是否繼續進行統計?(y/n)");
gets(str);
if(str[0]=='y'||str[0]=='Y') ;
else break;
}
}
}
#include"head.h"
int writetotext(student stud[]) /*將所有記錄寫入文件*/
{
int i=0,j;
FILE *fp;
char filename[20];
printf("輸入文件名稱:");
gets(filename);
fp=fopen(filename,"w");
fprintf(fp,"%d\n",numstus);
while(i<numstus)
{
fprintf(fp,"%s %s %s ",stud[i].number,stud[i].name,stud[i].sex);
for(j=0;j<numsubs;j++)
fprintf(fp,"%f ",stud[i].subject[j]);
fprintf(fp,"%f %f %d ",stud[i].score,stud[i].average,stud[i].index);
i++;
}
fclose(fp);
printf("已成功存儲!\n");
display(stud,0,numstus-1);
numstus=0;
return 0;
}
#include"head.h"
void welcome()
{
printf("\t*************************************************************\n");
printf("\t\t\t\t這是一個學生成績管理系統\n\t\t\t\t 傾情奉獻 歡迎使用!\n");
printf("\t*************************************************************\n");
}
void showtable()
{
printf("---------------------------------------------------------------------------------------\n");
printf("學號\t姓名\t性別\tA\tB\tC\tD\tE\t總分\t平均分\t名次\n");
printf("---------------------------------------------------------------------------------------\n");
}
void display(student stud[],int n1,int n2)
{
int i;
showtable(); /*顯示表頭*/
for(i=n1;i<=n2;i++)
printf("%s\t%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%d\t\n",stud[i].number,stud[i].name,stud[i].sex,stud[i].subject[0],stud[i].subject[1],stud[i].subject[2],stud[i].subject[3],stud[i].subject[4],stud[i].score,stud[i].average,stud[i].index);
/*通過循環輸出數據*/
}
void display1()
{
printf("\t\t本系統由計應精英一組親情製作\n\n");
printf("\t\t製作人員列表: (按比劃)\n");
printf("\t\t王慶斌\t\t\t張威\n\t\t李智\t\t\t周在峰\n\t\t楊凱\t\t\t胡楊\n");
printf("\n\n");
getchar();
}
#include"head.h"
#include<string.h>
void amendrecord(student stud[])
{
char str[5]; /*供用戶輸入*/
int i=-1,j;
if(numstus==0) /*沒有記錄返回*/
printf("沒有可供修改的記錄!");
while(i<0)
{
i=findrecord(stud);
if(i>=0)
{
printf("要刪除這個學生的信息嗎?(y/n)");
gets(str);
if(str[0]=='y'||str[0]=='Y')
{
deleterecord(stud,i);
count(stud);
}
else
{
printf("確定要修改這個學生的信息嗎?(y/n)");
gets(str);
if(str[0]=='y'||str[0]=='Y')
{
printf("下面請重新輸入學生的信息:\n");
printf("請輸入學號:");
gets(stud[i].number);
printf("請輸入姓名:");
gets(stud[i].name);
printf("請輸入性別(男/女 1/0):");
gets(str);
if(str[0]=='0')
strcpy(stud[i].sex,"女");
else
strcpy(stud[i].sex,"男");
stud[i].score=0;
printf("請按順序輸入成績:");
for(j=0;j<numsubs;j++)
{
scanf("%f",&stud[i].subject[j]);
stud[i].score+=stud[i].subject[j];
}
getchar();
stud[i].average=stud[i].score/numsubs;
}
count(stud);
}
display(stud,0,numstus-1);
}
printf("是否繼續進行其他修改?(y/n)\n");
gets(str);
if(str[0]=='y'||str[0]=='Y')
i=-1;
else i=1;
}
}
void deleterecord(student stu[],int i) /*刪除信息*/
{
int j;
while(i>=0)
{
for(j=i;j<numstus;j++)
stu[j]=stu[j+1];
numstus--;
printf("刪除成功!\n");
}
}
void count(student stud[])
{
int i,j;
for(i=0;i<numstus;i++)
{
stud[i].index=1;
for(j=0;j<numstus;j++)
if(stud[j].score>stud[i].score)
stud[i].index++;
}
}
#include "head.h"
void addrecord(student stud[])
{
int i=0,j,num;
char str[5];
if(numstus!=0)
{
printf("已有記錄存在是否覆蓋?(y/n)\n");
gets(str);
if(str[0]=='Y'||str[0]=='y')
i=0;
else i=numstus;
}
printf("請輸入增加的學生信息條目數:");
scanf("%d",&num);
if(i==0)
numstus=num;
else numstus+=num;
if(numstus>lens)
{
lens+=50;
pointer=(student *)realloc(pointer,lens*LEN);
}
printf("請輸入學生信息:\n");
for(;i<numstus;i++)
{
getchar();
printf("請輸入學號:");
gets(pointer[i].number);
printf("請輸入姓名:");
gets(pointer[i].name);
printf("請輸入性別(男/女 1/0):");
gets(pointer[i].sex);
if(pointer[i].sex[0]=='0') strcpy(pointer[i].sex,"女");
else strcpy(pointer[i].sex,"男");
printf("請輸入各科成績:(按ABCDE的順序):");
stud[i].score=0;
for(j=0;j<numsubs;j++)
{
scanf("%f",&stud[i].subject[j]); /*計算總分*/
stud[i].score+=stud[i].subject[j];
}
stud[i].average=stud[i].score/numsubs; /*計算平均分*/
}
count(stud); /*附名次*/
display(stud,0,numstus-1);
getchar();
}#include "head.h"
int numstus;
int lens;
student *pointer;
void main()
{
int i=1;
char str[2];
lens=100;
pointer=(student *)malloc(lens*LEN); /*分配內存*/
numstus=0;
welcome(); /*歡迎界面*/
while(i>0)
{
i=menu_select(); /*控制菜單*/
switch(i)
{
case 1:addrecord(pointer);break; /*增加學生信息*/
case 2:findrecord(pointer);break; /*查詢學生信息*/
case 3:amendrecord(pointer);break; /*修改學生信息*/
case 4:sort(pointer);break; /*學生信息排序*/
case 5:statistic(pointer);break; /*統計信息*/
case 6:openfile(pointer);break; /*打開文件*/
case 7:writetotext(pointer);break; /*保存文件*/
case 8:display(pointer,0,numstus-1);break; /*顯示記錄*/
case 0:
if(numstus!=0) printf("是否保存當前記錄?(y/n)");
gets(str);
if(str[0]=='y'||str[0]=='Y')
writetotext(pointer);
i=-1;break; /*退出系統*/
default:printf("請輸入數字0~8:\n");i=1; /*輸入錯誤*/
}
}
printf("\t\t歡迎再次使用本系統。\n\n");
display1();
}
自己一改就能用,給我加分哈!