基于数组的学生信息管理系统小案例

主函数main.c

 


#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Sort.h"
// 定义枚举类型DATA,表示不同的操作选项
enum DATA{
	A='1',
	B,
	C,
	D,
	E,
	F
};

int main()
{
// 创建一个链表对象ls
	list ls{};
	ls.size = 0;
// 无限循环,直到用户选择退出
	while (1)
	{
		view();
		enum Data data {};
		scanf("%c", &data);
		system("cls");
// 根据用户输入的字符执行相应的操作
		switch (data)
		{
		case A:
			Add(&ls);
			system("pause");
			break;		
		case B:
			Delete(&ls);
			system("pause");
			break;		
		case C:
			Update(&ls);
			system("pause");

			break;		
		case D:
			Select(ls);
			system("pause");

			break;		
		case E:
			Sort(&ls);
			system("pause");

			break;	
		case F:
			Dayin(ls);
			system("pause");

			break;		
		default:
			break;

		}
	}
	return 1;
}

Sort.h

#pragma once
#ifndef _SORT_H_  //防止文件重复引用 
#define _SORT_H_ 
#define MAX 20
//定义结构体
typedef struct Student {
	char name[20];
	int age;
	float core;
}Stu;
typedef struct List {
	Stu stu[MAX];
	int size;
}list;
int compare(const void* a, const void* b);
void Sort(struct List* ls);
void view();
void Dayin(struct List ls);
void Add(struct List* ls);
void Delete(struct List* ls);
void Update(struct List* ls);
void Select(struct List ls);
#endif // !_SORT_H_ 

Sort.c


#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include"Sort.h"
//比较器
int compare(const void* a, const void* b) {
	return ((Stu*)b)->core - ((Stu*)a)->core;
}

// 冒泡排序函数
void Sort(struct List* ls) {
	for (int i = 0; i < ls->size - 1; i++) {
		for (int j = 0; j < ls->size - 1 - i; j++) {
			if (compare(&ls->stu[j], &ls->stu[j + 1]) > 0) {
				Stu temp = ls->stu[j];
				ls->stu[j] = ls->stu[j + 1];
				ls->stu[j + 1] = temp;
			}
		}
	}
}
//视图
void view()
{
	printf("1、录入\n");
	printf("2、删除\n");
	printf("3、修改\n");
	printf("4、查找\n");
	printf("5、排序\n");
	printf("6、打印\n");
	printf("请输入功能,输入一个字符\n");
}
//添加
void Add(struct List* ls)
{
	if (ls->size == MAX)
	{
		printf("录入人员已满");
		return;
	}
	else {
		printf("请输入姓名:\n");
		scanf("%s", ls->stu[ls->size].name);
		printf("请输入年龄:\n");
		scanf("%d", &(ls->stu[ls->size].age));
		printf("请输入成绩:\n");
		scanf("%f", &(ls->stu[ls->size].core));
		ls->size++;
		printf("录入成功\n");
		Dayin(*ls);
	}
}
//删除
void Delete(struct List* ls)
{
	char name[20]{};
	printf("请输入要删除的姓名\n");
	scanf("%s", &name);
	int num = 0;
	while (num < ls->size)
	{
		if (strcmp(ls->stu[num].name, name) == 0)
		{
			for (int i = num; i < ls->size; i++)
			{
				ls->stu[i] = ls->stu[i + 1];
			}
			printf("删除成功\n");
			ls->size--;
			return;
		}
		num++;
	}
	printf("未找到该学生\n");
}
//显示
void Dayin(struct List ls)
{
	int num = 0;
	printf("姓名\t年龄\t成绩\t\n");
	while (num < ls.size)
	{
		printf("%s\t%d\t%.2f\t\n", ls.stu[num].name, ls.stu[num].age, ls.stu[num].core);
		num++;
	}
	return;
}
//修改
void Update(struct List* ls)
{
	char name[20]{};
	printf("请输入要修改的学生姓名\n");
	scanf("%s", name);
	int num = 0;
	while (num < ls->size)
	{
		if (strcmp(ls->stu[num].name, name) == 0)
		{
			printf("请输入姓名:\n");
			scanf("%s", ls->stu[num].name);
			printf("请输入年龄:\n");
			scanf("%d", &(ls->stu[num].age));
			printf("请输入成绩:\n");
			scanf("%f", &(ls->stu[num].core));
			printf("修改成功\n");
			return;
		}
		num++;
	}
	printf("未找到该学生\n");

}
//查询
void Select(struct List ls)
{
	char name[20]{};
	printf("请输入要查找的学生姓名\n");
	scanf("%s", name);
	int num = 0;
	while (num < ls.size)
	{
		if (strcmp(ls.stu[num].name, name) == 0)
		{
			printf("姓名\t年龄\t成绩\t\n");
			printf("%s\t%d\t%.2f\t\n", ls.stu[num].name, ls.stu[num].age, ls.stu[num].core);
			return;
		}
		num++;
	}
	printf("未找到该学生\n");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值