學生成績管理系統——課程設計報告

photoer_coder發表於2018-07-10

學期末課程設計的作業,利用連結串列和檔案的操作。日後再完善一些功能介紹。

/*************************************
*******程式名稱:學生成績管理系統 
*******作    者:
*******開發環境:Dev-C++5.11 
*******建立日期:2018.06.28
*******完成日期:2018.07.02 
**************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <windows.h>
#include <ctype.h>

#define YES 1
#define NO 0

typedef struct
{
	int num;
	float prob_score;
	float disc_score;
	float cpp_score;
	double aver_score;
	double sum_score;
	char sex;
}StudentDate;                                            //學生基本資訊
typedef struct student
{
	char name[20];                                       //學生姓名
	StudentDate one;                                     //學生其他資訊
	int rank;                                            //成績排名
	struct student *next;
}Student, *List;

Student *startCheck();                                   //登入系統
Student *createListHead();                               //建立連結串列的頭
void menu();                                             //主選單
void sortList(Student *pHead);                           //排序連結串列
void readListWithFile(Student *pHead, char *file_name);  //從檔案中讀取資料,並儲存在連結串列中
int  userChioce(Student *pHead);                         //使用者選擇
int  sortMenu();                                         //列印選擇選單
void passwordCheck();                                    //密碼檢測
void correctWord();                                      //修改密碼
void addStudentInfo(Student *pHead);                     //向連結串列的末尾新增資料
int  scanMenu();                                         //瀏覽選單
void findStudentInfo(Student *pHead);                    //查詢學生資訊
void delStudentInfo(Student *pHead);                     //刪除某一個學生資訊
void alterStudentInfo(Student *pHead);                   //修改學生資訊
void saveList(Student *pHead);                           //將連結串列儲存在檔案裡
void printList(Student *pHead);                          //列印連結串列
void statisStudentInfo(Student *pHead);                  //檢視學生資訊
int  statisMenu();                                       //檢視選單
void gotoxy(int x, int y);                               //改變游標位置函式
void inputData(Student *pHead, Student *node);           //輸入資訊,並檢測是否合法

int main()
{
	Student *pHead;

	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//得到視窗控制程式碼
	SMALL_RECT rc = { 0, 0, 94 - 1, 30 - 1 };   //{Y位置,X位置,寬,高}
	SetConsoleWindowInfo(hOut, true, &rc);// 重置視窗位置和大小

	system("color F1");//設定控制檯顏色
	pHead = startCheck();//讀取資訊
	while (1)
	{
		if (9 == userChioce(pHead)) //按9時退出系統
		{
			break;
		}
	}

	return 0;
}
/*================================================
== 函式名:Student *startCheck()
== 功  能:登陸系統 
== 參  數:無 
== 返回值:連結串列的頭指標 
=================================================*/
Student *startCheck()
{
	Student *pHead;
	FILE  *fp1;
	char password[20] = "77137714";
	int i;

	if ((fp1 = fopen("password.dat", "rb")) == NULL)//若密碼檔案不存在,新建密碼檔案
	{
		fp1 = fopen("password.dat", "wb");
		fwrite(password, 20, 1, fp1);
		fclose(fp1);
		printf("密碼初始化完成!(預設密碼為:77137714)");
		passwordCheck();
	}
	else
	{
		passwordCheck();
	}
	//讀條
	system("CLS");
	system("title 學生成績統計系統-載入中");//將標題欄命名為“學生成績統計系統”
	printf("\n\n\t\t\t歡迎進入本系統!正在載入.....");
	printf("\n\n\n\n\t\t\t┏━━━━━━━━━━━━━━━━━━━━┓\n");
	printf("\t\t\t┃                    ┃\n");
	printf("\t\t\t┗━━━━━━━━━━━━━━━━━━━━┛\n");
	gotoxy(26, 7);
	for (i = 0; i < 10; i++)
	{
		printf("█");
		Sleep(100);
	}

	pHead = createListHead();
	readListWithFile(pHead, "stud.dat");

	return pHead;
}
/*================================================
== 函式名:Student *createListHead()
== 功  能:建立一個單連結串列 
== 參  數:無 
== 返回值:返回連結串列的頭指標 
=================================================*/
Student *createListHead()
{
	Student *pHead;

	pHead = (List)malloc(sizeof(Student));
	pHead->next = NULL;

	return pHead;
}
/*================================================
== 函式名:void passwordCheck()
== 功  能:密碼輸入及判定 
== 參  數:無 
== 返回值:無 
=================================================*/
void passwordCheck()
{
	char password[20], inputword[20];
	int i = 0, j = 0;
	char c = 0;
	FILE *fp = fopen("password.dat", "rb");

	system("title 學生成績統計系統-密碼輸入");//將標題欄命名為“學生成績統計系統”
	fread(password, 20, 1, fp);
	for (i = 0; i < 3; i++)
	{
		j = 0;
		printf("\n\n\n\t\t你還有%d次機會輸入正確密碼: ", 3 - i);
		printf("\n\n\n\n\t\t\t┏━━━━━━━━━━━━━━━━┓\n");
		printf("\t\t\t┃                ┃\n");
		printf("\t\t\t┗━━━━━━━━━━━━━━━━┛\n");
		gotoxy(26, 8);
		while ((c = getch()) != 13 && j < 20)
		{
			if (c == '\b' && j >0)
			{
				putchar('\b');
				putchar(' ');
				putchar('\b');
				j--;
			}
			else
			{
				if (isprint(c))//isprint為檢測C是否為可列印的函式 
				{
					putchar('*');
					inputword[j] = c;
					j++;
				}
			}
		}
		inputword[j] = '\0';
		if (strcmp(inputword, password) == 0)
		{
			return;
		}
		else
		{
			system("CLS");
			printf("\t\t\t密碼輸入錯誤");
		}
	}
	if (3 == i)
	{
		system("CLS");
		printf("\n\n\n\t\t你已浪費所有機會!程式即將退出!");
		Sleep(2000);
		exit(0);
	}
	fclose(fp);
	system("CLS");

	return;
}
/*================================================
== 函式名:void readListWithFile(Student *pHead, char *file_name)
== 功  能:將檔案內的資訊讀取到連結串列中 
== 參  數:Student *pHead:連結串列頭指標 
           char *file_name:檔案指標 
== 返回值:無 
=================================================*/
void readListWithFile(Student *pHead, char *file_name)
{
	FILE *fp;
	Student *p1, *p2;
	int count, rank = 0;
	StudentDate dat;
	char stu_name[20];

	fp = fopen(file_name, "r");
	if (fp == NULL)
	{
		fp = fopen(file_name, "w");
		fclose(fp);
		return;
	}
	fseek(fp, 0L, 2);
	count = ftell(fp);
	p1 = pHead;
	fp = fopen(file_name, "r");
	while (!feof(fp))
	{
		p2 = (List)malloc(sizeof(Student));
		fscanf(fp, "%d %s %c %f %f %f\n", &p2->one.num, p2->name, &p2->one.sex, &p2->one.prob_score,
			&p2->one.disc_score, &p2->one.cpp_score);
		p2->one.sum_score = (double)(p2->one.prob_score + p2->one.disc_score + p2->one.cpp_score);
		p2->one.aver_score = p2->one.sum_score / 3;
		p2->next = NULL;
		p1->next = p2;
		p1 = p2;
		if (ftell(fp) == count)
		{
			break;
		}
	}
	//將連結串列排序,並初始化排名
	for (p1 = pHead->next; p1 != NULL; p1 = p1->next)
	{
		for (p2 = p1->next; p2 != NULL; p2 = p2->next)
		{
			if (p2->one.aver_score > p1->one.aver_score)
			{
				dat = p2->one;
				strcpy(stu_name, p2->name);
				p2->one = p1->one;
				strcpy(p2->name, p1->name);
				p1->one = dat;
				strcpy(p1->name, stu_name);
			}
		}
		p1->rank = ++rank;
	}
	fclose(fp);
}
/*================================================
== 函式名:int userChioce(Student *pHead)
== 功  能:使用者功能選擇 
== 參  數:連結串列頭指標 
== 返回值:功能對應整型數 
=================================================*/
int userChioce(Student *pHead)
{
	int bum;

	system("title 學生成績統計系統-主選單");//將標題欄命名為“學生成績統計系統”
	menu();
	printf("請按鍵選擇: ");
	bum = (int)(getch() - '0');
	switch (bum)
	{
	case 1:
		addStudentInfo(pHead);
		break;
	case 2:
		delStudentInfo(pHead);
		break;
	case 3:
		alterStudentInfo(pHead);
		break;
	case 4:
		findStudentInfo(pHead);
		break;
	case 5:
		statisStudentInfo(pHead);
		break;
	case 9:
		break;
	case 6:
		sortList(pHead);
		break;
	case 7:
		correctWord();
		break;
	case 8:
		MessageBox(NULL, TEXT("名稱:學生成績管理系統\n學校:西安郵電大學\n作者:計科1705 楊曉麟"), TEXT("關於"), MB_OK);
		break;
	default:
		break;
	}

	return bum;
}
/*================================================
== 函式名:void addStudentInfo(Student *pHead)
== 功  能:向連結串列末尾新增資料 
== 參  數:連結串列頭指標 
== 返回值:無 
=================================================*/
void addStudentInfo(Student *pHead)
{
	FILE *fp;
	Student *p1, *p2, *p3 = pHead;
	StudentDate dat;
	int judge = YES, rank = 0;
	char bum, stu_name[20];

	system("title 學生成績統計系統-新增");//將標題欄命名為“學生成績統計系統”
	fp = fopen("stud.dat", "a");
	while (pHead->next)
	{
		pHead = pHead->next;
	}
	while (judge)
	{
		p1 = (List)malloc(sizeof(Student));
		inputData(p3, p1);
		p1->next = NULL;
		pHead->next = p1;
		pHead = p1;
		fprintf(fp, "%d %s %c %.1f %.1f %.1f\n", p1->one.num, p1->name, p1->one.sex, p1->one.prob_score,
			p1->one.disc_score, p1->one.cpp_score);
		printf("是否繼續新增?(Y/N)");
		bum = getch();
		if (bum == 'n' || bum == 'N')
		{
			break;
		}

	}
	//並交換排名
	for (p1 = p3->next; p1 != NULL; p1 = p1->next)
	{
		for (p2 = p1->next; p2 != NULL; p2 = p2->next)
		{
			if (p2->one.aver_score > p1->one.aver_score)
			{
				dat = p2->one;
				strcpy(stu_name, p2->name);
				p2->one = p1->one;
				strcpy(p2->name, p1->name);
				p1->one = dat;
				strcpy(p1->name, stu_name);
			}
		}
		p1->rank = ++rank;
	}
	fclose(fp);
}
/*================================================
== 函式名:void inputData(Student *pHead, Student *node)
== 功  能:輸入資訊並檢測是否合法 
== 參  數:Student *pHead:連結串列頭指標 
           Student *node:連結串列工作指標 
== 返回值:無 
=================================================*/
void inputData(Student *pHead, Student *node)
{
	Student *p1 = node, *p2, *p3 = pHead;

	system("CLS");
	printf("\n\n\t請依次輸入學生的學號、姓名、性別、概率、離散、c++成績\n");
	printf("\t注意:姓名不能有空格,性別男用M表示,女用W表示,\n\t各科成績應該大於0小於100,輸入資料時請用空格隔開\n");
	scanf("%d %s %c %f %f %f", &p1->one.num, p1->name, &p1->one.sex, &p1->one.prob_score, &p1->one.disc_score, &p1->one.cpp_score);
	// 判斷輸入的學號是否已經存在
	p2 = p3->next;
	while (1)
	{
		if (p2 == NULL)
		{
			break;
		}
		if (p2->one.num == p1->one.num && p2 != node)
		{
			printf("該學號已存在,請重新輸入學號:");
			scanf("%d", &p1->one.num);
			p2 = p3->next;
		}
		else
		{
			p2 = p2->next;
		}
	}
	//性別是否合法
	while (1)
	{
		if (p1->one.sex != 'W'&& p1->one.sex != 'M')
		{
			p1->one.sex = getchar();
			printf("性別輸入不合法,請重新輸入: ");
			p1->one.sex = getchar();

		}
		else
		{
			break;
		}
	}
	//成績是否合法
	while (1)
	{
		if (p1->one.prob_score < 0 || p1->one.prob_score > 100)
		{
			printf("概率成績輸入不合法,請重新輸入:");
			scanf("%f", &p1->one.prob_score);
		}
		if (p1->one.disc_score < 0 || p1->one.disc_score > 100)
		{
			printf("離散成績輸入不合法,請重新輸入:");
			scanf("%f", &p1->one.disc_score);
		}
		if (p1->one.cpp_score < 0 || p1->one.cpp_score > 100)
		{
			printf("c++成績輸入不合法,請重新輸入:");
			scanf("%f", &p1->one.cpp_score);
		}
		if (p1->one.prob_score >= 0 && p1->one.prob_score <= 100 && p1->one.disc_score >= 0
			&& p1->one.disc_score <= 100 && p1->one.cpp_score >= 0 && p1->one.cpp_score <= 100)
		{
			break;
		} 
	}
	p1->one.sum_score = (double)(p1->one.prob_score + p1->one.disc_score + p1->one.cpp_score);
	p1->one.aver_score = p1->one.sum_score / 3;
}
/*================================================
== 函式名:void alterStudentInfo(Student *pHead)
== 功  能:修改學生資訊 
== 參  數:連結串列頭指標 
== 返回值:無 
=================================================*/
void alterStudentInfo(Student *pHead)
{
	int bum, count = 0, j = 0;
	int num;
	char student_name[20];
	Student *p1 = pHead->next;

	system("title 學生成績統計系統-修改");//將標題欄命名為“學生成績統計系統”
	bum = scanMenu();
	if (bum == 1)
	{
		printf("\n\t\t\t請輸入要修改學生的學號:");
		scanf("%d", &num);
		while (p1)
		{
			j++;
			if (p1->one.num == num)
			{
				inputData(pHead, p1);
				break;
			}
			p1 = p1->next;
		}
		if (p1 == NULL)
		{
			getch();
			printf("沒有該學生資訊!");
		}
	}
	else
	{
		if (bum == 2)
		{
			printf("\n\t\t\t請輸入要修改學生的姓名:");
			scanf("%s", student_name);
			while (p1)
			{
				j++;
				if (strcmp(p1->name, student_name) == 0)
				{
					inputData(pHead, p1);
					count = 1;
				}
				p1 = p1->next;
			}
			if (count == 0)
			{
				printf("\n\t\t\t沒有該學生資訊!");
				getch();
			}
		}
		else
		{
			if (3 == bum)
			{
				return;
			}
			else
			{
				alterStudentInfo(pHead);
			}
		}
	}
	saveList(pHead);
}
/*================================================
== 函式名:int scanMenu()
== 功  能:查詢選單顯示和選擇 
== 參  數:無 
== 返回值:功能對應整型數 
=================================================*/
int scanMenu()
{
	int bum;

	system("CLS");
	printf("\n\n\n");
	printf("\t\t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
	printf("\t\t┃                       以什麼方式查詢 ?                  ┃\n");
	printf("\t\t┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
	printf("\t\t┃                    1   按學生的學號                      ┃\n");
	printf("\t\t┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
	printf("\t\t┃                    2   按學生的姓名                      ┃\n");
	printf("\t\t┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
	printf("\t\t┃                    3   返回主選單                        ┃\n");
	printf("\t\t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
	printf("請按鍵選擇:");
	bum = (int)(getch() - '0');
	system("CLS");

	return bum;
}
/*================================================
== 函式名:void saveList(Student *pHead)
== 功  能:將連結串列儲存在檔案 
== 參  數:連結串列頭指標 
== 返回值:無 
=================================================*/
void saveList(Student *pHead)
{
	FILE *fp;

	fp = fopen("stud.dat", "w+");
	pHead = pHead->next;
	while (pHead)
	{
		fprintf(fp, "%d %s %c %.1f %.1f %.1f\n", pHead->one.num, pHead->name, pHead->one.sex, pHead->one.prob_score,
			pHead->one.disc_score, pHead->one.cpp_score);
		pHead = pHead->next;
	}
	fclose(fp);
}
/*================================================
== 函式名:void correctWord()
== 功  能:修改密碼 
== 參  數:無 
== 返回值:無 
=================================================*/
void correctWord()
{
	char password[20];
	FILE *fp;
	int i = 0;
	char c;

	system("title 學生成績統計系統-修改密碼");//將標題欄命名為“學生成績統計系統”
	system("CLS");
	printf("\n\n\n\n\t\t\t請輸入新的密碼:");

	while ((c = getch()) != 13 && i < 20)
	{
		if (c == '\b'&&i > 0)
		{
			putchar('\b');
			putchar(' ');
			putchar('\b');
			i--;
		}
		else
		{
			if (isprint(c))//isprint為檢測C是否為可列印的函式 
			{
				putchar('*');
				password[i] = c;
				i++;
			}
		}
	}
	password[i] = '\0';
	printf("\n\t\t\t是否將密碼修改為<%s>? Y or N:", password);
	c = getch();
	if (c == 'Y' || c == 'y')
	{
		fp = fopen("password.dat", "wb");
		fwrite(password, 20, 1, fp);
		fclose(fp);
		printf("\n\t\t\t修改成功!\n");
	}
	else
	{
		return;
	}
	getch();
}
/*================================================
== 函式名:void delStudentInfo(Student *pHead)
== 功  能:刪除資料 
== 參  數:連結串列頭指標 
== 返回值:無 
=================================================*/
void delStudentInfo(Student *pHead)
{
	Student *p1, *p2 = pHead;
	int bum;
	int num, count = 0;
	char student_name[20], c;

	system("title 學生成績統計系統-刪除");//將標題欄命名為“學生成績統計系統”
	bum = scanMenu();
	if (bum == 1)
	{
		p1 = pHead->next;
		printf("\n\t\t\t請輸入要刪除學生的學號:");
		scanf("%d", &num);
		while (p1)
		{
			if (p1->one.num == num)
			{
				count = 1;
				printf("\n\t\t\t刪除成功,刪除的學生學號為:%d", num);
				if (p1->next == NULL)
				{
					pHead->next = NULL;
					break;
				}
				else
				{
					pHead->next = p1->next;
					free(p1);
					p1 = pHead->next;
				}
			}
			else
			{
				pHead = pHead->next;
				p1 = pHead->next;
			}
		}
	}
	else
	{
		if (bum == 2)
		{
			p1 = pHead->next;
			printf("\n\t\t\t請輸入要刪除的學生姓名:");
			scanf("%s", student_name);
			//統計一共有多少個學生
			while (p1)
			{
				if (strcmp(p1->name, student_name) == 0)
				{
					count++;
				}
				p1 = p1->next;
			}
			if (count > 0)
			{
				printf("'\n\n\n\t\t\t一共有%d個人的資訊 全部刪除(Y) or 逐一刪除(N)?", count);//選擇全部刪除,還是逐一刪除
				c = getch();
				//全部刪除
				if (c == 'y' || c == 'Y')
				{
					p1 = pHead->next;
					while (p1)
					{
						if (strcmp(p1->name, student_name) == 0)
						{
							printf("\n\t\t\t刪除成功,姓名為: %s", student_name);
							if (p1->next == NULL)
							{
								pHead->next = NULL;
								break;
							}
							else
							{
								pHead->next = p1->next;
								free(p1);
								p1 = pHead->next;
							}
						}
						else
						{
							pHead = pHead->next;
							p1 = pHead->next;
						}
					}

				}
				else
				{
					//逐一刪除
					p1 = pHead->next;
					while (p1)
					{
						if (strcmp(p1->name, student_name) == 0)
						{
							printf("\n姓名:%s,學號:%d,性別:%c  是否刪除(y or n)?", p1->name, p1->one.num, p1->one.sex);
							c = getch();
							if (c == 'y' || c == 'Y')
							{
								if (p1->next == NULL)
								{
									pHead->next = NULL;
									break;
								}
								else
								{
									pHead->next = p1->next;
									free(p1);
									p1 = pHead->next;
								}
								printf("成功!");
							}
							else
							{
								pHead = pHead->next;
								p1 = pHead->next;
							}
						}
						else
						{
							pHead = pHead->next;
							p1 = pHead->next;
						}
					}
				}

			}
		}
		else
		{
			if (bum == 3)
			{
				return;
			}
			else
			{
				delStudentInfo(pHead);
			}
		}
	}
	if (count == 0)
	{
		printf("\n\t\t\t沒有該學生資訊!");
	}
	else
	{
		saveList(p2);
	}
	getch();
}
/*================================================
== 函式名:void findStudentInfo(Student *pHead)
== 功  能:資料查詢 
== 參  數:連結串列頭指標 
== 返回值:無 
=================================================*/
void findStudentInfo(Student *pHead)
{
	int bum, count = 0;
	int num;
	char student_name[20];

	pHead = pHead->next;
	system("title 學生成績統計系統-查詢");//4-將標題欄命名為“學生成績統計系統”
	bum = scanMenu();
	if (bum == 1)
	{
		printf("請輸入學生的學號:");
		scanf("%d", &num);
		while (pHead)
		{
			if (pHead->one.num == num)
			{
				if (count == 0)
				{
					printf("\n\t\t\t已經查到!\n");
					printf("\n\  ┏━━━━━━┳━━━━━━━━┳━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━┓");
					printf("\n  ┃ 學號 ┃ 姓 名  ┃性別┃概  率┃離  散┃c  ++ ┃平均分┃總  分┃名次┃\n");
					count = 1;
				}
				printf("  ┣━━━━━━╋━━━━━━━━╋━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━┫\n");
				printf("  ┃ %-5d┃ %-6s ┃  %c ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃ %-3d┃\n", pHead->one.num,
					pHead->name, pHead->one.sex, pHead->one.prob_score, pHead->one.disc_score,
					pHead->one.cpp_score, pHead->one.aver_score, pHead->one.sum_score, pHead->rank);
				break;
			}
			pHead = pHead->next;
		}
		if (pHead == NULL)
		{
			printf("\n\t\t\t沒有該學生記錄!\n");
		}
		else
		{
			printf("  ┗━━━━━━┻━━━━━━━━┻━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━┛\n");
		}
	}
	else
	{
		if (bum == 2)
		{
			printf("請輸入學生姓名:");
			scanf("%s", student_name);
			while (pHead)
			{
				if (strcmp(student_name, pHead->name) == 0)
				{
					if (count == 0)
					{
						printf("\n\t\t\t已經查到!\n");
						printf("\n\  ┏━━━━━━┳━━━━━━━━┳━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━┓");
						printf("\n  ┃ 學號 ┃ 姓 名  ┃性別┃概  率┃離  散┃c  ++ ┃平均分┃總  分┃名次┃\n");
						count = 1;
					}
					printf("  ┣━━━━━━╋━━━━━━━━╋━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━┫\n");
					printf("  ┃ %-5d┃ %-6s ┃  %c ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃ %-3d┃\n", pHead->one.num,
						pHead->name, pHead->one.sex, pHead->one.prob_score, pHead->one.disc_score,
						pHead->one.cpp_score, pHead->one.aver_score, pHead->one.sum_score, pHead->rank);
				}
				pHead = pHead->next;
			}
			if (count == 0)
			{
				printf("\n\t\t\t沒有該學生記錄!");
			}
			else
			{
				printf("  ┗━━━━━━┻━━━━━━━━┻━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━┛\n");
			}
		}
		else
		{
			if (bum == 3)
			{
				return;
			}
			else
			{
				findStudentInfo(pHead);
			}
		}

	}
	getch();
}
/*================================================
== 函式名:void menu()
== 功  能:選單顯示 
== 參  數:無 
== 返回值:無 
=================================================*/
void menu()
{
	system("CLS");//清屏
	printf("\n\n\n\t\t\t\t┌──────────────────────────────────┐\n");
	printf("\t\t\t\t│         學生成績統計系統         │\n");
	printf("\t\t\t\t├──────────────────────────────────┤\n");
	printf("\t\t\t\t│         1   增加學生記錄         │\n");
	printf("\t\t\t\t├──────────────────────────────────┤\n");
	printf("\t\t\t\t│         2   刪除學生記錄         │\n");
	printf("\t\t\t\t├──────────────────────────────────┤\n");
	printf("\t\t\t\t│         3   修改學生記錄         │\n");
	printf("\t\t\t\t├──────────────────────────────────┤\n");
	printf("\t\t\t\t│         4   查詢學生資訊         │\n");
	printf("\t\t\t\t├──────────────────────────────────┤\n");
	printf("\t\t\t\t│         5   統計學生成績         │\n");
	printf("\t\t\t\t├──────────────────────────────────┤\n");
	printf("\t\t\t\t│         6   檢視學生成績         │\n");
	printf("\t\t\t\t├──────────────────────────────────┤\n");
	printf("\t\t\t\t│         7   修改登陸密碼         │\n");
	printf("\t\t\t\t├──────────────────────────────────┤\n");
	printf("\t\t\t\t│         8      關於              │\n");
	printf("\t\t\t\t├──────────────────────────────────┤\n");
	printf("\t\t\t\t│         9    退出系統            │\n");
	printf("\t\t\t\t└──────────────────────────────────┘\n");
}
/*================================================
== 函式名:void sortList(Student *pHead)
== 功  能:資料排序 
== 參  數:連結串列頭指標 
== 返回值:無 
=================================================*/
void sortList(Student *pHead)
{
	Student *p1, *p2;
	StudentDate dat;
	char stu_name[20];
	int bum, count = 0, rank;

	system("title 學生成績統計系統-檢視");//6-將標題欄命名為“學生成績統計系統”
	bum = sortMenu();
	if (bum == 1)//按平均分高低排序
	{
		for (p1 = pHead->next; p1 != NULL; p1 = p1->next)
		{
			for (p2 = p1->next; p2 != NULL; p2 = p2->next)
			{
				if (p2->one.aver_score > p1->one.aver_score)
				{
					dat = p2->one;
					rank = p2->rank;
					strcpy(stu_name, p2->name);
					p2->one = p1->one;
					p2->rank = p1->rank;
					strcpy(p2->name, p1->name);
					p1->one = dat;
					p1->rank = rank;
					strcpy(p1->name, stu_name);
				}
			}
		}
	}
	else
	{
		if (bum == 2)//按學號高低排序
		{
			for (p1 = pHead->next; p1 != NULL; p1 = p1->next)
			{
				for (p2 = p1->next; p2 != NULL; p2 = p2->next)
				{
					if (p2->one.num < p1->one.num)
					{
						dat = p2->one;
						rank = p2->rank;
						strcpy(stu_name, p2->name);
						p2->one = p1->one;
						p2->rank = p1->rank;
						strcpy(p2->name, p1->name);
						p1->one = dat;
						p1->rank = rank;
						strcpy(p1->name, stu_name);
					}
				}
			}
		}
		else
		{
			if (bum == 3)
			{
				return;
			}
			else
			{
				sortList(pHead);
			}
		}
	}
	printList(pHead);
}
/*================================================
== 函式名:int sortMenu()
== 功  能:排序方式選單顯示和選擇 
== 參  數:無 
== 返回值:排序方式對應整型數 
=================================================*/
int sortMenu()
{
	int bum;

	system("CLS");
	printf("\n\n\n");
	printf("\t\t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
	printf("\t\t┃                       以什麼方式查詢 ?                  ┃\n");
	printf("\t\t┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
	printf("\t\t┃                    1   按成績的高低                      ┃\n");
	printf("\t\t┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
	printf("\t\t┃                    2   按學號的升次                      ┃\n");
	printf("\t\t┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
	printf("\t\t┃                    3   返回主選單                        ┃\n");
	printf("\t\t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
	printf("請按鍵選擇:");
	bum = (int)(getch() - '0');
	system("CLS");

	return bum;
}
/*================================================
== 函式名:void printList(Student *pHead)
== 功  能:連結串列資料列印 
== 參  數:連結串列頭指標 
== 返回值:無 
=================================================*/
void printList(Student *pHead)
{
	int count = 0;
	pHead = pHead->next;
	system("CLS");
	printf("\n\  ┏━━━━━━┳━━━━━━━━┳━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━┓");
	printf("\n  ┃ 學號 ┃ 姓 名  ┃性別┃概  率┃離  散┃c  ++ ┃平均分┃總  分┃名次┃\n");
	while (pHead)
	{
		printf("  ┣━━━━━━╋━━━━━━━━╋━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━┫\n");
		printf("  ┃ %-5d┃ %-6s ┃  %c ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃ %-3d┃\n", pHead->one.num,
			pHead->name, pHead->one.sex, pHead->one.prob_score, pHead->one.disc_score,
			pHead->one.cpp_score, pHead->one.aver_score, pHead->one.sum_score, pHead->rank);
		pHead = pHead->next;
		count++;
	}
	printf("  ┗━━━━━━┻━━━━━━━━┻━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━┛\n");
	printf("\t\t\t該班一共%d個人!", count);
	getch();
}
/*================================================
== 函式名:void statisStudentInfo(Student *pHead)
== 功  能:檢視學生資訊 
== 參  數:連結串列頭指標 
== 返回值:無 
=================================================*/
void statisStudentInfo(Student *pHead)
{
	double aver_score[5] = { 0,0,0,0,0 };
	Student *p1 = pHead->next;
	int count = 0;
	int bad = 0, bum;

	system("title 學生成績統計系統-統計");//將標題欄命名為“學生成績統計系統”
	bum = statisMenu();
	if (bum == 1)
	{
		while (p1)
		{
			aver_score[0] = aver_score[0] + p1->one.prob_score;
			aver_score[1] = aver_score[1] + p1->one.disc_score;
			aver_score[4] = aver_score[4] + p1->one.cpp_score;
			p1 = p1->next;
			count++;
		}
		aver_score[0] = aver_score[0] / count;
		aver_score[1] = aver_score[1] / count;
		aver_score[4] = aver_score[4] / count;
		p1 = pHead->next;
		printf("\n\n  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
		printf("  ┃\t概率的平均分為:%.1f,沒有達到達到平均分的學生有        ┃\n", aver_score[0]);
		printf("  ┣━━━━━━┳━━━━━━━━┳━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━┫\n");
		printf("  ┃ 學號 ┃ 姓 名  ┃性別┃概  率┃離  散┃  c++ ┃平均分┃總  分┃名次┃\n");
		printf("  ┣━━━━━━╋━━━━━━━━╋━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━┫\n");
		while (p1)
		{
			if (p1->one.prob_score < aver_score[0])
			{
				if (bad == 0)
				{
					printf("  ┃ %-5d┃ %-6s ┃  %c ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃ %-3d┃\n", p1->one.num,
						p1->name, p1->one.sex, p1->one.prob_score, p1->one.disc_score,
						p1->one.cpp_score, p1->one.aver_score, p1->one.sum_score, p1->rank);
				}
				else
				{
					printf("  ┣━━━━━━╋━━━━━━━━╋━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━┫\n");
					printf("  ┃ %-5d┃ %-6s ┃  %c ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃ %-3d┃\n", p1->one.num,
						p1->name, p1->one.sex, p1->one.prob_score, p1->one.disc_score,
						p1->one.cpp_score, p1->one.aver_score, p1->one.sum_score, p1->rank);
				}
				bad = 1;
			}
			p1 = p1->next;
		}
		bad = 0;
		p1 = pHead->next;
		printf("  ┣━━━━━━┻━━━━━━━━┻━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━┫\n");
		printf("  ┃\t離散的平均分為:%.1f,沒有達到達到平均分的學生有        ┃\n", aver_score[1]);
		printf("  ┣━━━━━━┳━━━━━━━━┳━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━┫\n");
		printf("  ┃ 學號 ┃ 姓 名  ┃性別┃概  率┃離  散┃  c++ ┃平均分┃總  分┃名次┃\n");
		printf("  ┣━━━━━━╋━━━━━━━━╋━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━┫\n");
		while (p1)
		{
			if (p1->one.disc_score < aver_score[1])
			{
				if (bad == 0)
				{
					printf("  ┃ %-5d┃ %-6s ┃  %c ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃ %-3d┃\n", p1->one.num,
						p1->name, p1->one.sex, p1->one.prob_score, p1->one.disc_score,
						p1->one.cpp_score, p1->one.aver_score, p1->one.sum_score, p1->rank);
				}
				else
				{
					printf("  ┣━━━━━━╋━━━━━━━━╋━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━┫\n");
					printf("  ┃ %-5d┃ %-6s ┃  %c ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃ %-3d┃\n", p1->one.num,
						p1->name, p1->one.sex, p1->one.prob_score, p1->one.disc_score,
						p1->one.cpp_score, p1->one.aver_score, p1->one.sum_score, p1->rank);
				}
				bad = 1;
			}
			p1 = p1->next;
		}

		bad = 0;
		p1 = pHead->next;
		printf("  ┣━━━━━━┻━━━━━━━━┻━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━┫\n");
		printf("  ┃\tc++的平均分為:%.1f,沒有達到達到平均分的學生有         ┃\n", aver_score[4]);
		printf("  ┣━━━━━━┳━━━━━━━━┳━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━┫\n");
		printf("  ┃ 學號 ┃ 姓 名  ┃性別┃概  率┃離  散┃  c++ ┃平均分┃總  分┃名次┃\n");
		printf("  ┣━━━━━━╋━━━━━━━━╋━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━┫\n");
		while (p1)
		{
			if (p1->one.cpp_score < aver_score[4])
			{
				if (bad == 0)
				{
					printf("  ┃ %-5d┃ %-6s ┃  %c ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃ %-3d┃\n", p1->one.num,
						p1->name, p1->one.sex, p1->one.prob_score, p1->one.disc_score,
						p1->one.cpp_score, p1->one.aver_score, p1->one.sum_score, p1->rank);
				}
				else
				{
					printf("  ┣━━━━━━╋━━━━━━━━╋━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━┫\n");
					printf("  ┃ %-5d┃ %-6s ┃  %c ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃ %-3d┃\n", p1->one.num,
						p1->name, p1->one.sex, p1->one.prob_score, p1->one.disc_score,
						p1->one.cpp_score, p1->one.aver_score, p1->one.sum_score, p1->rank);
				}
				bad = 1;
			}
			p1 = p1->next;
		}
		printf("  ┗━━━━━━┻━━━━━━━━┻━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━┛\n");
		printf("\t\t\t\t\t\t該班一共%d個人!", count);
	}
	else
	{
		if (bum == 2)
		{
			bad = 0;
			printf("\n\n  ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
			printf("  ┃\t概率成績大於90分或小於60的學生有                       ┃\n");
			printf("  ┣━━━━━━┳━━━━━━━━┳━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━┫\n");
			printf("  ┃ 學號 ┃ 姓 名  ┃性別┃概  率┃離  散┃  c++ ┃平均分┃總  分┃名次┃\n");
			printf("  ┣━━━━━━╋━━━━━━━━╋━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━┫\n");
			p1 = pHead->next;
			while (p1)
			{
				if (p1->one.prob_score < 60 || p1->one.prob_score >= 90)
				{
					if (bad == 0)
					{
						printf("  ┃ %-5d┃ %-6s ┃  %c ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃ %-3d┃\n", p1->one.num,
							p1->name, p1->one.sex, p1->one.prob_score, p1->one.disc_score,
							p1->one.cpp_score, p1->one.aver_score, p1->one.sum_score, p1->rank);
					}
					else
					{
						printf("  ┣━━━━━━╋━━━━━━━━╋━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━┫\n");
						printf("  ┃ %-5d┃ %-6s ┃  %c ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃ %-3d┃\n", p1->one.num,
							p1->name, p1->one.sex, p1->one.prob_score, p1->one.disc_score,
							p1->one.cpp_score, p1->one.aver_score, p1->one.sum_score, p1->rank);
					}
					bad = 1;
				}
				p1 = p1->next;
			}
			bad = 0;
			printf("  ┣━━━━━━┻━━━━━━━━┻━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━┫\n");
			printf("  ┃\t離散成績大於90分或小於60的學生有                       ┃\n");
			printf("  ┣━━━━━━┳━━━━━━━━┳━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━┫\n");
			printf("  ┃ 學號 ┃ 姓 名  ┃性別┃概  率┃離  散┃  c++ ┃平均分┃總  分┃名次┃\n");
			printf("  ┣━━━━━━╋━━━━━━━━╋━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━┫\n");
			p1 = pHead->next;
			while (p1)
			{
				if (p1->one.disc_score < 60 || p1->one.disc_score >= 90)
				{
					if (bad == 0)
					{
						printf("  ┃ %-5d┃ %-6s ┃  %c ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃ %-3d┃\n", p1->one.num,
							p1->name, p1->one.sex, p1->one.prob_score, p1->one.disc_score,
							p1->one.cpp_score, p1->one.aver_score, p1->one.sum_score, p1->rank);
					}
					else
					{
						printf("  ┣━━━━━━╋━━━━━━━━╋━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━┫\n");
						printf("  ┃ %-5d┃ %-6s ┃  %c ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃ %-3d┃\n", p1->one.num,
							p1->name, p1->one.sex, p1->one.prob_score, p1->one.disc_score,
							p1->one.cpp_score, p1->one.aver_score, p1->one.sum_score, p1->rank);
					}
					bad = 1;
				}
				p1 = p1->next;
			}
			bad = 0;
			printf("  ┣━━━━━━┻━━━━━━━━┻━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━┫\n");
			printf("  ┃\tc++成績大於90分或小於60的學生有                        ┃\n");
			printf("  ┣━━━━━━┳━━━━━━━━┳━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━┳━━━━┫\n");
			printf("  ┃ 學號 ┃ 姓 名  ┃性別┃概  率┃離  散┃  c++ ┃平均分┃總  分┃名次┃\n");
			printf("  ┣━━━━━━╋━━━━━━━━╋━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━┫\n");
			p1 = pHead->next;
			while (p1)
			{
				if (p1->one.cpp_score < 60 || p1->one.cpp_score >= 90)
				{
					if (bad == 0)
					{
						printf("  ┃ %-5d┃ %-6s ┃  %c ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃ %-3d┃\n", p1->one.num,
							p1->name, p1->one.sex, p1->one.prob_score, p1->one.disc_score,
							p1->one.cpp_score, p1->one.aver_score, p1->one.sum_score, p1->rank);
					}
					else
					{
						printf("  ┣━━━━━━╋━━━━━━━━╋━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━━━╋━━━━┫\n");
						printf("  ┃ %-5d┃ %-6s ┃  %c ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃%5.1f ┃ %-3d┃\n", p1->one.num,
							p1->name, p1->one.sex, p1->one.prob_score, p1->one.disc_score,
							p1->one.cpp_score, p1->one.aver_score, p1->one.sum_score, p1->rank);
					}
					bad = 1;
				}
				p1 = p1->next;
			}
			printf("  ┗━━━━━━┻━━━━━━━━┻━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━━━┻━━━━┛\n");
		}
		else
		{
			if (bum == 3)
			{
				return;
			}
			else
			{
				statisStudentInfo(pHead);
			}
		}
	}
	getch();
}
/*================================================
== 函式名:int statisMenu()
== 功  能:檢視方式選單和選擇 
== 參  數:無 
== 返回值:方式對應整型數 
=================================================*/
int statisMenu()
{
	int bum;

	system("CLS");
	printf("\n\n\n");
	printf("\t\t┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n");
	printf("\t\t┃                   以什麼方式統計學生資訊?               ┃\n");
	printf("\t\t┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
	printf("\t\t┃                      1   按平均分                        ┃\n");
	printf("\t\t┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
	printf("\t\t┃                      2   按是否合格                      ┃\n");
	printf("\t\t┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫\n");
	printf("\t\t┃                      3   返回主選單                      ┃\n");
	printf("\t\t┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n");
	printf("請按鍵選擇:");
	bum = (int)(getch() - '0');
	system("CLS");

	return bum;
}
/*================================================
== 函式名:void gotoxy(int x, int y)
== 功  能:游標移動到指定位置 
== 參  數:int x:列座標 
           int y:行座標 
== 返回值:無 
=================================================*/
void gotoxy(int x, int y)//x為列座標,y為行座標
{
	COORD pos = { x, y };
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut, pos);
}

 

相關文章