C language implementation of student management system (hand-in-hand teaching)

Posted by melsi on Wed, 15 Dec 2021 12:49:27 +0100

How to implement the student management system? First of all, we should be able to analyze the framework of the problem. In this way, when writing later, you will have a clear understanding of what you need.

Then, the task of the management system is to delete, find and modify student information, sort and print information.

These are the most basic functions. Write these functional frameworks in a c source file as the running area of the main function.

#include"StudentManager.h"

enum Option
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SORT,
	PRINT
};

int main()
{
	int input = 0;
	//Create personnel list
	Student stulist;//Personnel list
	//Initialize personnel list
	InitStudent(&stulist);
	do 
	{
		menu();
		printf("Please select:\n");
		scanf_s("%d", &input);
		switch(input)
		{
			case ADD:
				AddStudent(&stulist);//Modify a certain transmission address
				break;
			case DEL:
				DelStudent(&stulist);
				break;
			case SEARCH:
				SearchStudent(&stulist);//It can transfer values, but it is more efficient to transfer addresses
				break;
			case MODIFY:
				ModifyStudent(&stulist);
				break;
			case SORT:
				SortStudent(&stulist);
				break;
			case PRINT:
				PrintStulist(&stulist);
				break;
			case EXIT:
				printf("Exit management system\n");
			default:
				break;
		}
	} while (input);
	SaveFile(&stulist);
}

========================================================================= 

Then, a separate file is used to realize the declaration of the functions used, so as to make the business logic clearer: (at the same time, try to define the required storage types here, so that the function functions can be written more clearly)

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//Structure definition
typedef struct StuInfo
{
	//(1) The personal information of each student includes: name, student number, native place, age, mathematics, English and physics scores
	char name[15];
	char address[two 0];
	char ID[15];
	int age;
	int score[3];
	int sum;
	int order;
}StuInfo;
//Encapsulate the structure
typedef struct Student
{
	StuInfo data[100];  //Stored personnel information
	int size;  //Record the current number of people
}Student;

int FindByName(Student* pc, char name[]);
void InitStudent(Student* pc);
void AddStudent(Student* pc);
void DelStudent(Student* pc);
void SearchStudent(Student* pc);
void ModifyStudent(Student* pc);
void SortStudent(Student* pc);
void SaveFile(Student* pc);
void PrintStulist(const Student* pc);
void menu();

 =========================================================================

Then, it is the realization of the function function. Before realizing the function, we have determined what the storage method of student information we write is. Therefore, it is the realization of function function.

First, the most basic is the menu interface:

//Display menu interface
void menu()
{
	printf("---------------\n");
	printf("1.increase     2.Delete \n");
	printf("3.check     four.change \n");
	printf("5.Row     six.Print \n");
	printf("    0.sign out    \n");
	printf("---------------\n");
}

========================================================================= 

Then write a function to initialize the space (not much explanation):

//Initialize data for use
void InitStudent(Student* pc)
{
	pc->size = 0;
	memset(pc->data, 0, sizeof(pc->data));//Initialize all data to 0
}

Then comes the realization of some functions. For example, we first realize a printing function, because the code of the printing function can also be used and reflected in other functions (such as search):

If the pointer pc wants to dereference the address, it needs to use '- >' and others are '.'. Therefore, the operation of printing information is clear at a glance. First, the pointer dereference takes a single student information, and then the structure takes out the corresponding information data.

//Print information
void PrintStulist(const Student* pc)
{
	int i = 0;
	//Print title
	printf("%-10s\t%-10s\t%-10s\t%-10s\t%-20s\n", "full name", "Student number", "Native place", "Age", "achievement:Mathematics, English, physics");
	for (i = 0; i < pc->size; i++)
	{
		printf("%-10s\t%-10s\t%-10s\t%-10d\t%d\t%d\t%d\n",
			pc->data[i].name,
			pc->data[i].ID,
			pc->data[i].address,
			pc->data[i].age,
			pc->data[i].score[0], pc->data[i].score[1], pc->data[i].score[2]);//Entered three subject scores
	}

}

Here, we need to briefly explain this function. The const read-only attribute is used because we accept the information of the student list, However, in order to ensure that the student list information will not be misoperated (operations other than printing), so const is added. Then some people may ask, since they are afraid of being modified, why not directly define an ordinary variable to accept student information and use a pointer? Then think about it: the pointer accepts the address and the ordinary variable accepts all the information. Which is more efficient? Of course, the efficiency of address transmission is higher.

=========================================================================

Then there is the added function. A new student comes to the class and has to re-enter the information, so what does the added function need? Of course, you need to modify the corresponding information (when the student list defined here is encapsulated, it is defined as 100. Of course, you can also select #define macro definition for later maintenance):

//Add personnel information
void AddStudent(Student* pc)
{
	if (pc->size == 100)
	{
		printf("The list is full and cannot be added\n");
		return;
	}
	//Add information
	printf("Please enter your first name:\n");
	scanf_s("%s", pc->data[pc->size].name, 15);
	printf("Please enter student number:\n");
	scanf_s("%s", pc->data[pc->size].ID, 15);
	printf("Please enter your native place:\n");
	scanf_s("%s", pc->data[pc->size].address, 20);
	printf("Please enter age:\n");
	scanf_s("%d", &(pc->data[pc->size].age));
	printf("Please enter your math, English and physics scores:\n");
	scanf_s("%d %d %d", &(pc->data[pc->size].score[0]), &(pc->data[pc->size].score[1]), &(pc->data[pc->size].score[2]));
	pc->size++;
	printf("Information added successfully\n");
}

The operation of adding is very similar to printing, so I won't explain more.

=========================================================================

Well, let's look at the remaining functions, including modification, deletion and search. Let's carefully analyze these three functions. All of them need to find a student in a targeted way. Therefore, let's write a search function first (search can only be found in order, one by one to see which meets the conditions, so use for or while):

//Define a search function to delete, search and use
int FindByName(Student* pc, char name[])
{
	int i = 0;
	for (i = 0; i < pc->size; i++)
	{
		if ((strcmp(pc->data[i].name, name)) == 0)
		{
			return i;
		}
	}
	return -1;
}

The search here uses name search. You can also change the search function to achieve more advanced operations, such as finding people according to their grades and ID (the essence is the same).

=========================================================================

According to this search function, we can delete and modify. When a student commits a heinous crime, he will have to drop out of school, so next, we can delete:

It should be noted here that because the student's information has been changed, it must be sent to the address.

//Delete personnel information
void DelStudent(Student* pc)
{
	char name[15] = { 0 };
	if (pc->size == 0)
	{
		printf("The list is empty. There is no information to delete\n");
		return;
	}
	//1. Find the person to delete
	printf("Please enter the person to delete:\n");
	scanf_s("%s", name, 15);

	int position = FindByName(pc, name);
	/*
	Yes / no -- > 2 delete
	*/
	if (position == -1)
	{
		printf("The person to delete does not exist");
		return;
	}
	//delete
	int i = 0;
	for (i = position; i < pc->size - 1; i++)//size-1 is because the last element on the left does not need to be overwritten and will be deleted later
	{
		pc->data[i] = pc->data[i + 1];//Overwrite in turn to move forward and delete

	}
	pc->size--;
	printf("Delete succeeded\n");
}

The implementation of deletion is quite simple, because in fact, it is equivalent to finding the student, moving the whole table forward and directly covering the student (there is no way, the defect of the sequence table is that the deletion operation time complexity is high). At the same time, pay attention to judge whether the student exists when writing.

What about the lookup operation? The search function we wrote above is just that the program finds the information of this person. If you really need to find it, you have to check the information of this person. If the students in the class want to know their own information and ask the teacher to check it, they must output the information. Therefore, some printed codes are also removed here:

//Find the specified person -- > borrowed the code to delete the person and the code to print the information
void SearchStudent(Student* pc)
{
	char name[15] = { 0 };
	printf("Please enter the person you want to find:\n");
	scanf_s("%s", name, 15);
	int position = FindByName(pc, name);
	if (position == -1)
	{
		printf("The person you are looking for does not exist");
		return;
	}//The same code as the deleted judgment
	else//Go to print and find the code, and pay attention to changing some contents
	{
		printf("%-10s\t%-10s\t%-10s\t%-10s\t%-20s\n", "full name", "Student number", "Native place", "Age", "achievement:Mathematics, English, physics");
		printf("%-10s\t%-10s\t%-10s\t%-10d\t%d\t%d\t%d\n",
			pc->data[position].name,
			pc->data[position].ID,
			pc->data[position].address,
			pc->data[position].age,
			pc->data[position].score[0], pc->data[position].score[1], pc->data[position].score[2]);//Code picked from the print
	}
}

So, basically, as long as you understand the first few codes, the rest will be solved. Then we still have the modification functions related to search:

//Modify the specified person information -- > borrowed the searched code and the added code
void ModifyStudent(Student* pc)//Transform with the code found
{
	char name[15] = { 0 };
	printf("Please enter the person to modify:\n");
	scanf_s("%s", name, 15);
	int position = FindByName(pc, name);
	if (position == -1)
	{
		printf("The person to modify does not exist");
		return;
	}//The same code as the deleted judgment
	else//Note that this is the same code as adding
	{
		printf("Please enter your first name:\n");
		scanf_s("%s", pc->data[position].name, 15);
		printf("Please enter student number:\n");
		scanf_s("%s", pc->data[position].ID, 15);
		printf("Please enter your native place:\n");
		scanf_s("%s", pc->data[position].address, 20);
		printf("Please enter age:\n");
		scanf_s("%d", &(pc->data[position].age));
		printf("Please enter your math, English and physics scores:\n");
		scanf_s("%d %d %d", &(pc->data[position].score[0]), &(pc->data[position].score[1]), &(pc->data[position].score[2]));
		printf("Information modified successfully\n");
	}
}

Did you find it? It's a little like adding information. In short, the functions related to search are basically implemented, so the sorting function is not implemented. Since the sorting function has little to do with search, it is listed as the next block.

=========================================================================

Sorting, there are many sorting algorithms we can use, such as selection sorting, bubble sorting, insertion sorting, quick sorting, etc. here, a more understandable insertion sorting algorithm is used to sort students (sort by calculating the total score):

//Sort people
void SortStudent(Student* pc)
{
	for (int i = 0; i < pc->size; i++)
	{
		pc->data[i].sum = pc->data[i].score[0] + pc->data[i].score[1] + pc->data[i].score[2];
	}
	printf("Sorting result of total score:\n");//Use insert sort to do this
	StuInfo p;
	int i, j;
	for (i = 1; i < pc->size; i++)
		if (pc->data[i].sum > pc->data[i - 1].sum)
		{
			p = pc->data[i];
			for (j = i - 1; j >= 0 && pc->data[j].sum < p.sum; j--)
				pc->data[j + 1] = pc->data[j];
			pc->data[j + 1] = p;
		}
	StuInfo* t;
	i = 0;
	for (t = pc->data; t < pc->data + pc->size; t++)
	{
		pc->data[i].order = i + 1;
		i++;
	}
	//Print out the sorted list
	printf("%-10s\t%-10s\t%-10s\t%-10s\t%-20s\n", "full name", "Student number", "Native place", "Age", "achievement:Mathematics, English, physics");
	for (int i = 0; i < pc->size; i++)
	{
		printf("%-10s\t%-10s\t%-10s\t%-10d\t%d\t%d\t%d\n",
			pc->data[i].name,
			pc->data[i].ID,
			pc->data[i].address,
			pc->data[i].age,
			pc->data[i].score[0], pc->data[i].score[1], pc->data[i].score[2]);//Entered three subject scores
	}
}

Then, this code only slightly explains the sorting algorithm used. Insert sorting is to find the maximum value (you have to cycle to find it), and then put it in front. After cycling, you get a sequence. The specific algorithm will be explained in a CSDN later. After all, there are still a lot of sorting contents.

At the same time, this function also uses the printing code. After all, the order must be seen by others, so it also prints the information of the whole student list.

After writing here, all functions have been basically realized. However, since students' information may eventually be saved in files for future use, a simple file saving function is added here. Of course, if readers want to read files to write the management system, it is also possible:

//Storage file
void SaveFile(Student* pc)
{
	FILE* fp = NULL;
	fopen_s(&fp, "studentInfo.txt", "wt+");  //b means to open the file in binary mode
	if (fp == NULL) //Failed to open file with error message
	{
		printf("open file for write error\n");
	}
	int i = 0;
	for (i = 0; i < pc->size; i++) {
		fprintf(fp, "%-10s\t%-10s\t%-10s\t%-10d\t Results of three subjects:%d\t%d\t%d\n",
			pc->data[i].name,
			pc->data[i].ID,
			pc->data[i].address,
			pc->data[i].age,
			pc->data[i].score[0], pc->data[i].score[1], pc->data[i].score[2]);
	}
	fclose(fp);
	fp = NULL;
}

Finally, even if our student management system is completed in time, let's have a try with the code!

Topics: C Back-end