⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 functions.c

📁 一款用纯C语言编写的大学生成绩管理系统
💻 C
字号:
#include "MSI.h"

//一级主菜单界面
void MainMenu()
{
	printf("            Welcome to the Student Score MIS\n");
    printf("************************ Main Menu* **************************\n");
    printf("*      1.  Add Records         2.  Require Records           *\n");
	printf("*      3.  Delete Records      4.  Modify Records            *\n");
	printf("*      5.  Sort Records        6.  Save Records              *\n");
	printf("*      7.  Load Records        8.  Exit                      *\n");
    printf("**************************************************************\n");
}
//二级菜单界面:Require Records
void RequireRecords()
{
	printf("*********************Require Records *************************\n");
    printf("*          1.  Require Certain Record by Num                 *\n");
    printf("*          2.  Require All the Records                       *\n");
    printf("*          3.  Back to MainMenu                              *\n");
    printf("**************************************************************\n");
}
//二级菜单界面:Sort Records
void SortRecords()
{
	printf("*********************Sort Records ****************************\n");
    printf("*          1.  Sort Records by Num                           *\n");
    printf("*          2.  Sort Records by name                          *\n");
    printf("*          3.  Sort Records by Average Score                 *\n");
    printf("*          4.  Back to MainMenu                              *\n");
    printf("**************************************************************\n");
}
//一级主菜单选择
void SelectMain(Record *head, char s)
{
	Record *t;
	char choose, *num = (char *)calloc(11, sizeof(char));
	switch(s) {
	case '1':
		t = GetRecord(head);
		if (t != NULL)	AddRecord(head, t);
		break;
	case '2':
		do {
			RequireRecords();
			printf("Enter your choose:");
			fflush(stdin);
			scanf ("%c", &choose);
			SelectRequire(head, choose);
		} while (choose != '3');
		break;
	case '3':
		if(head->next == NULL)	printf("No records to delete! Please input records!\n");
		else {
			printf("Input the number of the record you want to delete:");
			fflush(stdin);
			scanf ("%s", num);
			DeleteRecord(head, num);
		}
		break;
	case '4':
		if(head->next == NULL)	printf("No records to modify! Please input records!\n");
		else {
			printf("Input the number of the record you want to modify:");
			fflush(stdin);
			scanf ("%s", num);
			ModifyRecord(head, num);
		}
		break;
	case '5':
		do {
			SortRecords();
			printf("Enter your choose:");
			fflush(stdin);

			scanf ("%c", &choose);
			printf("Original records before sorted:\n");
			Search_All(head);
			SelectSort(head, choose);
			if (choose != '4') {
				printf("New records after sorted:\n");
				Search_All(head);
			}
		} while (choose != '4');
		break;
	case '6':
		SaveRecords(head);
		break;
	case '7':
		LoadRecords(head);
		break;
	case '8':
		printf("You have chosen to quit!\n");
		exit(-1);
		break;
	default:
		printf("You didn't make the right choose. Please choose again!\n");
		break;
	}
	free(num);
}
//二级菜单选择:Require Records
void SelectRequire(PRecord head, char c)
{
	char *num = (char *)calloc(11, sizeof(char));
	switch(c) {
	case '1':
		if(head->next == NULL)	printf("No records to require! Please input records!\n");
		else {
			printf("Input the number of the record you want to find:");
			fflush(stdin);
			scanf ("%s", num);
			Print_Record(SearchRecord(head, num));
		}
		break;
	case '2':
		if(head->next == NULL)	printf("No records! Please input records!\n");
		else {
			printf("All records as follow:\n");
			Search_All(head);
		}
		break;
	case '3':
		printf("You have chosen to back to main menu!\n");
		break;
	default:
		printf("You didn't make the right choose. Please choose again!\n");
		break;
	}
	free(num);
}
//二级菜单选择:Sort Records
void SelectSort(PRecord head, char c)
{
	Record *thead = (PRecord)calloc(1, sizeof(Record)), *p, *tp, *r, *tr;
	thead->Num = (char *)calloc(11, sizeof(char));
	thead->Name = (char *)calloc(21, sizeof(char));
	thead->next = NULL;

	if(head->next == NULL)	printf("No records to sort!\n");
	else {
		switch(c) {
		case '1':
			for(p = head->next; p != NULL; p = tp) {
				tp = p->next;
				tr = thead;
				for(r = thead->next; r != NULL; r = r->next) {
					if(strcmp(p->Num, tr->Num) > 0 && strcmp(p->Num, r->Num) < 0) {
						p->next = r;
						tr->next = p;
						break;
					}
					tr = r;
				}
				if(r == NULL) {
					tr->next = p;
					p->next = NULL;
				}
			}
			head->next = thead->next;
			thead->next = NULL;
			break;
		case '2':
			for(p = head->next; p != NULL; p = tp) {
				tp = p->next;
				tr = thead;
				for(r = thead->next; r != NULL; r = r->next) {
					if(strcmp(p->Name, tr->Name) > 0 && strcmp(p->Name, r->Name) <= 0) {
						p->next = r;
						tr->next = p;
						break;
					}
					tr = r;
				}
				if(r == NULL) {
					tr->next = p;
					p->next = NULL;
				}
			}
			head->next = thead->next;
			thead->next = NULL;
		break;
		case '3':
			for(p = head->next; p != NULL; p = tp) {
				tp = p->next;
				tr = thead;
				for(r = thead->next; r != NULL; r = r->next) {
					if(p->Score[AVERAGE] > tr->Score[AVERAGE] && p->Score[AVERAGE] <= r->Score[AVERAGE]) {
						p->next = r;
						tr->next = p;
						break;
					}
					tr = r;
				}
				if(r == NULL) {
					tr->next = p;
					p->next = NULL;
				}
			}
			head->next = thead->next;
			thead->next = NULL;
			break;
		case '4':
			printf("You have chosen to back to main menu!\n");
			break;
		default:
			printf("You didn't make the right choose. Please choose again!\n");
			break;
		}
	}
	free(thead->Num);
	free(thead->Name);
	free(thead);
}
//链表初始化,构造一带有头节点的一空链表
PRecord Init(PRecord head)
{
	head = (PRecord)calloc(1, sizeof(Record));
	head->next = NULL;
	return head;
}
//信息录入,头插法
PRecord AddRecord(PRecord head, PRecord pr)
{
	pr->next = head->next;
	head->next = pr;
	return head;
}
//输入学号,查询某学生各门课程的成绩及平均成绩;
PRecord SearchRecord(PRecord head, char *num)
{
	Record *p;
	if(head->next == NULL) return NULL;
	for(p = head->next; p != NULL; p = p->next)	if(strcmp(p->Num, num) == 0)	return p;
	return NULL;
}
//打印输出某学生的记录
void Print_Record(Record *p)
{
	if(!p)	{	printf("Not found such record!\n");	return;	}
	printf("Num\t\tName\t\tChinese\tMaths\tEnglish\tAverage\n"); 
	printf("%s\t%s\t\t%d\t%d\t%d\t%d\n", p->Num, p->Name, p->Score[CHINESE], p->Score[MATHS], p->Score[ENGLISH], p->Score[AVERAGE]);
}
//查询所有学生各门课程的成绩;
void Search_All(Record *head)
{
	PRecord p;
	if(head->next == NULL)	return;
	printf("Num\t\tName\t\tChinese\tMaths\tEnglish\tAverage\n");
	for (p = head->next; p != NULL; p = p->next) 
		printf("%s\t%s\t\t%d\t%d\t%d\t%d\n", p->Num, p->Name, p->Score[CHINESE], p->Score[MATHS], p->Score[ENGLISH], p->Score[AVERAGE]);
}
//输入学号,删除该学生的成绩信息;
void DeleteRecord(PRecord head, char *num)
{
	Record *p, *r = head;
	if(head->next == NULL)	return;
	for(p = head->next; p != NULL; p = p->next) {
		if(strcmp(p->Num, num) == 0) {	
			r->next = p->next;	
			free(p->Num);
			free(p->Name);
			free(p);
			printf("Deleted record sucessfully!\n");	
			return;
		}
		r = p;
	}
	if(r->next == NULL)	printf("Not found such record to delete!\n");
}
//输入学号,查询并显示出该学生的成绩信息,并在此基础上进行修改。
void ModifyRecord(PRecord head, char *num)
{
	char ch;
	int scor, sum = 0;
	enum scores s;
	Record *p;
	if(head->next == NULL)	return;
	for(p = head->next; p != NULL; p = p->next)
		if(strcmp(p->Num, num) == 0) {
			Print_Record(p);
			printf("Now you're going to modify the record, choose as follow:\n \
1 to modify the score of Chinese\n \
2 to modify the score of Maths\n \
3 to modify the score of English\n \
0 to quit\n");
			printf("Enter your choose:");
			fflush(stdin);
			do {
				scanf ("%c", &ch);
				switch(ch) {
				case '1': 
					printf("Enter the new score of Chinese:");
					scanf ("%d", &scor);
					p->Score[CHINESE] = scor;
					printf("Modified the record successfully! Enter your new choose:");
					break;
				case '2':
					printf("Enter the new score of Maths:");
					scanf ("%d", &scor);
					p->Score[MATHS] = scor;
					printf("Modified the record successfully! Enter your new choose:");
					break;
				case '3':
					printf("Enter the new score of English:");
					scanf ("%d", &scor);
					p->Score[ENGLISH] = scor;
					printf("Modified the record successfully! Enter your new choose:");
					break;
				case '0':
					printf("You have quit to modify the record!\n");
					break;
				default:
					printf("You didn't make the right choose! Please choose again!\n");
					printf("Enter your new choose:");
					break;
				}
				fflush(stdin);
			} while (ch != '0');
			for(s = CHINESE; s <= ENGLISH; s++)	sum += p->Score[s];
			p->Score[AVERAGE] = sum / (N - 1);
			printf("The new record:\n");
			Print_Record(p);
			return;
		}
	if(p == NULL)	printf("Not found such record to modify!\n");
}
//标准输入一个记录
PRecord GetRecord(PRecord head)
{
	int sum = 0;
	enum scores s;
	Record *pr = (PRecord)calloc(1, sizeof(Record));
	pr->Num = (char *)calloc(11, sizeof(char));
	pr->Name = (char *)calloc(21, sizeof(char));
	
	printf("Input the student's number:");
	fflush(stdin);
	scanf ("%s", pr->Num);

	if(SearchRecord(head, pr->Num) != NULL)	{
		printf("There is already a record of such number!\n");
		return NULL;
	}
	
	printf("Input the student's name:");
	fflush(stdin);
	gets(pr->Name);

	printf("Follow the prompts, input the score of each course:\n"); 
	printf("Input Chinese score:");
	scanf ("%d", &pr->Score[CHINESE]);
	printf("Input Maths score:");
	scanf ("%d", &pr->Score[MATHS]);
	printf("Input English score:");
	scanf ("%d", &pr->Score[ENGLISH]);
	for(s = CHINESE; s <= ENGLISH; s++)	sum += pr->Score[s];
	pr->Score[AVERAGE] = sum / (N - 1);

	return pr;
}
//将信息存储于外部文件records.txt中
void SaveRecords(PRecord head)
{
	Record *p;
	FILE *cfPtr;
	char *file = (char *)calloc(21, sizeof(char));
	printf("Enter the filename you want to save as:");
	fflush(stdin);
	gets(file);
	if((cfPtr = fopen(file, "w+")) == NULL) {	printf("File could not be opened\n"); return;	}
	else 
		if(head->next == NULL) printf("No records to save! Please input records!\n");
		else {
			for(p = head->next; p != NULL; p = p->next)	fprintf(cfPtr, "%s\t%s\t%d\t%d\t%d\t%d\n", p->Num, p->Name, p->Score[CHINESE], p->Score[MATHS], p->Score[ENGLISH], p->Score[AVERAGE]);
			printf("Saved records successfully!\n");
		}
		fclose(cfPtr);
		free(file);
}
//从已保存好的文件records.txt中将信息读入内存
void LoadRecords(PRecord head)
{
	Record *p, *q = head, *r;
	FILE *cfPtr;
	char ch, *name, *file = (char *)calloc(21, sizeof(char));
	enum scores s;
	int i = 0;

	printf("Enter the filename you want to load:");
	fflush(stdin);
	gets(file);
	if((cfPtr = fopen(file, "r+")) == NULL) {	printf("File could not be opened\n");	return;	} 
	else {
		if (feof(cfPtr)) {	printf("No records in the file to load!\n");	fclose(cfPtr);	return;	}
		for(p = head->next; p != NULL; p = p->next)	q = p;
		while (!feof(cfPtr)) {
			p = (Record *)calloc(1, sizeof(Record));
			p->Num = (char *)calloc(11, sizeof(char));
			p->Name = (char *)calloc(21, sizeof(char));
			if(EOF == fscanf(cfPtr, "%s%s%d%d%d%d", p->Num, p->Name, &p->Score[CHINESE], &p->Score[MATHS], &p->Score[ENGLISH], &p->Score[AVERAGE])) 
				break;
			r = SearchRecord(head, p->Num); 
			if(r != NULL)	{
				printf("There is already a record of such number! Do you want to modify?(Y/N):");
				do {
					fflush(stdin);
					scanf ("%c", &ch);
					switch(ch) {
					case 'Y': case 'y':
						name = r->Name;
						r->Name = p->Name;
						for(s = AVERAGE; s <= ENGLISH; s++)	r->Score[s] = p->Score[s];
						printf("You have modified %d record!\n", ++i);
						free(name);
						free(p->Num);
						free(p);
						break;
					case 'N': case 'n':
						free(p->Num);
						free(p->Name);
						free(p);
						printf("The record you access from the file have been neglected!\n");
						break;
					default:
						printf("You didn't make the right choose! Please choose again:");
						break;
					}
				} while (ch != 'Y' && ch != 'N' && ch != 'y' && ch != 'n');
			}
			else {
				q->next = p;
				q = p;
				p->next = NULL;
				p = p->next;
			}
		}
		printf("Completed loading records from the file successfully!\n");
	}
	fclose(cfPtr);
	free(file);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -