⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 grademana.cpp

📁 学生成绩管理系统
💻 CPP
字号:
#include <stdio.h>

struct student
{
	int no;        //学号
	char name[15]; //姓名
	int score[3];  //三门课程的成绩
	double avr;    //平均成绩
};

struct student stu[50]; //声明一个结构数组变量

struct student input();
void display(struct student stud[],int count);
void sort(struct student stud[],int count);
void insert(struct student stud[],int count);
void del(struct student stud[],int count);

void main()
{
	int count;
    char ch;
	ch='y';
    
	printf("请输入学员信息。");
	printf("\n");
	count=0;
	while ((ch=='y') || (ch=='Y'))
	{

		stu[count]=input();       //调用录入信息函数
		count++;
		printf("\n 是否继续?(y or n)");
		scanf(" %c",&ch);
	} 
	printf("\n排序前的学员信息如下:");
	display(stu,count);           //调用显示信息函数
	sort(stu,count);              //调用排序函数
	printf("\n排序后的学员信息如下:");
	display(stu,count);	
	
	printf("\n\n是否要插入新学员?(y or n)");
	scanf(" %c",&ch);
	if(ch=='y' || ch=='Y')
	{
		insert(stu,count);       //调用插入信息函数
		count++;
		printf("\n插入新学员后的学员信息如下:");
		display(stu,count);
	}

	printf("\n\n是否要删除某个学员?(y or n)");
	scanf(" %c",&ch);
	if(ch=='y' || ch=='Y')
	{
		del(stu,count);    //调用删除信息函数
		count--;
		printf("\n删除后学员的信息如下:");
		display(stu,count);
	}
}

struct student input()   //录入信息函数
{
		struct student studn;
	    int sum,j;
	    printf("\n学号:");
		scanf("%d",&studn.no);
		
		printf("\n姓名:");
		scanf("%s",studn.name);
		
		printf("\n三门成绩:");
		sum=0;
		printf("\n");
		for(j=0;j<3;j++)
		{
			printf("成绩%d: ",j+1);
			scanf("%d",&studn.score[j]);
			sum+=studn.score[j];
		}
		studn.avr=sum/3.0;
		return studn;
}

void display(struct student stud[],int count) //显示信息函数
{
	printf("\n学号\t姓名\t\t平均成绩");
	
	printf("\n");
	for(int i=0;i<count;i++)
	{
		printf("%-03d",stud[i].no);
		printf("\t%-15s",stud[i].name);
		printf("\t%-10.1f",stud[i].avr);
		printf("\n");
	}
}

void sort(struct student stud[],int count)  //排序函数
{
	/*  冒泡排序法*/
	struct student t;
	for(int i=0;i<count;i++) 
	{
		for(int j=0;j<count-i-1;j++) //比较元素
		{ 
			if(stud[j].avr<stud[j+1].avr)
			{
				t=stud[j];
				stud[j]=stud[j+1];
				stud[j+1]=t;
			}
		}
	}
}

void insert(struct student stud[],int count)  //插入函数
{
	/*插入一个学员的信息,要求插入后的学员信息依然有序*/
	int i,j;
	struct student temp;
	printf("\n请输入要插入的学员信息");

	temp=input();
	for(i=0;i<count;i++)
	{
		if(stud[i].avr<temp.avr)
			break;
	}
	for(j=count;j>=i;j--)
	{
		stud[j+1]=stud[j];
	}
	stud[i]=temp;
}

void del(struct student stud[],int count) //删除函数
{
	int dno;
	printf("请输入要删除的学员的学号:");
	scanf("%d",&dno);
	for(int i=0;i<count;i++)
	{
		if(stud[i].no==dno)
			break;
	}
	for(int j=i;j<count-1;j++)
	{
		stud[j]=stud[j+1];
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -