C language realizes the practical teaching of student achievement management system

  • 2020-06-12 10:15:10
  • OfStack

Start developing small projects in C while you're on vacation, consolidating your basic knowledge and learning new ones.

The student achievement management system realizes the function has: the achievement entry, the student achievement inquiry, the deletion, the modification, through the document saves and so on.

Knowledge required to develop such a system: linear tables (linked lists), file manipulation, sorting (if grades are required).

The development environment is VS2015; There is no header file for ES10en.h under Linux, and you need to modify the code associated with the getch() function.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
 
/* Student information structure */
typedef struct Node
{
	char Name[10];		// The student's name 
	char ID[15];		// Student student id 
	int Score[3];	//3 Subject grades (Mathematics, English, Data Structure) 
	float Ave_Sco;
	struct Node *next;
}Lnode;
 
void Display();  /* Interface display function */
void GetScore(Lnode *&h); /* Score entry function */
void PrintScore(Lnode *h); /* Result printing function */
void ModifyScore(Lnode *h); /* Grade modification function */
void FindInf(Lnode *h);  /* To find information */
void Delete(Lnode *h);  /* Delete function */
void Quit(Lnode *h);  /* Exit function */
void SaveInf(Lnode *h);
void LoadInf(Lnode *h);
 
/* Initializes the list */
void InitList(Lnode *&head) 
{
	head = (Lnode *)malloc(sizeof(Lnode));
	if (head == NULL)
	{
		printf("error!");
		exit(1);
	}
	head->next = NULL;  // Nullifies the header node pointer field 
}
 
int main()
{
	Lnode *ScoreList;  // Create a linked list of grades where all student information is stored 
	int Function;
	char flag; 
	int t = 0;
	InitList(ScoreList);
	LoadInf(ScoreList);
 
	while (1)
	{
		Display();
		printf(" Please select operation:  ");
		scanf("%d", &Function);
		switch (Function)
		{
		case 1: while (1)
		{
			GetScore(ScoreList);
			printf(" Whether to continue typing   ( Y/N ) ");
			scanf("%s", &flag);
			if (flag == 'N' || flag == 'n')break;
		} 	system("cls"); break;
		case 2: PrintScore(ScoreList);	_getch(); system("cls"); break;
		case 3: ModifyScore(ScoreList);	system("cls"); break;
		case 4: FindInf(ScoreList); _getch(); system("cls"); break;
		case 5: Delete(ScoreList); _getch(); system("cls"); break;
		case 6: Quit(ScoreList); break;
 
		default: printf("Error !!!!!!!!!   Please re-enter: ");
			break;
		} //switch The end of the 
	}
	
	return 0;
}
 
/* System interface display */
void Display()
{
	printf("\t\t**********************************************\n");
	printf("\t\t************* Welcome to the performance management system *************\n");
	printf("\t\t**********************************************\n");
	printf("\t\t\t\t1 , record the score \n");
	printf("\t\t\t\t2 , print results \n");
	printf("\t\t\t\t3 , modify grades \n");
	printf("\t\t\t\t4 , search for student information \n");
	printf("\t\t\t\t5 , delete student information \n");
	printf("\t\t\t\t6 , quit the system \n");
	printf("\n\n\n\n\n\n");
}
 
/* Achievements recorded */
void GetScore(Lnode *&h)
{
	Lnode *p, *q = h;
	char name[10], id[15];
	int Math, English, Datastruct;
	p = (Lnode *)malloc(sizeof(Lnode));		// Application nodes for student information 
	printf(" Please enter student information in turn: \n");
	printf(" The name   Student id   mathematics   English   The data structure \n");
	scanf("%s %s %d %d %d", &name, &id, &Math, &English, &Datastruct);
 
	for (; q->next != NULL; q = q->next){;}  // Move to the tail node 
	
	strcpy(p->Name, name);
	strcpy(p->ID, id);
	p->Score[0] = Math;
	p->Score[1] = English;
	p->Score[2] = Datastruct;
	p->Ave_Sco = ((float)((p->Score[0] + p->Score[1] + p->Score[2]) - 150)) / 30;
 
	p->next = NULL;
	q->next = p;
	q = p;
}
 
/* Results the print */
void PrintScore(Lnode *h)
{
 
	Lnode *p = h->next;
	printf("%-14s%-8s%-8s%-8s%-8s%-8s\n"," ranking ", " Student id ", " The name ", " mathematics ", " English ", " The data structure ", " Grade point average ");
	while (p != NULL)
	{
		printf("%-14s%-8s%-8d%-8d%-8d%.2f\n", p->ID, p->Name, p->Score[0], p->Score[1], p->Score[2], p->Ave_Sco);
		p = p->next;
	}
}
 
/* Results modified */
void ModifyScore(Lnode *h)
{
	Lnode *p = h->next;
	char name[10], id[15];
	int Math, English, Datastruct;
	printf(" Please enter student name: ");
	scanf("%s", name);
	printf(" Please enter student id: ");
	scanf("%s", id);
 
	while (p)
	{
		if (strcmp(p->Name, name)==0 && strcmp(p->ID, id)==0)
		{
			printf(" Current Student Information :\n");
			printf("%-14s%-8s%-8s%-8s%-8s\n", " Student id ", " The name ", " mathematics ", " English ", " The data structure ");
			printf("%-14s%-8s%-8d%-8d%-8d\n", p->ID, p->Name, p->Score[0], p->Score[1], p->Score[2]);
			printf(" Please enter the corrected math score: ");
			scanf("%d", &Math);
			printf(" Please enter the corrected English score: ");
			scanf("%d", &English);
			printf(" Please enter the corrected data structure score: ");
			scanf("%d", &Datastruct);
			p->Score[0] = Math;
			p->Score[1] = English;
			p->Score[2] = Datastruct;
			break;
		}
		else
		{
			p = p->next;
		}
	}//while End of the cycle 
}
 
/* Find information */
void FindInf(Lnode *h)
{
	Lnode *p = h->next;
	char name[10], id[15];
	printf(" Please enter student name: ");
	scanf("%s", name);
	printf(" Please enter student id: ");
	scanf("%s", id);
 
	while (p)
	{
		if (strcmp(p->Name, name) == 0 && strcmp(p->ID, id) == 0)
		{
			printf(" Current Student Information :\n");
			printf("%-14s%-8s%-8s%-8s%-8s\n", " Student id ", " The name ", " mathematics ", " English ", " The data structure ");
			printf("%-14s%-8s%-8d%-8d%-8d\n", p->ID, p->Name, p->Score[0], p->Score[1], p->Score[2]);
			break;
		}
		else
		{
			p = p->next;
		}
	}//while End of the cycle 
}
 
/* delete */
void Delete(Lnode *h)
{
	Lnode *p = h, *q;
	q = p->next;
	char name[10], id[15];
	printf(" Please enter student name: ");
	scanf("%s", name);
	printf(" Please enter student id: ");
	scanf("%s", id);
 
	while (q)
	{
		if (strcmp(q->Name, name) == 0 && strcmp(q->ID, id) == 0)
		{
			p->next = q->next;
			free(q);  // delete p node 		
			printf(" Delete the success \n");
			break;
		}
		else
		{
			p = p->next;
			q = q->next;
		}
	}//while End of the cycle 
}
 
/* Log out */
void Quit(Lnode *h)
{
	SaveInf(h);  // Save the information on exit 
	exit(0);
}
 
/* Open the file */
void LoadInf(Lnode *h)
{
	Lnode *p = h;
	Lnode *q;  // Temporary variable   Used to hold information read from a file 
	FILE* file = fopen("./Information.dat", "rb");
	if (!file)
	{
		printf(" File opening failed! ");
		return ;
	}
 
	/*
		 use feof Questions to note when judging whether the file is closed: 
			 When the reading of the file ends, feof The function does not immediately set the flag to -1 , but 
			 Need to reread 1 After that, it will be set. So read it first 1 Times. 
	*/
	q = (Lnode *)malloc(sizeof(Lnode));
	fread(q, sizeof(Lnode), 1, file);
	while (!feof(file))  //1 Read straight to the end of the file 
	{
		p->next = q;
		p = q;
		q = (Lnode *)malloc(sizeof(Lnode));
		fread(q, sizeof(Lnode), 1, file);
	} //while End of the cycle 
 
	p->next = NULL;
	fclose(file);
}
 
/* Save the information to a file */
void SaveInf(Lnode *h)
{
	Lnode *p = h->next;
	int flag;
	FILE* file = fopen("./Information.dat", "wb");
	if (!file)
	{
		printf(" File opening failed! ");
		return;
	}
	while (p != NULL)
	{
		flag = fwrite(p, sizeof(Lnode), 1, file);  // will p Writes to the file 
		if (flag != 1)
		{
			break;
		}
		p = p->next;
	}
	fclose(file);
}

Even though it's a small, simple project, there are a lot of problems.

1: Linked list correlation

In writing achievements recorded and print function, found only (no join file) always the last one data, determine the list of related operation no problem, after careful judgment logic relationship, every time is found in the first node to GetScore () function, after the application of memory for the new node, directly to the data stored in the new application of nodes, and did not move the list to the end node, each entry, will be covered before one of the input data. The solution is to move the list to the last node after passing it to the function, and hook the newly applied node after the last node.


/* Achievements recorded */
void GetScore(Lnode *&h)
{
	Lnode *p, *q = h;
	char name[10], id[15];
	int Math, English, Datastruct;
	p = (Lnode *)malloc(sizeof(Lnode));		// Application nodes for student information 
	printf(" Please enter student information in turn: \n");
	printf(" The name   Student id   mathematics   English   The data structure \n");
	scanf("%s %s %d %d %d", &name, &id, &Math, &English, &Datastruct);
 
	for (; q->next != NULL; q = q->next){;}  // Move to the tail node 
	// Save the data 
	strcpy(p->Name, name);
	strcpy(p->ID, id);
	p->Score[0] = Math;
	p->Score[1] = English;
	p->Score[2] = Datastruct;
	p->Ave_Sco = ((float)((p->Score[0] + p->Score[1] + p->Score[2]) - 150)) / 30;
        // Always pointing to the end 1 A node 
	p->next = NULL;
	q->next = p;
	q = p;
}

2. File operation

The main problem with file saving is that every time you print data, you always get 1 more line of code than normal. The method is while(! feof (file)). When you rule out an error, you determine two possibilities: saving an extra line; One more row has been read. After a certain degree of relationship between feof() and EOF, it is determined that one more row is read.

When using the feof() function to judge the end of a file, the feof() function will return -1 only after one reading after the file has reached the end. The solution is to read the first data before the loop reads, and then read it normally. Notice the problem of reading one more time.


/* Open the file */
void LoadInf(Lnode *h)
{
	Lnode *p = h;
	Lnode *q;  // Temporary variable   Used to hold information read from a file 
	FILE* file = fopen("./Information.dat", "rb");
	if (!file)
	{
		printf(" File opening failed! ");
		return ;
	}
 
	/*
		 use feof Questions to note when judging whether the file is closed: 
			 When the reading of the file ends, feof The function does not immediately set the flag to -1 , but 
			 Need to reread 1 After that, it will be set. So read it first 1 Times. 
	*/
	q = (Lnode *)malloc(sizeof(Lnode));
	fread(q, sizeof(Lnode), 1, file);
	while (!feof(file))  //1 Read straight to the end of the file 
	{
		p->next = q;
		p = q;
		q = (Lnode *)malloc(sizeof(Lnode));
		fread(q, sizeof(Lnode), 1, file);
	} //while End of the cycle 
	p->next = NULL;
	fclose(file);
}


Related articles: