#define NULL 0
#define LEN sizeof(struct student)
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
//定義結構體
struct student
{
char StuID[20];
char CourseCode[20];
char CourseName[20];
float Credit;
float UsualResult;
float ShiyanResult;
float JuanmianResult;
float ZongheResult;
float ShideCredit;
struct student *next;
};
struct student *head;
void Menu();
struct student *Create();
void Save();
void Sort();
void Serch();
//void Delete();
struct student *Delete();
void Display();
/************主函數*******************/
int main()
{
system("color 0D");
int choice;
char yes_no;
do
{
//system("cls");
Menu();
printf ("請在0~6中輸入你的選擇:\n");
scanf("%d",&choice);
printf ("\n");
switch(choice)
{
case 1: Create(); break;
case 2: Sort(); break;
case 3: Serch(); break;
case 4: Delete(); break;
case 5: Save(); break;
case 6: Display(); break;
case 0: printf("謝謝你的使用!"); exit(0); break;
default: printf("輸入錯誤。。請輸入0~6之間的數字");
}
printf("\n繼續輸入?Y/N : ");
do
{
scanf("%c",&yes_no);
}while( (yes_no != 'Y') && (yes_no != 'y') && (yes_no != 'N') && (yes_no!='n'));
}while((yes_no == 'Y')||(yes_no == 'y'));
return 0;
}
/************菜單函數*****************/
void Menu()
{
printf("\n\n ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
printf(" |"); printf("\t\t\t歡迎進入學生管理系統"); printf("\t\t\t|");
printf("\n ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
printf(" |"); printf("\t\t\t 1.錄入學生資訊"); printf("\t\t\t|\n");
printf(" |"); printf("\t\t\t 2.排序學生資訊"); printf("\t\t\t|\n");
printf(" |"); printf("\t\t\t 3.尋找學生資訊"); printf("\t\t\t|\n");
printf(" |"); printf("\t\t\t 4.刪除學生資訊"); printf("\t\t\t|\n");
printf(" |"); printf("\t\t\t 5.儲存學生資訊"); printf("\t\t\t|\n");
printf(" |"); printf("\t\t\t 6.顯示學生資訊"); printf("\t\t\t|\n");
printf(" |"); printf("\t\t\t 0.退出管理系統"); printf("\t\t\t|\n");
printf(" ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
}
/************建立鏈表並錄入資料 **************/
struct student *Create()
{
struct student *p,*q;
int i,m;
printf("請輸入您要建立的結點個數:");
scanf("%d",&m);
for(i = 1 ; i <= m ; i ++)
{
p=(struct student *)malloc(LEN);
printf ("請輸入第%d個學生的資訊:\n",i);
printf ("\n請輸入學號:");
scanf("%s",p->StuID);
printf ("\n請輸入課程編號:");
scanf("%s",p->CourseCode);
printf ("\n請輸入課程名稱:");
scanf ("%s",p->CourseName);
printf ("\n請輸入學分:");
scanf("%f",&p->Credit);
printf ("\n請輸入平時成績:");
scanf("%f",&p->UsualResult);
printf ("\n請輸入實驗成績:");
scanf("%f",&p->ShiyanResult);
printf ("\n請輸入卷面成績:");
scanf("%f",&p->JuanmianResult);
if((p->ShiyanResult) == -1)
{
p->ZongheResult = p->UsualResult*30/100 + p->JuanmianResult*70/100;
printf("綜合成績 = %f\n",p->ZongheResult);
}
else
{
p->ZongheResult = p->UsualResult*15/100 + p->JuanmianResult*70/100 + p->ShiyanResult*0.15/100;
printf("綜合成績 = %f\n",p->ZongheResult );
}
int ZHResult = (p->ZongheResult)/10;
switch(ZHResult)
{
case 10:
case 9: p->ShideCredit = p->Credit*100/100;
printf("實得學分 = %f\n",p->ShideCredit); break;
case 8: p->ShideCredit = p->Credit*80/100;
printf("實得學分 = %f\n",p->ShideCredit); break;
case 7: p->ShideCredit=p->Credit*75/100;
printf("實得學分 = %f\n",p->ShideCredit); break;
case 6: p->ShideCredit = p->Credit*60/100;
printf("實得學分 = %f\n",p->ShideCredit); break;
default: p->ShideCredit = 0; printf("實得學分 = %f\n",p->ShideCredit);
}
p->next = NULL;
if(head == NULL)
{
head = p;
q = p;
}
q->next = p;
q = p;
}
return head;
}
/***************排序函數*******************/
void Sort()
{
int choice;
struct student *p1,*p2;
char stuID[20];
char couCode[20];
char couName[20];
float credit;
float usualRes;
float syRes;
float jmRes;
float zhRes;
float sdCre;
printf("\n");
printf("1綜合成績降序排列:\n");
printf("2實得學分升序排列:\n");
printf("請輸入的選擇:");
scanf("%d",&choice);
printf("\n");
switch(choice)
{
case 1: for(p1 = head ; p1 != NULL ; p1 = p1->next)
{
for(p2 = p1->next ; p2 != NULL ; p2 = p2->next) //選擇法排序,每次選出最小的排到表頭
{
if(p1->ZongheResult<p2->ZongheResult)
{
zhRes = p2->ZongheResult;
p2->ZongheResult = p1->ZongheResult;
p1->ZongheResult = zhRes;
credit = p2->Credit;
p2->Credit = p1->Credit;
p1->Credit = p2->Credit;
usualRes = p2->UsualResult;
p2->UsualResult = p1->UsualResult;
p1->UsualResult = usualRes;
syRes = p2->ShiyanResult;
p2->ShiyanResult = p1->ShiyanResult;
p1->ShiyanResult = syRes;
jmRes = p2->JuanmianResult;
p2->JuanmianResult = p1->JuanmianResult;
p1->JuanmianResult = jmRes;
sdCre = p2->ShideCredit;
p2->ShideCredit = p1->ShideCredit;
p1->ShideCredit = sdCre;
strcpy(stuID,p2->StuID);
strcpy(p2->StuID,p1->StuID);
strcpy(p1->StuID,stuID);
strcpy(couCode,p2->CourseCode);
strcpy(p2->CourseCode,p1->CourseCode);
strcpy(p1->CourseCode,couCode);
strcpy(couName,p2->CourseName);
strcpy(p2->CourseName,p1->CourseName);
strcpy(p1->CourseName,couName);
}
}
}
Display();
break;
case 2:for(p1 = head ; p1 != NULL ; p1 = p1->next)
{
for(p2 = p1->next ; p2 != NULL ; p2 = p2->next) //選擇法排序,每次選出最小的排到表頭
{
if(p1->ShideCredit<p2->ShideCredit)
{
sdCre = p2->ShideCredit;
p2->ShideCredit = p1->ShideCredit;
p1->ShideCredit = sdCre;
zhRes = p2->ZongheResult;
p2->ZongheResult = p1->ZongheResult;
p1->ZongheResult = zhRes;
credit = p2->Credit;
p2->Credit = p1->Credit;
p1->Credit = p2->Credit;
usualRes = p2->UsualResult;
p2->UsualResult = p1->UsualResult;
p1->UsualResult = usualRes;
syRes= p2->ShiyanResult;
p2->ShiyanResult = p1->ShiyanResult;
p1->ShiyanResult = syRes;
jmRes = p2->JuanmianResult;
p2->JuanmianResult = p1->JuanmianResult;
p1->JuanmianResult = jmRes;
strcpy(stuID,p2->StuID);
strcpy(p2->StuID,p1->StuID);
strcpy(p1->StuID,stuID);
strcpy(couCode,p2->CourseCode);
strcpy(p2->CourseCode,p1->CourseCode);
strcpy(p1->CourseCode,couCode);
strcpy(couName,p2->CourseName);
strcpy(p2->CourseName,p1->CourseName);
strcpy(p1->CourseName,couName);
}
}
}
Display();
break;
}
return;
}
/********成績查詢函數**************/
void Serch()
{
struct student *p;
char stuid[20];
if(head == NULL)
{
printf("\nlist null!\n");
return;
}
printf("請輸入你要查詢的學號:\n");
scanf("%s",&stuid);
for(p = head ; p != NULL ; p = p->next)
{
if (strcmp(p->StuID,stuid) == 0)
{
printf("學號: ");
printf("s%\n",p->StuID);
printf("課程編號\t課程名稱\t綜合成績\t實得成績\n");
printf("%4s\t\t%4s\t\t%4.1f\t\t%4.1f",p->CourseCode,p->CourseName,p->ZongheResult,p->ShideCredit);
}
else if(p->next == NULL)
printf("\n no found");
}
}
/*********刪除函數***************/
struct student *Delete()
{
struct student *p1,*p2;
char stuid[20];
printf("請輸入你要刪除的學號:");
scanf("%s",stuid);
if(head == NULL)
printf("\n鏈表為空白");
else
{
p1 = p2 = head;
while((strcmp(p1->StuID,stuid) !=0 ) && (p1->next) != NULL)
{
p2 = p1;
p1 = p1->next;
}
if((strcmp(p1->StuID,stuid) == 0))
{
if(p1 == head)
head = p1->next;
else
p2->next = p1->next;
printf("\n該資訊已被刪除");
free(p1);
}
else
printf("not been found!!\n");
}
return head;
}
/*************儲存函數****************/
void Save()
{
FILE *fp;
struct student *p;
if ((fp = fopen("B.txt","w")) == NULL)
{
printf("can't open!\n");
exit(0);
}
for(p = head ; p != NULL ; p = p->next)
fprintf(fp,"%c %c %c %f %f %f %f %f\n",p->StuID,p->CourseCode,p->CourseName,
p->StuID,p->UsualResult,p->ShiyanResult,p->JuanmianResult,
p->ZongheResult,p->ShideCredit);
fclose(fp);
printf("資訊已儲存");
}
/*********顯示記錄***********/
void Display()
{
struct student *p;
if(head == NULL)
{
printf("\n list null");
return;
}
printf("學號 課程編號 課程名稱 學分 平時成績 實驗成績 卷面成績 綜合成績 實得學分\n");
for(p = head ; p != NULL ; p = p->next)
printf("%2s %4s %10s %8.1f %6.1f %6.1f %10.1f %10.1f %6.1f\n",p->StuID,p->CourseCode,p->CourseName,
p->Credit,p->UsualResult,p->ShiyanResult,p->JuanmianResult,p->ZongheResult,p->ShideCredit);
}