当前位置:网站首页>C language implementation of classroom random roll call system

C language implementation of classroom random roll call system

2022-07-23 14:31:00 Namely ang

Today, review the curriculum design written in school , Review what you learned in school , At the same time, I also hope to help those poor people who are tortured by the curriculum like me ~

1、 Project requirement

Random roll call procedure in class
requirement It can fully realize the existing functions of our roll call program . That is, download the list of students in a class from the website of the academic affairs office and save it for future use . Your program can selectively load the save file of a class in some way and read the corresponding student information into memory , And then to Random To find the information of a student and display it to realize roll call .

2、 Implementation of project functions

To achieve a complete roll call system , except The most basic random roll call , There needs to be more 、 Delete 、 modify And so on , This requires us to think , Implement them in a functional way . following , Is the function I used in this project .

First, let's take a look at the software structure diagram of the project






First, define a structure , Because it is only used for class roll call , Therefore, the information of students in the structure here only includes student number and name .

typedef struct StuInfo
{
    
	int id;
    char name[MAX];
}StuInfo;

Function one : Read the contents of the selected open file into the structure

// Read the contents of the file into the structure 
void stu_open(char* file, FILE* fp,StuInfo* s,int num)
{
    
	int i = 0;
	fscanf(fp, "%d", &num);// Number of students enrolled in class 
	for (i = 0; i < num; i++)
	{
    
		fscanf(fp, "%d %s", &(s[i].id), s[i].name);// Read the information of each student in a circle 
	}
	return;
}

When passing parameters here ,file Is to select the file to open ,s Structure array created for ,num For class size , These will be presented later in the complete code , Because here is to explain the specific code content , Therefore, the specific creation process is not reflected . The transmission of the following functions is the same .



Function two : Random roll call function

// Random roll call 
void stu_byname(char* file,StuInfo* s,int num)
{
    
	FILE* fp = fopen(file, "r+");
	fscanf(fp, "%d", &num);
	int i = 0;
	i = rand() % num;// Get the random number in the class number 
	printf("\n Student number : %d\n full name : %s\n", s[i].id, s[i].name);// Print and display the student information corresponding to the random number 
}

The random number generator has been set in the main function :srand((unsigned int)time(NULL)), Therefore, we only need to use it directly rand() Get a random number .

By the way, let's explain how random numbers are obtained :
srand Set a random number seed for , To prevent the repetition of random numbers generated each time , Therefore, we need to set different random number seeds , And time is changing every moment , So we can use timestamp as random number seed , To generate different random numbers .time Function can generate the timestamp of the current time , Its standard form is time_t time (time_t* timer), Its parameter is a pointer , We just need to call time function , Therefore, the parameter can be written directly as NULL,time The return type of is time_t, and srand What you need is an integer , So we also need to cast it to unsigned int , This constitutes our final random number generator :srand((unsigned int)time(NULL)), When generating random numbers later , Just call rand() Function .

Time stamp :
 Insert picture description here

Function three : Add student information

// Add student information 
void stu_add(char* file, StuInfo* s, int num)
{
    
	FILE* fp = fopen(file, "r+");
	fscanf(fp, "%d", &num);
	int i = 0;
	int j = 0;
	int m = 0;
	char str[20] = {
     0 };
	printf(" Please enter the student number and name of the added student :>");
	scanf("%d %s", &m, &str);

	// Judge whether the student number already exists 
	for (i = 0; i < num; i++)
	{
    
		if (m == s[i].id)
		{
    
			printf(" The student number already exists !\n");
			printf(" Add failure !\n");
			break;
		}
	}

	// If it does not exist , Add to the student 
	if (i == num)
	{
    
		num = num + 1;
		// Reset the file pointer to the beginning of the file , And re write the class number 
		rewind(fp);
		fprintf(fp, "%d", num);
		// Move the file pointer to the end of the file , Write the newly added student 
		fseek(fp, 0, SEEK_END);
		fprintf(fp, "\n%d %s", m, str);
		printf(" Add success \n");
	}
}

Function four : Delete student information

// Delete student information 
void stu_del(char* file, StuInfo* s, int num)
{
    
	FILE* fp = fopen(file, "r+");
	fscanf(fp, "%d", &num);
	stu_close(file, fp);
	// Close the file , And start again with "w+" To open the file 
	FILE* ffp = fopen(file, "w+");
	int input = 0;
	int k = 0;
	do
	{
    
		menu_del();
		printf(" Please enter the deletion method :>");
		scanf("%d", &input);
		switch (input)
		{
    
		case 1:
			stu_del_id(file, fp,s,num);
			break;
		case 2:
			stu_del_name(file, fp,s,num);
			break;
	    // Because it is with "w+" The way to open the file , The contents will be emptied , therefore , If you cancel or exit delete , You need to write the contents of the structure into the file 
		case 0:
			fprintf(ffp, "%d\n", num);
			for (k = 0; k < num; k++)
			{
    
				fprintf(ffp, "%d %s", s[k].id, s[k].name);
				if (k < num - 1)
				{
    
					fputc('\n', ffp);
				}
			}
			printf(" Exit delete !\n");
			break;
		default:
			rewind(ffp);
			fprintf(ffp, "%d\n", num);
			for (k = 0; k < num; k++)
			{
    
				fprintf(ffp, "%d %s", s[k].id, s[k].name);
				if (k < num - 1)
				{
    
					fputc('\n', ffp);
				}
			}
			printf(" Input error ! Please re-enter !\n");
			break;
		}
		stu_close(file, ffp);
	} while (input);
}

Here is Two ways to delete : Delete by student number and by name ( The following query 、 modify 、 Sorting also includes these two methods ), And confirm the deletion and other operations , Further improve the function of the code . Next is the specific code of the two deletion methods :

// Delete by student number 
void stu_del_id(char* file, FILE* fp,StuInfo* s,int num)
{
    
	int m = 0;
	int i = 0;
	int j = 0;
	int k = 0;
	int flag = 0;
	
	printf(" Please enter the student number to delete :>");
	scanf("%d", &m);
	for (i = 0; i < num; i++)
	{
    
		if (m == s[i].id)
		{
    
			// Confirm whether to delete 
			printf(" The student information is :\n");
			printf(" Student number :>%d  full name :>%s\n", s[i].id, s[i].name);
			printf(" Are you sure you want to delete this student's information ?(0/1):>");
			scanf("%d", &flag);
			switch (flag)
			{
    
			case 1:
				for (j = i; j < num; j++)
				{
    
					s[j] = s[j + 1];
				}
				num--;
				// Reset the file pointer to the beginning of the file , And re write the class number 
				rewind(fp);
				fprintf(fp, "%d\n", num);
				for (k = 0; k < num; k++)
				{
    
					fprintf(fp, "%d %s", s[k].id, s[k].name);
					if (k < num - 1)
					{
    
						fputc('\n', fp);
					}
				}
				printf(" Delete successful !\n");
				break;
			case 0:
				printf(" Successful operation ! Cancel deletion , Go back to the upper menu !\n");
				break;
			default:
				printf(" Input error ! Please re-enter :>\n");
				break;
			}
			break;
		}
	}
	if (i == num)
	{
    
		printf(" The student number does not exist !\n");
		printf(" Delete failed !\n");
	}
}
// Delete by name 
void stu_del_name(char* file, FILE* fp,StuInfo* s,int num)
{
    
	int i = 0;
	int j = 0;
	int k = 0;
	int flag = 0;
	char str[20] = {
     0 };
	//fscanf(fp, "%d\n", &num);
	//stu_open(file, fp, s, num);
	printf(" Please enter the name to delete :>");
	scanf("%s", &str);
	for (i = 0; i < num; i++)
	{
    
		if (strcmp(s[i].name, str) == 0)
		{
    
			// Confirm whether to delete 
			printf(" The student information is :\n");
			printf(" Student number :>%d  full name :>%s\n", s[i].id, s[i].name);
			printf(" Are you sure you want to delete this student's information ?(0/1):>");
			scanf("%d", &flag);
			switch (flag)
			{
    
			case 1:
				for (j = i; j < num; j++)
				{
    
					s[j] = s[j + 1];
				}
				num--;
				// Reset the file pointer to the beginning of the file , And re write the class number 
				rewind(fp);
				fprintf(fp, "%d\n", num);
				for (k = 0; k < num; k++)
				{
    
					fprintf(fp, "%d %s", s[k].id, s[k].name);
					if (k < num - 1)
					{
    
						fputc('\n', fp);
					}
				}
				printf(" Delete successful !\n");
				break;
			case 0:
				printf(" Successful operation ! Cancel deletion , Go back to the upper menu !\n");
				break;
			default:
				printf(" Input error ! Please re-enter :>\n");
				break;
			}
			break;
		}
	}
	if (i == num)
	{
    
		printf(" The name does not exist !\n");
		printf(" Delete failed !\n");
	}
}

The general idea of the two deletion methods is the same , It is to find the information of the student and print it first , Confirm whether to delete , After confirming the deletion , Cover up one by one with the information of the students behind , And reduce the number of classes by one , To achieve the effect of deleting information .

Function five : Search for student information

// Query information 
void stu_located(char* file, StuInfo* s, int num)
{
    
	FILE* fp = fopen(file, "r+");
	fscanf(fp, "%d", &num);
	int input = 0;
	do
	{
    
		menu_located();
		printf(" Please select the query method :>");
		scanf("%d", &input);
		switch (input)
		{
    
		case 1:
			located_id(s,num);
			break;
		case 2:
			located_name(s,num);
			break;
		case 0:
		    printf(" Cancel the query !\n");
			break;
		default:
			printf(" Input error ! Please re-enter :>\n");
			break;
		}
	} while (input);
}
// By student number 
void located_id(StuInfo* s, int num)
{
    
	int m = 0;
	int i = 0;
	printf(" Please enter the student number to inquire :>");
	scanf("%d", &m);
	for (i = 0; i < num; i++)
	{
    
		if (m == s[i].id)
		{
    
			// The query is successful , Output the student information 
			printf(" The student information is :\n");
			printf(" Student number :>%d  full name :>%s\n", s[i].id, s[i].name);
			break;
		}
	}
	if (i == num)
	{
    
		printf(" The student number does not exist !\n");
	}
}
// Search by name 
void located_name(StuInfo* s, int num)
{
    

	char s1[MAX];
	int i = 0;
	int j = 0;

	printf(" Please enter the name you want to query :>");
	scanf("%s", &s1);
	for (i = 0; i < num; i++)
	{
    
		if (strcmp(s1, s[i].name) == 0)
		{
    
			printf(" Student number :>%d  full name :>%s\n", s[i].id, s[i].name);
			break;
		}
	}
	if (i == num)
	{
    
		printf(" The name does not exist !\n");
	}
}

Function six : Modify student information

// Modify student information 
void stu_multify(char* file, StuInfo* s, int num)
{
    
	FILE* fp = fopen(file, "r+");
	fscanf(fp, "%d", &num);
	int input = 0;
	do
	{
    
		menu_multify();
		printf(" Please select the modification method :>");
		scanf("%d", &input);
	switch (input)
		{
    
		case 1:
			multify_id(file, fp,s,num);
			break;
		case 2:
			multify_name(file, fp,s,num);
			break;
		case 0:
		    printf(" Cancel and modify !\n");
			break;
		default:
			printf(" Input error ! Please re-enter :>\n");
			break;
		}
	} while (input);
}
// Enter student number to modify 
void multify_id(char* file, FILE* fp, StuInfo* s, int num)
{
    
	char s1[20] = {
     0 };
	int i = 0;
	int j = 0;
	int k = 0;
	int m = 0;
	int n = 0;
	printf(" Please input the student number of the student to be modified :>");
	scanf("%d", &m);
	for (i = 0; i < num; i++)
	{
    
		if (m == s[i].id)
		{
    
			printf(" The student number of the current student is :>%d  The name is :>%s\n", s[i].id, s[i].name);
			printf(" Please enter the modified student number :>");
			scanf("%d", &n);

			// Judge whether the modified student number already exists 
			for (k = 0; k < num; k++)
			{
    
				if (n == s[k].id)
				{
    
					printf(" The student number already exists !\n");
					printf(" The student's name is :>%s\n", s[k].name);
					break;
				}
			}
			if (k == num)
			{
    
				s[i].id = n;
				printf(" The student's student number after revision is :>%d  The name is :>%s\n", s[i].id, s[i].name);
				rewind(fp);
				fprintf(fp, "%d\n", num);
				for (j = 0; j < num; j++)
				{
    
					fprintf(fp, "%d %s", s[j].id, s[j].name);
					if (j < num - 1)
					{
    
						fputc('\n', fp);
					}
				}
				printf(" Modification successful !\n");
				break;
			}
		}
	}
	if (i > num)
	{
    
		printf(" The student number does not exist !\n");
		printf(" Modification failed !\n");
	}
}
// Enter the name to modify 
void multify_name(char* file, FILE* fp, StuInfo* s, int num)
{
    
	char s1[20] = {
     0 };
	char s2[20] = {
     0 };
	int i = 0;
	int j = 0;
	int k = 0;
	int m = 0;
	printf(" Please enter the name of the student to be modified :>");
	scanf("%s", &s1);
	for (i = 0; i < num; i++)
	{
    
		if (strcmp(s1, s[i].name) == 0)
		{
    
			printf(" The student number of the current student is :>%d  The name is :>%s\n", s[i].id, s[i].name);
			printf(" Please enter the modified name :>");
			scanf("%s", s1);
			strcpy(s[i].name, s1);
			printf(" The student's student number after revision is :>%d  The name is :>%s\n", s[i].id, s[i].name);
			rewind(fp);
			fprintf(fp, "%d\n", num);
			for (j = 0; j < num; j++)
			{
    
				fprintf(fp, "%d %s", s[j].id, s[j].name);
				if (j < num - 1)
				{
    
					fputc('\n', fp);
				}
			}
			printf(" Modification successful !\n");
			break;
		}
	}
	if (i > num)
	{
    
		printf(" The name does not exist !\n");
		printf(" Modification failed !\n");
	}
}

Function seven : Show student information

// Show student information 
void stu_show(char* file, StuInfo* s, int num)
{
    
	FILE* fp = fopen(file, "r+");
	fscanf(fp, "%d\n", &num);
	int i = 0;
	printf(" The student information of this class is :\n");
	printf(" Student number   full name \n");
	for (i = 0; i < num; i++)
	{
    
		printf("%d %s\n", s[i].id, s[i].name);
	}
}

Here, through the loop , Print and display the information of all students .

Function eight : Sort student information

// Sort student information 
void stu_sort(char* file, StuInfo* s, int num)
{
    
	FILE* fp = fopen(file, "r+");
	fscanf(fp, "%d\n", &num);
	int i = 0;
	int input = 0;
	menu_sort();
	printf(" Please select sort by :>");
	scanf("%d", &input);
	switch (input)
	{
    
	case 1:
		qsort(s, num, sizeof(s[0]), cmp_by_id);
		printf(" Sort complete !\n");
		break;
	case 2:
		qsort(s, num, sizeof(s[0]), cmp_by_name);
		printf(" Sort complete !\n");
		break;
	case 0:
		printf(" Unsort !\n");
		break;
	default:
		printf(" Input error ! Please re-enter :>\n");
		break;
	}

	rewind(fp);
	fprintf(fp, "%d\n", num);
	for (i = 0; i < num; i++)
	{
    
		fprintf(fp, "%d %s", s[i].id, s[i].name);
		if (i < num - 1)
		{
    
			fputc('\n', fp);
		}
	}
}
// Sort by student number 
int cmp_by_id(const void* e1, const void* e2)
{
    
	//e1、e2 by void* type , First convert its mandatory type to a structure (StuInfo*) type , Point to id Compare 
	return (((StuInfo*)e1)->id - ((StuInfo*)e2)->id);
}
// Sort by name 
int cmp_by_name(const void* e1, const void* e2)
{
    
	return strcmp(((StuInfo*)e1)->name, ((StuInfo*)e2)->name);
}

The sorting method I use here is qsort function

void qsort
(
void* base,// The first address of the array to be sorted
size_t num,// Number of elements of the array to be sorted
size_t width,// The size of each element of the array to be sorted ( In bytes )
int (* cmp)(const void* e1,const void* e2)// A function pointer , The address of the function used to compare two elements ( This function should be implemented by itself ), The two parameters are the addresses of the two elements to be compared
)

qsort There are four parameters inside the function , Here their meanings have been listed one by one . Note that when implementing the last function , The parameter type must be const void*.

3、 Project presentations

1. Exhibition

( Because the functions are based on the existing class files , therefore , Here I first show the class student information .)
 Exhibition

2. Roll call

 Roll call

3. Add

 increase


4. Delete

 Delete


5. lookup

 Inquire about

6. modify

 modify

7. Sort

 Sort ·1


 Sort 2

4、 Code display

1. byname.h

( Contains the definition of macros 、 The reference of header file, the declaration of function and the creation of structure )
Don't forget to reference this header file in the next two files

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <errno.h>

#define MAX 100

// Create a structure 
typedef struct StuInfo
{
    
	int id;
	char name[MAX];
}StuInfo;

void stu_open(char* file, FILE* fp,StuInfo* s,int num);
void stu_byname(char* file,StuInfo* s,int num);
void stu_add(char* file,StuInfo* s,int num);
void stu_del(char* file,StuInfo* s,int num);
void stu_located(char* file,StuInfo* s,int num);
void stu_show(char* file,StuInfo* s,int num);
void stu_multify(char* file,StuInfo* s,int num);
void stu_sort(char* file,StuInfo* s,int num);
void stu_close(char* file, FILE* fp);

2. byname.c

( Including the implementation of various functional functions )

#define _CRT_SECURE_NO_WARNINGS 1

#include "byname.h"

// Read the contents of the file into the structure 
void stu_open(char* file, FILE* fp,StuInfo* s,int num)
{
    
	int i = 0;
	//stu_write(file, fp, count);
	fscanf(fp, "%d", &num);
	for (i = 0; i < num; i++)
	{
    
		fscanf(fp, "%d %s", &(s[i].id), s[i].name);
	}
	return;
}

// Random roll call 
void stu_byname(char* file,StuInfo* s,int num)
{
    
	FILE* fp = fopen(file, "r+");
	fscanf(fp, "%d", &num);
	//stu_open(file, fp, s, num);
	int i = 0;
	// Get a random number 
	i = rand() % num;
	printf("\n Student number : %d\n full name : %s\n", s[i].id, s[i].name);
}

// Add student information 
void stu_add(char* file, StuInfo* s, int num)
{
    
	FILE* fp = fopen(file, "r+");
	fscanf(fp, "%d", &num);
	int i = 0;
	int j = 0;
	int m = 0;
	char str[20] = {
     0 };
	printf(" Please enter the student number and name of the added student :>");
	scanf("%d %s", &m, &str);

	// Judge whether the student number already exists 
	for (i = 0; i < num; i++)
	{
    
		if (m == s[i].id)
		{
    
			printf(" The student number already exists !\n");
			printf(" Add failure !\n");
			break;
		}
	}

	// If it does not exist , Add to the student 
	if (i == num)
	{
    
		num = num + 1;
		// Reset the file pointer to the beginning of the file , And re write the class number 
		rewind(fp);
		fprintf(fp, "%d", num);
		// Move the file pointer to the end of the file , Write the newly added student 
		fseek(fp, 0, SEEK_END);
		fprintf(fp, "\n%d %s", m, str);
		printf(" Add success \n");
	}
}

// Delete menu 
void menu_del()
{
    
	printf("*********************************\n");
	printf("********* 1. Delete by student number  *********\n");
	printf("********* 2. Delete by name  *********\n");
	printf("********* 0. Cancel deletion  *********\n");
	printf("*********************************\n");
}

// Delete by student number 
void stu_del_id(char* file, FILE* fp,StuInfo* s,int num)
{
    
	int m = 0;
	int i = 0;
	int j = 0;
	int k = 0;
	int flag = 0;
	
	printf(" Please enter the student number to delete :>");
	scanf("%d", &m);
	for (i = 0; i < num; i++)
	{
    
		if (m == s[i].id)
		{
    
			// Confirm whether to delete 
			printf(" The student information is :\n");
			printf(" Student number :>%d  full name :>%s\n", s[i].id, s[i].name);
			printf(" Are you sure you want to delete this student's information ?(0/1):>");
			scanf("%d", &flag);
			switch (flag)
			{
    
			case 1:
				for (j = i; j < num; j++)
				{
    
					s[j] = s[j + 1];
				}
				num--;
				// Reset the file pointer to the beginning of the file , And re write the class number 
				rewind(fp);
				fprintf(fp, "%d\n", num);
				for (k = 0; k < num; k++)
				{
    
					fprintf(fp, "%d %s", s[k].id, s[k].name);
					if (k < num - 1)
					{
    
						fputc('\n', fp);
					}
				}
				printf(" Delete successful !\n");
				break;
			case 0:
				printf(" Successful operation ! Cancel deletion , Go back to the upper menu !\n");
				break;
			default:
				printf(" Input error ! Please re-enter :>\n");
				break;
			}
			break;
		}
	}
	if (i == num)
	{
    
		printf(" The student number does not exist !\n");
		printf(" Delete failed !\n");
	}

}

// Delete by name 
void stu_del_name(char* file, FILE* fp,StuInfo* s,int num)
{
    
	int i = 0;
	int j = 0;
	int k = 0;
	int flag = 0;
	char str[20] = {
     0 };
	//fscanf(fp, "%d\n", &num);
	//stu_open(file, fp, s, num);
	printf(" Please enter the name to delete :>");
	scanf("%s", &str);
	for (i = 0; i < num; i++)
	{
    
		if (strcmp(s[i].name, str) == 0)
		{
    
			// Confirm whether to delete 
			printf(" The student information is :\n");
			printf(" Student number :>%d  full name :>%s\n", s[i].id, s[i].name);
			printf(" Are you sure you want to delete this student's information ?(0/1):>");
			scanf("%d", &flag);
			switch (flag)
			{
    
			case 1:
				for (j = i; j < num; j++)
				{
    
					s[j] = s[j + 1];
				}
				num--;
				// Reset the file pointer to the beginning of the file , And re write the class number 
				rewind(fp);
				fprintf(fp, "%d\n", num);
				for (k = 0; k < num; k++)
				{
    
					fprintf(fp, "%d %s", s[k].id, s[k].name);
					if (k < num - 1)
					{
    
						fputc('\n', fp);
					}
				}
				printf(" Delete successful !\n");
				break;
			case 0:
				printf(" Successful operation ! Cancel deletion , Go back to the upper menu !\n");
				break;
			default:
				printf(" Input error ! Please re-enter :>\n");
				break;
			}
			break;
		}
	}
	if (i == num)
	{
    
		printf(" The name does not exist !\n");
		printf(" Delete failed !\n");
	}
}

// Delete student information 
void stu_del(char* file, StuInfo* s, int num)
{
    
	FILE* fp = fopen(file, "r+");
	fscanf(fp, "%d", &num);
	stu_close(file, fp);
	// Close the file , And start again with "w+" To open the file 
	FILE* ffp = fopen(file, "w+");
	int input = 0;
	int k = 0;
	do
	{
    
		menu_del();
		printf(" Please enter the deletion method :>");
		scanf("%d", &input);
		switch (input)
		{
    
		case 1:
			stu_del_id(file, fp,s,num);
			break;
		case 2:
			stu_del_name(file, fp,s,num);
			break;
	    // Because it is with "w+" The way to open the file , The contents will be emptied , therefore , If you cancel or exit delete , You need to write the contents of the structure into the file 
		case 0:
			fprintf(ffp, "%d\n", num);
			for (k = 0; k < num; k++)
			{
    
				fprintf(ffp, "%d %s", s[k].id, s[k].name);
				if (k < num - 1)
				{
    
					fputc('\n', ffp);
				}
			}
			printf(" Exit delete !\n");
			break;
		default:
			rewind(ffp);
			fprintf(ffp, "%d\n", num);
			for (k = 0; k < num; k++)
			{
    
				fprintf(ffp, "%d %s", s[k].id, s[k].name);
				if (k < num - 1)
				{
    
					fputc('\n', ffp);
				}
			}
			printf(" Input error ! Please re-enter !\n");
			break;
		}
		stu_close(file, ffp);
	} while (input);
}

// Query menu 
void menu_located()
{
    
	printf("--------------------------\n");
	printf("-------1. By student number -------\n");
	printf("-------2. Search by name -------\n");
	printf("-------0. Exit query ---------\n");
	printf("--------------------------\n");
}

// By student number 
void located_id(StuInfo* s, int num)
{
    
	int m = 0;
	int i = 0;
	printf(" Please enter the student number to inquire :>");
	scanf("%d", &m);
	for (i = 0; i < num; i++)
	{
    
		if (m == s[i].id)
		{
    
			// The query is successful , Output the student information 
			printf(" The student information is :\n");
			printf(" Student number :>%d  full name :>%s\n", s[i].id, s[i].name);
			break;
		}
	}
	if (i == num)
	{
    
		printf(" The student number does not exist !\n");
	}
}

// Search by name 
void located_name(StuInfo* s, int num)
{
    

	char s1[MAX];
	int i = 0;
	int j = 0;

	printf(" Please enter the name you want to query :>");
	scanf("%s", &s1);
	for (i = 0; i < num; i++)
	{
    
		if (strcmp(s1, s[i].name) == 0)
		{
    
			printf(" Student number :>%d  full name :>%s\n", s[i].id, s[i].name);
			break;
		}
	}
	if (i == num)
	{
    
		printf(" The name does not exist !\n");
	}
}

// Query information 
void stu_located(char* file, StuInfo* s, int num)
{
    
	FILE* fp = fopen(file, "r+");
	fscanf(fp, "%d", &num);
	int input = 0;
	do
	{
    
		menu_located();
		printf(" Please select the query method :>");
		scanf("%d", &input);
		switch (input)
		{
    
		case 1:
			located_id(s,num);
			break;
		case 2:
			located_name(s,num);
			break;
		case 0:
			break;
		default:
			printf(" Input error ! Please re-enter :>\n");
			break;
		}
	} while (input);
}

// Modify the menu 
void menu_multify()
{
    
	printf("*******************************\n");
	printf("********* 1. Change the student number  *********\n");
	printf("********* 2. Change the name  *********\n");
	printf("********* 0. Cancel and modify  *********\n");
	printf("*******************************\n");
}

// Enter student number to modify 
void multify_id(char* file, FILE* fp, StuInfo* s, int num)
{
    
	//fscanf(fp, "%d", &num);
	char s1[20] = {
     0 };
	int i = 0;
	int j = 0;
	int k = 0;
	int m = 0;
	int n = 0;
	printf(" Please input the student number of the student to be modified :>");
	scanf("%d", &m);
	for (i = 0; i < num; i++)
	{
    
		if (m == s[i].id)
		{
    
			printf(" The student number of the current student is :>%d  The name is :>%s\n", s[i].id, s[i].name);
			printf(" Please enter the modified student number :>");
			scanf("%d", &n);

			// Judge whether the modified student number already exists 
			for (k = 0; k < num; k++)
			{
    
				if (n == s[k].id)
				{
    
					printf(" The student number already exists !\n");
					printf(" The student's name is :>%s\n", s[k].name);
					break;
				}
			}
			if (k == num)
			{
    
				s[i].id = n;
				printf(" The student's student number after revision is :>%d  The name is :>%s\n", s[i].id, s[i].name);
				rewind(fp);
				fprintf(fp, "%d\n", num);
				for (j = 0; j < num; j++)
				{
    
					fprintf(fp, "%d %s", s[j].id, s[j].name);
					if (j < num - 1)
					{
    
						fputc('\n', fp);
					}
				}
				printf(" Modification successful !\n");
				break;
			}
		}
	}
	if (i > num)
	{
    
		printf(" The student number does not exist !\n");
		printf(" Modification failed !\n");
	}
}

// Enter the name to modify 
void multify_name(char* file, FILE* fp, StuInfo* s, int num)
{
    
	//fscanf(fp, "%d", &num);
	char s1[20] = {
     0 };
	char s2[20] = {
     0 };
	int i = 0;
	int j = 0;
	int k = 0;
	int m = 0;
	printf(" Please enter the name of the student to be modified :>");
	scanf("%s", &s1);
	for (i = 0; i < num; i++)
	{
    
		if (strcmp(s1, s[i].name) == 0)
		{
    
			printf(" The student number of the current student is :>%d  The name is :>%s\n", s[i].id, s[i].name);
			printf(" Please enter the modified name :>");
			scanf("%s", s1);
			strcpy(s[i].name, s1);
			printf(" The student's student number after revision is :>%d  The name is :>%s\n", s[i].id, s[i].name);
			rewind(fp);
			fprintf(fp, "%d\n", num);
			for (j = 0; j < num; j++)
			{
    
				fprintf(fp, "%d %s", s[j].id, s[j].name);
				if (j < num - 1)
				{
    
					fputc('\n', fp);
				}
			}
			printf(" Modification successful !\n");
			break;
		}
	}
	if (i > num)
	{
    
		printf(" The name does not exist !\n");
		printf(" Modification failed !\n");
	}
}

// Modify student information 
void stu_multify(char* file, StuInfo* s, int num)
{
    
	FILE* fp = fopen(file, "r+");
	fscanf(fp, "%d", &num);
	int input = 0;
	do
	{
    
		menu_multify();
		printf(" Please select the modification method :>");
		scanf("%d", &input);
	switch (input)
		{
    
		case 1:
			multify_id(file, fp,s,num);
			break;
		case 2:
			multify_name(file, fp,s,num);
			break;
		case 0:
			break;
		default:
			printf(" Input error ! Please re-enter :>\n");
			break;
		}
	} while (input);

}

// Show student information 
void stu_show(char* file, StuInfo* s, int num)
{
    
	FILE* fp = fopen(file, "r+");
	fscanf(fp, "%d\n", &num);
	int i = 0;
	printf(" The student information of this class is :\n");
	printf(" Student number   full name \n");
	for (i = 0; i < num; i++)
	{
    
		printf("%d %s\n", s[i].id, s[i].name);
	}
}

// Sort menu 
void menu_sort()
{
    
	printf("***********************************\n");
	printf("********** 1. Sort by student number  ***********\n");
	printf("********** 2. Sort by name  ***********\n");
	printf("********** 0. Unsort  ***********\n");
	printf("***********************************\n");
}

qsort function 
//void qsort
// (
// void* base,// The first address of the array to be sorted 
// size_t num,// Number of elements of the array to be sorted 
// size_t width,// The size of each element of the array to be sorted ( In bytes )
// int (*cmp)(const void* e1,const void* e2)// A function pointer , The address of the function used to compare two elements ( This function should be implemented by itself ), Inside 
// ) // The two parameters of are the addresses of the two elements to be compared 

// Sort by student number 
int cmp_by_id(const void* e1, const void* e2)
{
    
	//e1、e2 by void* type , First convert its mandatory type to a structure (StuInfo*) type , Point to id Compare 
	return (((StuInfo*)e1)->id - ((StuInfo*)e2)->id);
}

// Sort by name 
int cmp_by_name(const void* e1, const void* e2)
{
    
	return strcmp(((StuInfo*)e1)->name, ((StuInfo*)e2)->name);
}

// Sort student information 
void stu_sort(char* file, StuInfo* s, int num)
{
    
	FILE* fp = fopen(file, "r+");
	fscanf(fp, "%d\n", &num);
	int i = 0;
	int input = 0;
	menu_sort();
	printf(" Please select sort by :>");
	scanf("%d", &input);
	switch (input)
	{
    
	case 1:
		qsort(s, num, sizeof(s[0]), cmp_by_id);
		break;
	case 2:
		qsort(s, num, sizeof(s[0]), cmp_by_name);
		break;
	case 0:
		printf(" Unsort !\n");
		break;
	default:
		printf(" Input error ! Please re-enter :>\n");
		break;
	}

	printf(" Sort complete !\n");
	rewind(fp);
	fprintf(fp, "%d\n", num);
	for (i = 0; i < num; i++)
	{
    
		fprintf(fp, "%d %s", s[i].id, s[i].name);
		if (i < num - 1)
		{
    
			fputc('\n', fp);
		}
	}
}

// Close file 
void stu_close(char* file, FILE* fp)
{
    
	fclose(fp);
	fp = NULL;
}

3.test.c

( Contains the main function , And the codes of the first two documents )

#define _CRT_SECURE_NO_WARNINGS 1
#include "byname.h"

void menu()
{
    
	printf("*****************************************************\n");
	printf("************* 1. Start roll call  2. Add  *************\n");
	printf("************* 3. Delete  4. lookup  *************\n");
	printf("************* 5. modify  6. Show  *************\n");
	printf("************* 7. Sort  0. sign out  *************\n");
	printf("*****************************************************\n");
}

void test()
{
    
	StuInfo s[MAX] = {
     0 };// Student array 
	int num = 0;// Class size 
	char file[32];
	printf(" Please enter the class file to open :>");
	scanf("%s", &file);
	FILE* fp;
	fp = fopen(file, "r+");

	// If opening fails , Then print “Open Failed!”, And show the reason why the opening failed 
	if (fp == NULL)
	{
    
		printf("Open Failed: % s\n", strerror(errno));
		return;
	}
	// File to "r+" Open and write its contents into the structure , Then close the file , Then open the file in the way required by each function 
	stu_open(file, fp, s, num);
	stu_close(file, fp);

	int input = 0;
	do
	{
    
		menu();
		printf(" Please enter your choice :>");
		scanf("%d", &input);
		switch (input)
		{
    
		case 1:
			stu_byname(file,s,num);
			break;
		case 2:
			stu_add(file,s,num);
			break;
		case 3:
			stu_del(file,s,num);
			break;
		case 4:
			stu_located(file,s,num);
			break;
		case 5:
			stu_multify(file,s,num);
			break;
		case 6:
			stu_show(file,s,num);
			break;
		case 7:
			stu_sort(file,s,num);
			break;
		case 0:
			printf(" About to quit ! Welcome to use next time !\n");
			break;
		default:
			printf(" Input error ! Please re-enter :>\n");
			break;
		}
		stu_close(file, fp);
	} while (input);
}

int main()
{
    
	srand((unsigned int)time(NULL));
	test();
	return 0;
}

5、 summary

Complete a complete project independently for the first time , At the beginning, it was very difficult , I even think I can't finish , But in “ Force ” Slowly groping , Finally, I succeeded .
Today, I will show it in the way of blog , If there is anything wrong , Please don't hesitate to give me some advice , I will listen carefully and correct it in time !!!
 Insert picture description here

原网站

版权声明
本文为[Namely ang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230816019636.html