#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//#define MAX 1000
#define MAX_NUM 4
#define MAX_NAME 20
#define MAX_AGE 4
#define MAX_SEX 5
#define MAX_ADDR 10
#define DEFAULT_CAPACITY 3//定义初始化内存大小
enum Option//让switch的选择可读性提高
{
EXIT,
ADD,
DEL,
SEARCH,
CHANGE,
SHOW,
SORT
};
typedef struct PeoInfo//定义学生 学号 姓名 年龄 性别 地址
{
int num;//1000-9999
char name[MAX_NAME];
int age;
char sex[MAX_SEX];
char addr[MAX_ADDR];
}PeoInfo;
typedef struct Student
{
struct PeoInfo *data;//此时data是结构体指针可以指向动态开辟的空间
int size;//记录当前班级有多少人
int capacity;//当前班级的最大容量
}Student;
void menu();
void InitStudent(struct Student* ps);
void Add(struct Student* ps);
void Del(struct Student* ps);
void Search(const struct Student* ps);
void Change(struct Student* ps);
void Show(const struct Student* ps);
void Sort(struct Student* ps);
//int FindByName(struct Student*, char name[MAX_NAME]);--此函数不需要暴露,只需要内部调用即可
void DestoryStudent(Student*ps);
#define _CRT_SECURE_NO_WARNINGS 1
#include"DT_student.h"
void menu()
{
printf("**************学生管理系统*******************\n");
printf("***1.添加学生 2.删除学生****\n");
printf("***3.查找学生 4.修改学生信息\n");
printf("***5.展示 6.排序********\n");
printf("***0.exit \n");
}
void InitStudent(struct Student* ps)//初始化
{
ps->data =(PeoInfo*) malloc(DEFAULT_CAPACITY * sizeof(PeoInfo));//先开辟三个结构体空间首地址交给data
if (ps->data == NULL)//开辟空间失败直接结束
{
return;
}
ps->size = 0;//记录学生个数
ps->capacity = DEFAULT_CAPACITY;//默认初始值个数3
}
static int FindByName(struct Student* ps, char name[MAX_NAME])//专门用来查找有没有输入的那个学生
{//static修饰本函数后,只能在本源文件内被使用
int i = 0;
for (i = 0; i < ps->size; i++)
{
if (strcmp(ps->data[i].name, name) == 0)
{
return i;//找到就把元素下标返回
}
}
return -1;//找不到情况
}
void Checkcapacity(struct Student* ps)
{
if (ps->size == ps->capacity)//说明容量已满
{
//增容
struct PeoInfo *ptr=realloc(ps->data, (ps->capacity + 2) * sizeof(struct PeoInfo));
if (ptr != NULL)//增容成功
{
ps->data = ptr;//这样就把新开辟的地址重新交给ps指向的data维护并且防止开辟失败把原先数据清除
ps->capacity += 2;
printf("增容成功\n");
}
else
{
printf("增容失败");
}
}
}
void Add(struct Student* ps)
{
//检测当前容量是否还存的下
//1.满了增加空间
//2.不满的话不进行操作
Checkcapacity(ps);
//增加数据
printf("请输入学号\n");
scanf("%d", &ps->data[ps->size].num);
//先检测要录入的学号存不存在 1000-9999
/*for (int i = 1000; i <= 9999; i++)
{
if (ps->data[i].num == ps->data[ps->size].num)
{
}
}*/
printf("请输入名字\n");
scanf("%s", ps->data[ps->size].name);
printf("请输入年龄\n");
scanf("%d", &(ps->data[ps->size].age));
printf("请输入性别\n");
scanf("%s", ps->data[ps->size].sex);
printf("请输入地址\n");
scanf("%s", ps->data[ps->size].addr);
ps->size++;
printf("添加成功\n");
}
void Del(struct Student* ps)
{
char name[MAX_NAME];
printf("请输入要删除人的名字\n");
scanf("%s", name);
//1查找要删除的人在什么位置
int pos = FindByName(ps, name);//找到了返回名字所在的元素下标,找不到返回-1
//2删除
if (pos == -1)
{
printf("要删除的人不存在\n");
}
else
{
int j = 0;
for (j = pos; j < ps->size - 1; j++)//删除完其中一个后,其后的元素都向前挪一位
{
ps->data[j] = ps->data[j + 1];
}
ps->size--;//为了删除完后长度-1这样就显现不出来原先有的或者出现越界访问
printf("删除成功\n");
}
}
void Search(const struct Student* ps)
{
char name[MAX_NAME];
printf("请输入要查找的学生名\n");
scanf("%s", name);
int pos = FindByName(ps, name);
if (pos == -1)
{
printf("要查找的人不存在\n");
}
else//找到的情况
{
printf("%-5s\t%-5s\t%-5s\t%-5s\t%-5s\n", "学号", "姓名", "年龄", "性别", "地址");
printf("%-5d\t%-5s\t%-5d\t%-5s\t%-5s\n",
ps->data[pos].num,
ps->data[pos].name,
ps->data[pos].age,
ps->data[pos].sex,
ps->data[pos].addr);
}
}
void Change(struct Student* ps)
{
char name[MAX_NAME];
printf("请输入你要修改的学生名\n");
scanf("%s", name);
int pos = FindByName(ps, name);
if (pos == -1)
{
printf("要修改人的信息不存在\n");
}
else//通过返回的下标直接让data数据里的相关下标进行输入
{
printf("请输入学号\n");
scanf("%d", &ps->data[pos].num);
printf("请输入名字\n");
scanf("%s", ps->data[pos].name);
printf("请输入年龄\n");
scanf("%d", &(ps->data[pos].age));
printf("请输入性别\n");
scanf("%s", ps->data[pos].sex);
printf("请输入地址\n");
scanf("%s", ps->data[pos].addr);
printf("修改成功\n");
}
}
void Show(const struct Student* ps)
{
if (ps->size == 0)
{
printf("班级为空\n");
}
else
{
printf("%-5s\t%-5s\t%-5s\t%-5s\t%-5s\n", "学号", "姓名", "年龄", "性别", "地址");
for (int i = 0; i < ps->size; i++)
{
printf("%-5d\t%-5s\t%-5d\t%-5s\t%-5s\n",
ps->data[i].num,
ps->data[i].name,
ps->data[i].age,
ps->data[i].sex,
ps->data[i].addr);
}
}
}
struct PeoInfo tmp;
void Sort(struct Student* ps)
{
for (int i = 0; i < ps->size; i++)
{
for (int j = 0; j < ps->size - i; j++)
{
if (ps->data[j].num <= ps->data[j + 1].num)
{
tmp = ps->data[j];
ps->data[j] = ps->data[j + 1];
ps->data[j + 1] = tmp;
}
}
}
if (ps->size == 0)
{
printf("班级为空\n");
}
else
{
printf("%-5s\t%-5s\t%-5s\t%-5s\t%-5s\n", "学号", "姓名", "年龄", "性别", "地址");
for (int i = 0; i < ps->size; i++)
{
printf("%-5d\t%-5s\t%-5d\t%-5s\t%-5s\n",
ps->data[i].num,
ps->data[i].name,
ps->data[i].age,
ps->data[i].sex,
ps->data[i].addr);
}
}
}
void DestoryStudent(Student* ps)
{
free(ps->data);
ps->data = NULL;
}