C language construction project-student management system (non-linked list)

Table of contents

Build offer.h file

Build the main function in offer.c

Password login system

Build the interface function in my_oferr.c 

Use help menu interface functions

Add student information interface function 

Query student information interface function

Delete student information interface function 

Save student information interface 

open file fopen

close file fclose

 Determine whether to save the file fwrite

Exit the executable file exit

Display student information interface function

 Modify student information interface function

 Student ranking information interface function

--------------------------------------

---------------- 

Every time we return, it is for a better start;

Every pause is to accumulate strength.

Has everyone in C language entered the practical training stage recently?

Are you still worried about not being able to write code?

Next, I will lead you to complete it together!

A simple project in C language-student management system!

Mainly uses arrays and pointer functions

The construction of the linked list will be updated later.

Title: Student Management System
Student data consists of student ID, name, gender, age, and grades in three courses (C language, advanced mathematics, and major physics)

 Implementation functions include:
(1) Password login
(2) Usage tips
(3) Information Enter
(4) Query information
(5) Delete information
(6) Save information
(7) Display information

(8) Modify information

(9) Score ranking

For this kind of code that exceeds hundreds of digits, it is best for everyone to develop the habit ofwriting code in modules:

I created one .h file and two .c files, .h usually contains header files and structure types, The a>.c file sets up the main function main, and completes the corresponding function interface. This way the division of labor is clear, there will be no confusion, and it can improve the efficiency of coding~

First understand the project functions we want to implement:Log in to the system with password, use the help menu, add student information, query student information, delete student information, save current information, display current information , modify student information and student performance ranking


Build offer.h file

Because students have the following basic information: name, age, student ID, and gender, which are represented by name, age, id, and sex in turn, it can be implemented through a structure. The struct statement defines the storage of different types of data items and defines a structure. Body array Stu, used to store information about each student

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void TiShi();      //提示功能
void StuXinXi();   //增加信息
void ChaZhao();    //查找信息
void ShanGai();    //删改信息
void BaoCun();     //保存信息
void XianShi();    //显示信息
void XiuGai();     //修改信息
void PaiMing();    //学生排名
#define N 20
#define Number sizeof(struct Student)//求结构体的大小    
//创建结构体包含学生信息
struct Student
{
	char Stuid[N];     //学号
	char Stuname[N];   //名字
	int Stuage;        //年龄
	char Stusex[N];    //性别
	double score[3];   //成绩
}Stu[Number];
//枚举体美化选项
enum menu2
{
	Quit = 0,
	Resume = 1
};
enum ChaZhao
{
	CaoZuo1 = 1,
	CaoZuo2 = 2,
	CaoZuo3 = 3
};

1. It is best to define a macro here#defineotherwise it will be very troublesome to modify it in the future

2. The enumeration beautification option was mentioned in my previous blog to improve the readability of the code.

3. The rest is the introduction of header files, declaration of functions and creation of structures.

Build the main function in offer.c

 To use the header file we set in offer.h, add #include"offer.h" to offer.c

#include"offer.h"

 The overall framework is as follows:

#include"offer.h"
int sum = 1;
int main()
{
	int i = 0;
	int input = 0, count = 0;
	char mima[20] = "123";//登入的密码
	char shuru[20];
	system("color F4");
	printf("\t\t\t     **************************************\n");
	printf("\t\t\t     |       *欢迎使用学生管理系统*       |\n");
	printf("\t\t\t     |           *管理员: 小唐*           |\n");
	printf("\t\t\t      ------------------------------------\n");
	printf("请输入管理员密码:\n");
	while ((count = _getch()) != '\r')   
	{
		if (count == '\b')
		{
			i--;
			printf("\b \b");
		}
		else
		{
			shuru[i++] = count;
			printf("*");
		}
	}
	shuru[i++] = '\0';    //getch赋值的是单个字符,所以为了比较两个字符串的大小要给shuru数组后面添加'\0'
	if (strcmp(mima, shuru) == 0)
	{
		printf("\n密码正确,您已进入系统!\n");
	}
	else
	{
		printf("\n密码错误,请重新输入!\n");
		return 0;
	}
    system("pause");
	system("cls");
	do
	{
		menu2();
		printf("请选择继续还是结束操作:\n");
		scanf("%d", &sum);
		switch (sum)
		{
		case Quit:
			system("cls");//清除屏幕
			printf("您已退出学生管理系统!\n");
			break;
		case Resume:
			menu1();
			void(*p[9])() = { NULL,TiShi,StuXinXi,ChaZhao,ShanGai,BaoCun,XianShi,XiuGai,PaiMing };//指针数组函数
			printf("请输入你要进行的操作\n");
			scanf("%d", &input);
			system("cls");
			(*p[input])();
			break;
		default:
			printf("输入错误,重新输入!\n");
			break;
		}
	} while (sum);
	return 0;
}

Next, I will lead you to interpret the above code

Password login system

Before entering the student management system, I set up asmall system password login interface to determine whether to enter or exit

	int count = 0;
	char mima[20] = "123";//登入的密码
	char shuru[20];
	system("color F4");
	printf("\t\t\t     **************************************\n");
	printf("\t\t\t     |       *欢迎使用学生管理系统*       |\n");
	printf("\t\t\t     |           *管理员: 小唐*           |\n");
	printf("\t\t\t      ------------------------------------\n");
	printf("请输入管理员密码:\n");
	while ((count = _getch()) != '\r')   
	{
		if (count == '\b')
		{
			i--;
			printf("\b \b");
		}
		else
		{
			shuru[i++] = count;
			printf("*");
		}
	}
	shuru[i++] = '\0';    //getch赋值的是单个字符,所以为了比较两个字符串的大小要给shuru数组后面添加'\0'
	if (strcmp(mima, shuru) == 0)
	{
		printf("\n密码正确,您已进入系统!\n");
	}
	else
	{
		printf("\n密码错误,请重新输入!\n");
		return 0;
	}

1. We use while to perform getch input, getch input of a single character is not allowed The message will be displayed, but we press the Enter key to end the input and the loop will stop

2.' \r ' means the Enter key, and ' \b ' means the Delete key< /span>

3. Enter one character each time through the loopcount is assigned to the array shuru, and print " * "

4.getch assigns a single character, so in order to compare the sizes of two strings, you need to add ' \0 ' after the shuru array. a>

5.strcmp compares the size of two strings. For details, please see my previous blog

The above is the entire content of our password login system

Next I will explain the next code implementation

 The function of menu2() is to choose to continue the operation or exit the system

void menu2()
{
	system("color F4");
	printf("\t\t\t*********************\n");
	printf("\t\t\t*********************\n");
	printf("\t\t\t-------1.Resume------\n");
	printf("\t\t\t-------0.Quit--------\n");
	printf("\t\t\t*********************\n");
}

1.system("cls")-clear screen function, which will clear the displayed content and is included by #include<stdlib.h>

2.system("color F4"); is a function that changes the background color and is #include<stdlib>

3. We use enumeration types to beautify the options Quit is 0 and Resume is 1

4. We use a pointer array function to directly call the interface function, because the function type without a return value is void, which reduces the writing of repeated code. If you are not comfortable with it, you can also use switch.


Build the interface function in my_oferr.c 

First define the global variables in our my_oferr.c

#include"offer.h"
int m = 0;              //记录录入学生的人数

Because it displays a management system

So we design a main menu

This function is implemented by defining a function menu1():

void menu1()
{
	printf("\n");
	system("color F4");
	printf("\t\t\t*****************************************************\n");
	printf("\t\t\t*---------------------------------------------------*\n");
	printf("\t\t\t*                   学生管理系统                    *\n");
	printf("\t\t\t*****************************************************\n");
	printf("\t\t\t********************系统功能菜单*********************\n");
	printf("\t\t\t----------------------     --------------------------\n");
	printf("\t\t\t*****************************************************\n");
	printf("\t\t\t**    1、使用帮助菜单   *     2、增加学生信息      **\n");
	printf("\t\t\t*****************************************************\n");
	printf("\t\t\t**    3、查询学生信息   *     4、删除学生信息      **\n");
	printf("\t\t\t*****************************************************\n");
	printf("\t\t\t**    5、保存当前信息   *     6、显示当前信息      **\n");
	printf("\t\t\t*****************************************************\n");
	printf("\t\t\t**    7、修改学生信息   *     8、学生成绩排名      **\n");
	printf("\t\t\t*****************************************************\n");
	printf("\t\t\t*****************************************************\n");
	printf("\t\t\t----------------------     --------------------------\n");
}

You can set this up as you like~ 


 Use the help menu interface function

void TiShi()
{
	printf("欢迎来到学生管理系统:\n");
	printf("在这个管理系统中,教师和学生都不需要去了解自己的信息,只需通过网络就能实现自己的信息管理。\n");
	printf("教师可以轻松便捷地掌握学生相关情况、姓名、学号以及考试成绩等基本信息。\n");
	printf("它还提供一个搜索功能和设置权限,可以将学生的信息进行搜索,\n也可根据关键字或其他方式对其进行定位,并且还能显示出该人的相关基本资料。\n");
	printf("如果由技术上的缺陷还请联系项目负责人:\n17817473648\n");
	system("pause");
	system("cls");
}

 system("pause") is a pause function, press any key to restart, and is #include<stdlib.h>

This function interface mainly serves as a reminder. Friends can add their own management requirements. 


 Add student information interface function 

void StuXinXi()
{
	int i = m,flag,n = 0;
	printf("请输入你要添加的学生人数:\n");
	scanf("%d", &n);
	printf("--------------------------*学生信息录入系统*--------------------------------\n");
	if (n > 0)
	{
		do {
			flag = 1;
			while (flag)
			{
				flag = 0;
				printf("请输入第%d位学生的学号:\n", i + 1);
				scanf("%s", Stu[i].Stuid);
				for (int j = 0; j < i; j++)
				{
					if (strcmp(Stu[i].Stuid, Stu[j].Stuid) == 0)
						//判断是否重复
					{
						printf("该学生已经录入,请重新选择!\n");
						flag = 1;
						break;
					}
				}
			}
			printf("请输入第%d学生的姓名:\n", i + 1);
			scanf("%s", Stu[i].Stuname);
			printf("请输入第%d学生的年龄:\n", i + 1);
			scanf("%d", &Stu[i].Stuage);
			printf("请输入第%d学生的性别:\n", i + 1);
			scanf("%s", &Stu[i].Stusex);
			printf("请输入第%d学生的C语言成绩:\n", i + 1);
			scanf("%lf", &Stu[i].score[0]);
			printf("请输入第%d学生的高数成绩:\n", i + 1);
			scanf("%lf", &Stu[i].score[1]);
			printf("请输入第%d学生的大物成绩:\n", i + 1);
			scanf("%lf", &Stu[i].score[2]);
			i++;
		} while (i < n + m);
		m += n;
	if (flag == 0)
	{
		printf("添加完成!请进行下一步操作:\n");
	}
		system("pause");
		system("cls");
	}
}

1.flag is used to determine the last output prompt statement and indicate whether the input is successful or not.

2. Then the whe loop assigns initial values ​​to the structure members.

3. Just add the number of people to the global variable m.


Query student information interface function

void ChaZhao()
{
	int flag, input;
	char Stu1[Number];
	printf("\t\t\t----------*学生信息查找系统*----------\n");
	printf("\t\t\t**************************************\n");
	printf("\t\t\t******1.请输入你要查找的学生名字******\n");
	printf("\t\t\t******2.请输入你要查找的学生学号******\n");
	printf("\t\t\t******------3.退出本次操作------******\n");
	printf("\t\t\t**************************************\n");
	while (1)
	{
		flag = 0;
		printf("请输入你要进行的查询操作:\n");
		scanf("%d", &input);
		switch (input)
		{
		case CaoZuo1:
			printf("请输入你要查找的学生名字:\n");
			scanf("%s", Stu1);
			for (int i = 0; i < m; i++)
			{
				if (strcmp(Stu[i].Stuname, Stu1) == 0)
				{
					flag = 1;
					printf("学号:\t\t名字:\t年龄:\t性别:\tC语言成绩:\t高数成绩:\t大物成绩:\n");
					printf("%s\t%s\t%d\t%s\t%.2lf\t\t%.2lf\t\t%.2lf\n", Stu[i].Stuid, Stu[i].Stuname, Stu[i].Stuage, Stu[i].Stusex, Stu[i].score[0], Stu[i].score[1], Stu[i].score[2]);
				}
			}break;
		case CaoZuo2:
			printf("请输入你要查找的学生学号:\n");
			scanf("%s", Stu1);
			for (int i = 0; i < m; i++)
			{
				if (strcmp(Stu[i].Stuid, Stu1) == 0)
				{
					flag = 1;
					printf("学号:\t\t名字:\t年龄:\t性别:\tC语言成绩:\t高数成绩:\t大物成绩:\n");
					printf("%s\t%s\t%d\t%s\t%.2lf\t\t%.2lf\t\t%.2lf\n", Stu[i].Stuid, Stu[i].Stuname, Stu[i].Stuage, Stu[i].Stusex, Stu[i].score[0], Stu[i].score[1], Stu[i].score[2]);
				}
			}break;
		case CaoZuo3:
			return;
		default:
			printf("输入错误,重新选择!\n");
		}
		if (flag == 0)
		{
			printf("该学生没有录入系统,请重新查找!\n");
		}
	}
	system("pause");
}


Delete student information interface function 

void ShanGai()
{
	int flag = 0;
	char arr[Number];  //定义一个数组
	printf("----------------------------------*学生名单*----------------------------------------------------------\n");
	printf("学号:\t\t名字:\t年龄:\t性别:\tC语言成绩:\t高数成绩:\t大物成绩:\n");
	for (int i = 0; i < m; i++)
	{
		printf("%s\t%s\t%d\t%s\t%.2lf\t\t%.2lf\t\t%.2lf\n", Stu[i].Stuid, Stu[i].Stuname, Stu[i].Stuage, Stu[i].Stusex, Stu[i].score[0], Stu[i].score[1], Stu[i].score[2]);
	}
	printf("请输入你要删除学生的学号:\n");
	scanf("%s", arr);
	for (int i = 0; i < m; i++)
	{
		if (strcmp(Stu[i].Stuid, arr) == 0) //查找对应的学号
		{
			flag = 1;
			for (int j = i; j < m - 1; j++) //m-1防止越界访问
			{
				Stu[j] = Stu[j + 1];      //整个结构体后一个覆盖前一个
			}
		}
	}
	if (flag == 0)
	{
		printf("无法查找该学生信息!\n");
	}
	else
	{
		printf("删除成功,请查询系统!\n");
		m--;           //删除成功总人数减一
		printf("----------------------------------*学生名单*----------------------------------------------------------\n");
		printf("学号:\t\t名字:\t年龄:\t性别:\tC语言成绩:\t高数成绩:\t大物成绩:\n");
		for (int i = 0; i < m; i++)
		{
			printf("%s\t%s\t%d\t%s\t%.2lf\t\t%.2lf\t\t%.2lf\n", Stu[i].Stuid, Stu[i].Stuname, Stu[i].Stuage, Stu[i].Stusex, Stu[i].score[0], Stu[i].score[1], Stu[i].score[2]);
		}
	}
	system("pause");
}

Save student information interface 

Here is the function content of the file saved: 

Opening sentencefopen

You can use the fopen( )  function to create a new file or open an existing file. This call will initialize the type  that contains all the necessary information to control the flow Information. Here is the prototype of this function call:FILE of type FILE

FILE *fopen( const char *filename, const char *mode );

Here, filename is a string used to name the file, access mode mode can be one of the following values:

关闭文品fclose

To close the file, use the fclose( ) function. The prototype of the function is as follows:

 int fclose( FILE *fp );

If the file is closed successfully, fclose( ) The function returns zero. If an error occurs when closing the file, the function returns EOF. This function actually clears the data in the buffer, closes the file, and frees all memory used for the file. EOF is a definition in the header file #include<stdio.h>  Constant in .

 Determine whether to save the file fwrite

fwrite(file,string,length)
file Required. Specifies the open file to be written to.
string Required. Specifies the string to be written to the open file.
length Optional. Specifies the maximum number of bytes to be written.

The fwrite() function writes the content to an open file in binary Output in the form

The function will run when reaches the specified length or reaches the end of file (EOF) (whichever comes first), a>Stop running.

If the function executes successfully, returns the number of bytes written. On failure, returns FALSE.

Exit line itemexit

void exit(int status) 

Terminate the calling process immediately. Any open file descriptors belonging tothis process will be closed, and the child processes of this process will be inherited by process 1, initialized, and sent to the parent process. A SIGCHLD signal. 

void BaoCun()
{
	FILE* fp;              //文件指针
	char filename[Number];
	printf("------------------------------\n");
	printf("---*请输入你要保存的文件名*---\n");
	printf("------------------------------\n");
	scanf("%s", filename);
	if (fp = fopen("filename.txt", "wb")== NULL)//fopen以.txt的形式打开文件函数
	{
		printf("打开文件失败!\n");
		exit(0);
	}
	for (int i = 0; i < m; i++)
	{
		if (fwrite(&Stu[i], sizeof(struct Student), 1, fp) != 1)
		{
			printf("保存失败!\n");
		}
		else
		{
			printf("保存成功!\n");    //返回字节数,则成功保存
		}
	}
	fclose(fp);   //关闭文件
    fp = NULL;    //防止fp变为野指针
	system("pause");
}

After we save it, view the saved file here: 

Display student information interface function

void XianShi()
{
	if (m == 0)
	{
		printf("您好!现在还暂未录入学生信息,请稍后重试");
	}
	else 
	{
		printf("现在有%d名学生:\n", m);
		printf("-------------------------------------*成绩显示*----------------------------------------------\n");
		printf("学号:\t\t名字:\t年龄:\t性别:\tC语言成绩:\t高数成绩:\t大物成绩:\n");
		for (int i = 0; i < m; i++)
		{
			printf("%s\t%s\t%d\t%s\t%.2lf\t\t%.2lf\t\t%.2lf\n", Stu[i].Stuid, Stu[i].Stuname, Stu[i].Stuage, Stu[i].Stusex, Stu[i].score[0], Stu[i].score[1], Stu[i].score[2]);
		}
	}
	system("pause");
}

 

 Modify student information interface function

void XiuGai()
{
	int input = 0;
	int  flag;
	char id[Number], name[Number], sex[Number];
	int age;
	double score;
	printf("请输入要修改的学生学号:\n");
	scanf("%s", &id);
	while (1)
	{
		flag = 0;
		for (int i = 0; i < m; i++)
		{
			if (strcmp(id, Stu[i].Stuid) == 0)
			{
				flag = 1;
				printf("学号:\t\t名字:\t年龄:\t性别:\tC语言成绩:\t高数成绩:\t大物成绩:\n");
				printf("%s\t%s\t%d\t%s\t%.2lf\t\t%.2lf\t\t%.2lf\n", Stu[i].Stuid, Stu[i].Stuname, Stu[i].Stuage, Stu[i].Stusex, Stu[i].score[0], Stu[i].score[1], Stu[i].score[2]);
				printf("**************************************************\n");
				printf("----1.修改学生性别             2.修改学生姓名-----\n");
				printf("----3.修改学生年龄             4.修改C语言成绩----\n");
				printf("----5.修改高数成绩             6.修改大物成绩-----\n");
				printf("********************7.退出本菜单******************\n");
				printf("请输入你要修改的选项:\n");
				scanf("%d", &input);
				switch (input)
				{
				case 1:
					printf("请输入要修改的性别:\n");
					scanf("%s", sex);
					strcpy(Stu[i].Stusex, sex);
					break;
				case 2:
					printf("请输入要修改的姓名:\n");
					scanf("%s",name);
					strcpy(Stu[i].Stuname, name);
					break;
				case 3:
					printf("请输入要修改的年龄:\n");
					scanf("%d", &age);
					Stu[i].Stuage = age;
					break;
				case 4:
					printf("请输入要修改的C语言成绩:\n");
					scanf("%lf", &score);
					Stu[i].score[0] = score;
					break;
				case 5:
					printf("请输入要修改的高数成绩:\n");
					scanf("%lf", &score);
					Stu[i].score[1] = score;
					break;
				case 6:
					printf("请输入要修改的大物成绩:\n");
					scanf("%lf", &score);
					Stu[i].score[2] = score;
					break;
				case 7:
					return;
				default:
					printf("选择错误,请重新选择!\n");
					break;
				}
			}
			if (1 <= input && input <= 6)
			{
				printf("恭喜你修改成功!\n");
				break;
			}
			
			if (flag == 0)
			{
				printf("没有找到该学生信息,请重新输入学生学号:\n");
				gets(name);
			}
		}
	}
	system("pause");
}

char* strcpy(char * destination, const char * source ); 

strcpy: copy function, copies the value on the right side of the brackets to the left side. For details, please see my previous blog

 Student ranking information interface function

int PaiXu(const void* e1, const void* e2)
{
	return  ((struct Student*)e2)->score[0] - ((struct Student*)e1)->score[0];
}
int PaiXu1(const void* e1, const void* e2)
{
	return  ((struct Student*)e2)->score[0] - ((struct Student*)e1)->score[0];
}
int PaiXu2(const void* e1, const void* e2)
{
	return  ((struct Student*)e2)->score[2] - ((struct Student*)e1)->score[2];
}
void PaiMing()
{
	int input = 0;
	while (1)
	{
		printf("\t\t**************************************************\n");
		printf("\t\t---------------------***光荣榜***-----------------\n");
		printf("\t\t**************************************************\n");
		printf("\t\t********1.C语言-------------------2.高数**********\n");
		printf("\t\t********3.大物--------------------4.退出系统******\n");
		printf("\t\t**************************************************\n");
		printf("请输入你要查询的排名:\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("\t学号:\t\t名字:\tC语言成绩:\n");
			int rs = sizeof(Stu) / sizeof(Stu[0]);
			qsort(Stu, rs, sizeof(Stu[0]), PaiXu);//排序
			for (int i = 0; i < m; i++)
			{
				printf("第%d名\t%s\t%s\t%.2lf", i+1, Stu[i].Stuid, Stu[i].Stuname, Stu[i].score[0]);
				printf("\n");
			}
			break;
		case 2:
			printf("\t学号:\t\t名字:\t高数成绩:\n");
			int rs1 = sizeof(Stu) / sizeof(Stu[0]);
			qsort(Stu, rs1, sizeof(Stu[0]), PaiXu1);
			for (int i = 0; i < m; i++)
			{
				printf("第%d名\t%s\t%s\t%.2lf", i+1, Stu[i].Stuid, Stu[i].Stuname, Stu[i].score[0]);
				printf("\n");
			}
			break;
		case 3:
			printf("\t学号:\t\t名字:\t大物成绩:\n");
			int rs2 = sizeof(Stu) / sizeof(Stu[0]);
			qsort(Stu, rs, sizeof(Stu[0]), PaiXu2);
			for (int i = 0; i < m; i++)
			{
				printf("第%d名\t%s\t%s\t%.2lf", i+1, Stu[i].Stuid, Stu[i].Stuname, Stu[i].score[0]);
				printf("\n");
			}
			break;
		case 4:
			return;
		default:
			printf("选择错误,重新选择!\n");
			break;
		}
	}
}

We use qsort sorting method:

void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*));

Base first element address - array name

num number of elements

The size of the size data type

int (*compar)(const void*,const void*)) comparison function pointer 

The following is the implementation of the entire code:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 20      
#define Number sizeof(struct Student)
struct Student
{
	char Stuid[N];    
	char Stuname[N];  
	int Stuage;        
	char Stusex[N];       
	double score[3];  
}Stu[Number];
enum menu2
{
	Quit = 0,
	Resume = 1
};
enum ChaZhao
{
	CaoZuo1 = 1,
	CaoZuo2 = 2,
	CaoZuo3 = 3
};

int m = 0;
void menu1()
{
	printf("\n");
	system("color F4");
	printf("\t\t\t*****************************************************\n");
	printf("\t\t\t*---------------------------------------------------*\n");
	printf("\t\t\t*                   学生管理系统                    *\n");
	printf("\t\t\t*****************************************************\n");
	printf("\t\t\t********************系统功能菜单*********************\n");
	printf("\t\t\t----------------------     --------------------------\n");
	printf("\t\t\t*****************************************************\n");
	printf("\t\t\t**    1、使用帮助菜单   *     2、增加学生信息      **\n");
	printf("\t\t\t*****************************************************\n");
	printf("\t\t\t**    3、查询学生信息   *     4、删除学生信息      **\n");
	printf("\t\t\t*****************************************************\n");
	printf("\t\t\t**    5、保存当前信息   *     6、显示当前信息      **\n");
	printf("\t\t\t*****************************************************\n");
	printf("\t\t\t**    7、修改学生信息   *     8、学生成绩排名      **\n");
	printf("\t\t\t*****************************************************\n");
	printf("\t\t\t*****************************************************\n");
	printf("\t\t\t----------------------     --------------------------\n");
}
void menu2()
{
	system("color F4");
	printf("*********************\n");
	printf("*********************\n");
	printf("-------1.Resume------\n");
	printf("-------0.Quit--------\n");
	printf("*********************\n");
}
void TiShi()
{
	printf("欢迎来到学生管理系统:\n");
	printf("在这个管理系统中,教师和学生都不需要去了解自己的信息,只需通过网络就能实现自己的信息管理。\n");
	printf("教师可以轻松便捷地掌握学生相关情况、姓名、学号以及考试成绩等基本信息。\n");
	printf("它还提供一个搜索功能和设置权限,可以将学生的信息进行搜索,\n也可根据关键字或其他方式对其进行定位,并且还能显示出该人的相关基本资料。\n");
	printf("如果由技术上的缺陷还请联系项目负责人:\n唐工:[email protected]\n");
	system("pause");
	system("cls");
}
void StuXinXi()
{
	int i = m,flag,n = 0;
	printf("请输入你要添加的学生人数:\n");
	scanf("%d", &n);
	printf("--------------------------*学生信息录入系统*--------------------------------\n");
	if (n > 0)
	{
		do {
			flag = 1;
			while (flag)
			{
				flag = 0;
				printf("请输入第%d位学生的学号:\n", i + 1);
				scanf("%s", Stu[i].Stuid);
				for (int j = 0; j < i; j++)
				{
					if (strcmp(Stu[i].Stuid, Stu[j].Stuid) == 0)
					{
						printf("该学生已经录入,请重新选择!\n");
						flag = 1;
						break;
					}
				}
			}
			printf("请输入第%d学生的姓名:\n", i + 1);
			scanf("%s", Stu[i].Stuname);
			printf("请输入第%d学生的年龄:\n", i + 1);
			scanf("%d", &Stu[i].Stuage);
			printf("请输入第%d学生的性别:\n", i + 1);
			scanf("%s", &Stu[i].Stusex);
			printf("请输入第%d学生的C语言成绩:\n", i + 1);
			scanf("%lf", &Stu[i].score[0]);
			printf("请输入第%d学生的高数成绩:\n", i + 1);
			scanf("%lf", &Stu[i].score[1]);
			printf("请输入第%d学生的大物成绩:\n", i + 1);
			scanf("%lf", &Stu[i].score[2]);
			i++;
		} while (i < n + m);
		m += n;
		if (flag == 0)
		{
			printf("添加完成!请进行下一步操作:\n");
		}
		system("pause");
		system("cls");
	}
}
void ChaZhao()
{
		int flag, input;
		char Stu1[Number];
		printf("\t\t\t----------*学生信息查找系统*----------\n");
		printf("\t\t\t**************************************\n");
		printf("\t\t\t******1.请输入你要查找的学生名字******\n");
		printf("\t\t\t******2.请输入你要查找的学生学号******\n");
		printf("\t\t\t******------3.退出本次操作------******\n");
		printf("\t\t\t**************************************\n");
		while (1)
		{
			flag = 0;
			printf("请输入你要进行的查询操作:\n");
			scanf("%d", &input);
			switch (input)
			{
			case CaoZuo1:
				printf("请输入你要查找的学生名字:\n");
				scanf("%s", Stu1);
				for (int i = 0; i < m; i++)
				{
					if (strcmp(Stu[i].Stuname, Stu1) == 0)
					{
						flag = 1;
						printf("学号:\t\t名字:\t年龄:\t性别:\tC语言成绩:\t高数成绩:\t大物成绩:\n");
						printf("%s\t%s\t%d\t%s\t%.2lf\t\t%.2lf\t\t%.2lf\n", Stu[i].Stuid, Stu[i].Stuname, Stu[i].Stuage, Stu[i].Stusex, Stu[i].score[0], Stu[i].score[1], Stu[i].score[2]);
					}
				}break;
			case CaoZuo2:
				printf("请输入你要查找的学生学号:\n");
				scanf("%s", Stu1);
				for (int i = 0; i < m; i++)
				{
					if (strcmp(Stu[i].Stuid, Stu1) == 0)
					{
						flag = 1;
						printf("学号:\t\t名字:\t年龄:\t性别:\tC语言成绩:\t高数成绩:\t大物成绩:\n");
						printf("%s\t%s\t%d\t%s\t%.2lf\t\t%.2lf\t\t%.2lf\n", Stu[i].Stuid, Stu[i].Stuname, Stu[i].Stuage, Stu[i].Stusex, Stu[i].score[0], Stu[i].score[1], Stu[i].score[2]);
					}
				}break;
			case CaoZuo3:
				return;
			default:
				printf("输入错误,重新选择!\n");
			}
			if (flag == 0)
			{
				printf("该学生没有录入系统,请重新查找!\n");
			}
		}
		system("pause");
	}   
void ShanGai()
{
	int flag = 0;
	char arr[Number];
	printf("----------------------------------*学生名单*----------------------------------------------------------\n");
	printf("学号:\t\t名字:\t年龄:\t性别:\tC语言成绩:\t高数成绩:\t大物成绩:\n");
	for (int i = 0; i < m; i++)
	{
		printf("%s\t%s\t%d\t%s\t%.2lf\t\t%.2lf\t\t%.2lf\n", Stu[i].Stuid, Stu[i].Stuname, Stu[i].Stuage, Stu[i].Stusex, Stu[i].score[0], Stu[i].score[1], Stu[i].score[2]);
	}
	printf("请输入你要删除学生的学号:\n");
	scanf("%s", arr);
	for (int i = 0; i < m; i++)
	{
		if (strcmp(Stu[i].Stuid, arr) == 0) 
		{
			flag = 1;
			for (int j = i; j < m - 1; j++) 
			{
				Stu[j] = Stu[j + 1];      
			}
		}
	}
	if (flag == 0)
	{
		printf("无法查找该学生信息!\n");
	}
	else
	{
		printf("删除成功,请查询系统!\n");
		m--;          
		printf("----------------------------------*学生名单*----------------------------------------------------------\n");
		printf("学号:\t\t名字:\t年龄:\t性别:\tC语言成绩:\t高数成绩:\t大物成绩:\n");
		for (int i = 0; i < m; i++)
		{
			printf("%s\t%s\t%d\t%s\t%.2lf\t\t%.2lf\t\t%.2lf\n", Stu[i].Stuid, Stu[i].Stuname, Stu[i].Stuage, Stu[i].Stusex, Stu[i].score[0], Stu[i].score[1], Stu[i].score[2]);
		}
	}
	system("pause");
}
void BaoCun()
{
	FILE* fp;              
	char filename[Number];
	printf("------------------------------\n");
	printf("---*请输入你要保存的文件名*---\n");
	printf("------------------------------\n");
	scanf("%s", filename);
	if (fp = fopen("filename.txt", "wb")== NULL)
	{
		printf("打开文件失败!\n");
		exit(0);
	}
	for (int i = 0; i < m; i++)
	{
		if (fwrite(&Stu[i], sizeof(struct Student), 1, fp) != 1)
		{
			printf("保存失败!\n");
		}
		else
		{
			printf("保存成功!\n");
		}
	}
	fclose(fp);
    fp = NULL;  
	system("pause");
}
void XianShi()
{
	if (m == 0)
	{
		printf("您好!现在还暂未录入学生信息,请稍后重试");
	}
	else 
	{
		printf("现在有%d名学生:\n", m);
		printf("-------------------------------------*成绩显示*----------------------------------------------\n");
		printf("学号:\t\t名字:\t年龄:\t性别:\tC语言成绩:\t高数成绩:\t大物成绩:\n");
		for (int i = 0; i < m; i++)
		{
			printf("%s\t%s\t%d\t%s\t%.2lf\t\t%.2lf\t\t%.2lf\n", Stu[i].Stuid, Stu[i].Stuname, Stu[i].Stuage, Stu[i].Stusex, Stu[i].score[0], Stu[i].score[1], Stu[i].score[2]);
		}
	}
	system("pause");
}
void XiuGai()
{
	int input = 0;
	int  flag;
	char id[Number], name[Number], sex[Number];
	int age;
	double score;
	printf("请输入要修改的学生学号:\n");
	scanf("%s", &id);
	while (1)
	{
		flag = 0;
		for (int i = 0; i < m; i++)
		{
			if (strcmp(id, Stu[i].Stuid) == 0)
			{
				flag = 1;
				printf("学号:\t\t名字:\t年龄:\t性别:\tC语言成绩:\t高数成绩:\t大物成绩:\n");
				printf("%s\t%s\t%d\t%s\t%.2lf\t\t%.2lf\t\t%.2lf\n", Stu[i].Stuid, Stu[i].Stuname, Stu[i].Stuage, Stu[i].Stusex, Stu[i].score[0], Stu[i].score[1], Stu[i].score[2]);
				printf("**************************************************\n");
				printf("----1.修改学生性别             2.修改学生姓名-----\n");
				printf("----3.修改学生年龄             4.修改C语言成绩----\n");
				printf("----5.修改高数成绩             6.修改大物成绩-----\n");
				printf("********************7.退出本菜单******************\n");
				printf("请输入你要修改的选项:\n");
				scanf("%d", &input);
				switch (input)
				{
				case 1:
					printf("请输入要修改的性别:\n");
					scanf("%s", sex);
					strcpy(Stu[i].Stusex, sex);
					break;
				case 2:
					printf("请输入要修改的姓名:\n");
					scanf("%s",name);
					strcpy(Stu[i].Stuname, name);
					break;
				case 3:
					printf("请输入要修改的年龄:\n");
					scanf("%d", &age);
					Stu[i].Stuage = age;
					break;
				case 4:
					printf("请输入要修改的C语言成绩:\n");
					scanf("%lf", &score);
					Stu[i].score[0] = score;
					break;
				case 5:
					printf("请输入要修改的高数成绩:\n");
					scanf("%lf", &score);
					Stu[i].score[1] = score;
					break;
				case 6:
					printf("请输入要修改的大物成绩:\n");
					scanf("%lf", &score);
					Stu[i].score[2] = score;
					break;
				case 7:
					return;
				default:
					printf("选择错误,请重新选择!\n");
					break;
				}
			}
			if (1 <= input && input <= 6)
			{
				printf("恭喜你修改成功!\n");
				break;
			}
			
			if (flag == 0)
			{
				printf("没有找到该学生信息,请重新输入学生学号:\n");
				gets(name);
			}
		}
	}
	system("pause");
}

int PaiXu(const void* e1, const void* e2)
{
	return  ((struct Student*)e2)->score[0] - ((struct Student*)e1)->score[0];
}
int PaiXu1(const void* e1, const void* e2)
{
	return  ((struct Student*)e2)->score[0] - ((struct Student*)e1)->score[0];
}
int PaiXu2(const void* e1, const void* e2)
{
	return  ((struct Student*)e2)->score[2] - ((struct Student*)e1)->score[2];
}
void PaiMing()
{
	int input = 0;
	while (1)
	{
		printf("\t\t**************************************************\n");
		printf("\t\t---------------------***光荣榜***-----------------\n");
		printf("\t\t**************************************************\n");
		printf("\t\t********1.C语言-------------------2.高数**********\n");
		printf("\t\t********3.大物--------------------4.退出系统******\n");
		printf("\t\t**************************************************\n");
		printf("请输入你要查询的排名:\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("\t学号:\t\t名字:\tC语言成绩:\n");
			int rs = sizeof(Stu) / sizeof(Stu[0]);
			qsort(Stu, rs, sizeof(Stu[0]), PaiXu);//排序
			for (int i = 0; i < m; i++)
			{
				printf("第%d名\t%s\t%s\t%.2lf", i+1, Stu[i].Stuid, Stu[i].Stuname, Stu[i].score[0]);
				printf("\n");
			}
			break;
		case 2:
			printf("\t学号:\t\t名字:\t高数成绩:\n");
			int rs1 = sizeof(Stu) / sizeof(Stu[0]);
			qsort(Stu, rs1, sizeof(Stu[0]), PaiXu1);
			for (int i = 0; i < m; i++)
			{
				printf("第%d名\t%s\t%s\t%.2lf", i+1, Stu[i].Stuid, Stu[i].Stuname, Stu[i].score[0]);
				printf("\n");
			}
			break;
		case 3:
			printf("\t学号:\t\t名字:\t大物成绩:\n");
			int rs2 = sizeof(Stu) / sizeof(Stu[0]);
			qsort(Stu, rs, sizeof(Stu[0]), PaiXu2);
			for (int i = 0; i < m; i++)
			{
				printf("第%d名\t%s\t%s\t%.2lf", i+1, Stu[i].Stuid, Stu[i].Stuname, Stu[i].score[0]);
				printf("\n");
			}
			break;
		case 4:
			return;
		default:
			printf("选择错误,重新选择!\n");
			break;
		}
	}
}
int sum = 1;
int main()
{
	int i = 0;
	int input = 0, count = 0;
	char mima[20] = "123";//登入的密码
	char shuru[20];
	char mingzi[20];
	system("color F4");
	printf("\t\t\t     **************************************\n");
	printf("\t\t\t     |       *欢迎使用学生管理系统*       |\n");
	printf("\t\t\t     |           *管理员: 小唐*           |\n");
	printf("\t\t\t      ------------------------------------\n");
	printf("请输入管理员密码:\n");
	while ((count = _getch()) != '\r')
	{
		if (count == '\b')
		{
			i--;
			printf("\b \b");
		}
		else
		{
			shuru[i++] = count;
			printf("*");
		}
	}
	shuru[i++] = '\0';   
	if (strcmp(mima, shuru) == 0)
	{
		printf("\n密码正确,您已进入系统!\n");
	}
	else
	{
		printf("\n密码错误,请重新输入!\n");
		return 0;
	}
	system("pause");
	system("cls");
	do
	{
		menu2();
		printf("请选择继续还是结束操作:\n");
		scanf("%d", &sum);
		switch (sum)
		{
		case Quit:
			system("cls");
			printf("您已退出学生管理系统!\n");
			break;
		case Resume:
			menu1();
			void(*p[9])() = { NULL,TiShi,StuXinXi,ChaZhao,ShanGai,BaoCun,XianShi,XiuGai,PaiMing };//指针数组函数
			printf("请输入你要进行的操作\n");
			scanf("%d", &input);
			system("cls");
			(*p[input])();
			break;
		default:
			printf("输入错误,重新输入!\n");
			break;
		}
	} while (sum);
	return 0;
}

Guess you like

Origin blog.csdn.net/2301_79201049/article/details/134845399