C language small project - student performance management system

System interface and related requirements

1) The system is running, and the following interface is opened. List the system help menu (that is, the command menu), prompting for commands.

insert image description here

2) At the beginning, the grades have not been entered, so the grades cannot be listed even when the command L is entered. It should prompt "The score sheet is empty! Please use the command T to enter the student's score first."
Similarly, when entering other score processing commands, it will also be processed accordingly.
insert image description here

3) Enter the command T and call the Type subfunction to record the grades.
The interface prompts to enter the number of students

Enter 3 to prompt to enter the grades of 3 courses of 3 students, and list the header of the transcript "Student Number Chinese Mathematics English", prompting student number: 1

Enter the 3 course grades of student No. 1, separated by spaces, and press Enter to end. Prompt student number: 2

Enter the 3 course grades of student No. 2, separated by spaces, and press Enter to end. Prompt student number: 3

Enter the 3 course grades of student No. 3, separated by spaces, and press Enter to end. The Type sub-function call ends and returns. Prompts for commands.insert image description here

4) Input the command L and call the List subfunction to output the score sheet. The call of the List subfunction ends and returns. Prompts for commands.insert image description here

5) Input command A, call the Average sub-function to calculate the average score, and prompt "The average score has been calculated. Please use command L to view it." The Average sub-function call ends and returns. Prompts for commands.
Input the command L and call the List subfunction to output the score sheet. The call of the List subfunction ends and returns. Prompts for commands.
insert image description here

6) Input the command P, call the Sort sub-function to sort the records of each student according to the average score from high to low, and prompt "Sorting is complete. Please use command L to view." The Sort sub-function call ends and returns. Prompts for commands.

Input the command L and call the List subfunction to output the score sheet. The call of the List subfunction ends and returns. Prompts for commands.
insert image description here

7) Enter the command S, call the Search sub-function to query the student's grades, and prompt "Enter the student number to be queried".

Enter 2 to find the grade of student No. 2 and output it. The Search sub-function is called and returns. Prompts for commands.
insert image description here

8) Enter the command C to execute the clearing function statement system("clear");

Clears all contents of the screen. Prompts for commands.
insert image description hereinsert image description here

9) Enter the command H to call the Help subfunction to display the help menu. The help sub-function is called and returns. Prompts for commands.
insert image description here

10) Enter the command Q to exit the system.
insert image description here

Note:
1) When outputting array elements, the student number should be processed separately and output as an integer (that is, keep 0 decimal places). In the same way, when calculating the grades, the student numbers in the first column should be ignored, and only those after the second column will be calculated. Grades retain 1 decimal place.
2) The number of students is n throughout, and the value of n is used to judge whether the sub-function of the current command can be called and executed. For example: when n=0, it means that the result has not been entered yet. And once the command T is input, that is, the Type sub-function is called to enter the grade, the value of n is no longer 0. When n! =0, you can perform other grade operations, but you can no longer perform the operation of recruiting grades. So when the command entered by the user cannot be executed, a prompt should be given.

the code

#include <stdio.h>
#include <stdlib.h>
//#include "hs.h"
struct student
{
    
    
	int id;
	float yw;
	float sx;
	float wy;
	float pj;
};
void help(void);
int type(struct student *p);
void list(struct student *p,int n);
void average(struct student *p,int n);
void search (struct student *p);
void sort(struct student *p,int n);
int main(int argc, const char *argv[])
{
    
    
	char ch;
	struct student stu[32];
	int n=0;
	while(1)
	{
    
    
		printf("请输入命令 = ");
		//getchar();
		scanf("%c",&ch);
		putchar(10);
		if(ch=='T')
		{
    
    
			n=type(stu);
		}
		else if(ch=='L')
		{
    
    
			if(n==0)
			{
    
    
				printf("成绩表为空!请先使用T录入成绩!\n");
				putchar(10);
			}
			else
				list(stu,n);
		}
		else if(ch=='A')
		{
    
    
			if(n==0)
			{
    
    
				printf("成绩表为空!请先使用T录入成绩!\n");
				putchar(10);
			}
			else
			{
    
    
				average(stu,n);
				printf("平均分已计算,请使用命令L查看!\n");
				putchar(10);
			}
		}
		else if(ch=='H')
			help();
		else if(ch=='C')
			system("clear");
		else if(ch=='S')
		{
    
    
			if(n==0)
			{
    
    
				printf("成绩表为空!请先使用T录入成绩!\n");
				putchar(10);
			}
			else
			{
    
    
				search(stu);
				putchar(10);
			}
		}
		else if(ch=='P')
		{
    
    
			if(n==0)
			{
    
    
				printf("成绩表为空!请先使用T录入成绩!\n");
				putchar(10);
			}
			else
			{
    
    
				sort(stu,n);
				putchar(10);
			}
		}
		else if(ch=='Q')
		{
    
    
			printf("Press any key to continue!\n");
			return -1;
		}
		getchar();
	}
	return 0;
}
int type(struct student *p)
{
    
    
	int n=0;
	printf("请输入学生人数:");
	scanf("%d",&n);
	printf("请输入学生三门课的成绩:\n");
	printf("学号 语文 数学 外语\n");
	for(int i=0;i<n;i++)
	{
    
    
		printf("%d    ",i+1);
		struct student stu[i];
		scanf("%f %f %f",&(p+i)->yw,&(p+i)->sx,&(p+i)->wy);
	}
	return n;
}
void list(struct student *p,int n)
{
    
    
	printf("学生成绩如下:\n");
	printf("学号 语文 数学 外语 平均分\n");
	for(int i=0;i<n;i++)
	{
    
    
		printf("%d    ",i+1);
		printf("%.1f %.1f %.1f %.1f",p->yw,p->sx,p->wy,p->pj);
		p++;
		putchar(10);
	}
}
void average(struct student *p,int n)
{
    
    
	for(int i=0;i<n;i++)
	{
    
    
		(p->pj)=((p->yw)+(p->sx)+(p->wy))/3;
		p++;
	}
}
void help(void)
{
    
    
	printf("**********************************\n");
	printf(" *  学生成绩管理系统——帮助菜单  * \n");
	printf("**********************************\n");
	printf(" *   H = 显示帮助菜单           * \n");
	printf(" *   T = 成绩录入               * \n");
	printf(" *   A = 计算学生平均分         * \n");
	printf(" *   L = 列出成绩单             * \n");
	printf(" *   P = 按平均成绩由高到低排序 * \n");
	printf(" *   S = 按学号查询学生成绩     * \n");
	printf(" *   C = 清屏                   * \n");
	printf(" *   Q =退出系统                * \n");
	printf("**********************************\n");
	printf(" *Copyright(c) 2022.3.15 By liq* \n");
	printf("**********************************\n");
}
void search(struct student *p)
{
    
    
	int s=0;
	printf("请输入要查询的学生号:");
	scanf("%d",&s);
	printf("学号 语文 数学 外语 平均分\n");
	printf("%d   %.1f  %.1f  %.1f  %.1f",s,(p+s-1)->yw,(p+s-1)->sx,(p+s-1)->wy,(p+s-1)->pj);
	putchar(10);
}
void sort(struct student *p,int n)
{
    
    
	struct student temp;
	int i,j;
	for(i=0;i<n;i++)
	{
    
    
		for(j=0;j<n-i-1;j++)
		{
    
    
			if(p[j].pj<p[j+1].pj)
			{
    
    
				temp=p[j];
				p[j]=p[j+1];
				p[j+1]=temp;
			}
		}
	}
	printf("排序完成,请使用命令L查看!\n");
}

Notice

Write in separate files if necessary.
Just take out the function part of the above code and create two new files: fun.c and fun.h. The fun.c file is used to store the structure declaration and function part of the above code (plus the corresponding header file). fun.h is used to store structure declarations and function declarations (plus corresponding header files).
Add the corresponding header file to the main function: #include "fun.h" (double quotes, not <>).
When compiling, you need to compile the main function and the newly created fun.c file together, and the operation is still the same as before, just use ./a.out to run.
The details are shown in the figure below:
1. Create two new files (same name, different suffixes), compile and run (multiple files need to be compiled at the same time).
insert image description here

2.hs.c stores structure declarations and corresponding functions (the functions inside can also be split into other files, so I won’t split them here).

#include <stdio.h>
#include <stdlib.h>
struct student
{
    
    
	int id;
	float yw;
	float sx;
	float wy;
	float pj;
};
int type(struct student *p)
{
    
    
	int n=0;
	printf("请输入学生人数:");
	scanf("%d",&n);
	putchar(10);
	printf("请输入学生三门课的成绩:\n");
	putchar(10);
	printf("学号 语文 数学 外语\n");
	for(int i=0;i<n;i++)
	{
    
    
		printf("%d    ",i+1);
		struct student stu[i];
		scanf("%f %f %f",&(p+i)->yw,&(p+i)->sx,&(p+i)->wy);
	}
	putchar(10);
	return n;
}
void list(struct student *p,int n)
{
    
    
	printf("学生成绩如下:\n");
	printf("学号 语文 数学 外语 平均分\n");
	for(int i=0;i<n;i++)
	{
    
    
		printf("%d    ",i+1);
		printf("%.1f %.1f %.1f %.1f",p->yw,p->sx,p->wy,p->pj);
		p++;
		putchar(10);
	}
	putchar(10);
}
void average(struct student *p,int n)
{
    
    
	for(int i=0;i<n;i++)
	{
    
    
		(p->pj)=((p->yw)+(p->sx)+(p->wy))/3;
		p++;
	}
}
void help(void)
{
    
    
	printf("**********************************\n");
	printf(" *  学生成绩管理系统——帮助菜单  * \n");
	printf("**********************************\n");
	printf(" *   H = 显示帮助菜单           * \n");
	printf(" *   T = 成绩录入               * \n");
	printf(" *   A = 计算学生平均分         * \n");
	printf(" *   L = 列出成绩单             * \n");
	printf(" *   P = 按平均成绩由高到低排序 * \n");
	printf(" *   S = 按学号查询学生成绩     * \n");
	printf(" *   C = 清屏                   * \n");
	printf(" *   Q =退出系统                * \n");
	printf("**********************************\n");
	printf(" *Copyright(c) 2022.3.15 By liq* \n");
	printf("**********************************\n");
}
void search(struct student *p)
{
    
    
	int s=0;
	printf("请输入要查询的学生号:");
	scanf("%d",&s);
	putchar(10);
	printf("学号 语文 数学 外语 平均分\n");
	printf("%d   %.1f  %.1f  %.1f  %.1f",s,(p+s-1)->yw,(p+s-1)->sx,(p+s-1)->wy,(p+s-1)->pj);
	putchar(10);
}
void sort(struct student *p,int n)
{
    
    
	struct student temp;
	int i,j;
	for(i=0;i<n;i++)
	{
    
    
		for(j=0;j<n-i-1;j++)
		{
    
    
			if(p[j].pj<p[j+1].pj)
			{
    
    
				temp=p[j];
				p[j]=p[j+1];
				p[j+1]=temp;
			}
		}
	}
	printf("排序完成,请使用命令L查看!\n");
}

3.hs.h stores the structure declaration and the function declaration corresponding to the function in hs.c.

#include <stdio.h>
#include <stdlib.h>
struct student
{
    
    
	int id;
	float yw;
	float sx;
	float wy;
	float pj;
};
int type(struct student *p);
void list(struct student *p,int n);
void average(struct student *p,int n);
void help(void);
void search(struct student *p);
void sort(struct student *p,int n);

4. main function

#include <stdio.h>
#include <stdlib.h>
#include "hs.h"
int main(int argc, const char *argv[])
{
    
    
	char ch;
	struct student stu[32];
	int n=0;
	while(1)
	{
    
    
		printf("请输入命令 = ");
		scanf("%c",&ch);
		putchar(10);
		if(ch=='T')
		{
    
    
			n=type(stu);
		}
		else if(ch=='L')
		{
    
    
			if(n==0)
			{
    
    
				printf("成绩表为空!请先使用T录入成绩!\n");
				putchar(10);
			}
			else
				list(stu,n);
		}
		else if(ch=='A')
		{
    
    
			if(n==0)
			{
    
    
				printf("成绩表为空!请先使用T录入成绩!\n");
				putchar(10);
			}
			else
			{
    
    
				average(stu,n);
				printf("平均分已计算,请使用命令L查看!\n");
				putchar(10);
			}
		}
		else if(ch=='H')
			help();
		else if(ch=='C')
			system("clear");
		else if(ch=='S')
		{
    
    
			if(n==0)
			{
    
    
				printf("成绩表为空!请先使用T录入成绩!\n");
				putchar(10);
			}
			else
			{
    
    
				search(stu);
				putchar(10);
			}
		}
		else if(ch=='P')
		{
    
    
			if(n==0)
			{
    
    
				printf("成绩表为空!请先使用T录入成绩!\n");
				putchar(10);
			}
			else
			{
    
    
				sort(stu,n);
				putchar(10);
			}
		}
		else if(ch=='Q')
		{
    
    
			printf("Press any key to continue!\n");
			return -1;
		}
		getchar();
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_47023150/article/details/123582338