#define _CRT_SECURE_NO_WARNINGS 1
#include"infor.h"
//静态
//void Initstudent(student* pc)
//{
// pc->sz = 0;
// memset(pc->data, 0, sizeof(pc->data));//memset()内存设置 使整个结构体数组初始化
//}
//动态
void Initstudent(student* pc)
{
//指针data维护开辟的动态内存空间 calloc初始化开辟空间
pc->data = (Student*)calloc(begin,sizeof(Student));
pc->sz = 0; //sz为有效信息个数
pc->capacity = begin; //定义目前容量为初始容量
}
void ADDstudent(student* pc)
{
if (pc->sz == pc->capacity) //容量满了完成增容
{
Student* ptr = (Student*)realloc(pc->data, (pc->capacity + add)*sizeof(Student));
if (ptr != NULL)
{
pc->data = ptr;
pc->capacity += add;
printf("~~~增容成功~~~\n");
}
else
{
perror("ADDstudent");//失败报错
printf("增加联系人失败\n");
return;
}
}
//增加一个人的信息
printf("请输入姓名:\n");
scanf("%s", pc->data[pc->sz].name);
printf("请输入性别:\n");
scanf("%s", pc->data[pc->sz].sex);
printf("请输入年龄:\n");
scanf("%d", &(pc->data[pc->sz].age));
printf("请输入电话:\n");
scanf("%s", pc->data[pc->sz].tele);
printf("请输入地址:\n");
scanf("%s", pc->data[pc->sz].addr);
pc->sz++;
printf("增加成功\n");
}
void DESETORYstudent(student* pc) //销毁个人信息
{
free(pc->data); //释放内存
pc->data = NULL; //回归为0
pc->capacity = 0;
pc->sz = 0;
}
//void ADDstudent(student* pc)
//{
// if (pc->sz == MAX)
// {
// printf("通讯录已满,无法添加\n");
// return;
// }
// //增加一个人的信息
// printf("请输入姓名:\n");
// scanf("%s", pc->data[pc->sz].name);
// printf("请输入性别:\n");
// scanf("%s", pc->data[pc->sz].sex);
// printf("请输入年龄:\n");
// scanf("%d", &(pc->data[pc->sz].age));
// printf("请输入电话:\n");
// scanf("%s", pc->data[pc->sz].tele);
// printf("请输入地址:\n");
// scanf("%s", pc->data[pc->sz].addr);
// pc->sz++;
// printf("增加成功\n");
//}
void PRINTstudent(student* pc)
{
int i = 0;
printf("%-10s%-10s%-10s%-15s%-30s\n", "姓名", "性别", "年龄", "电话", "地址");
for (i = 0; i < pc->sz; i++)
{
printf("%-10s%-10s%-10d%-15s%-30s\n",
pc->data[i].name,
pc->data[i].sex,
pc->data[i].age,
pc->data[i].tele,
pc->data[i].addr
);
}
}
static int Findbyname(student* pc, char name[]) //通过名字来查找学生 stadic静态局部变量 该函数只在本文件中应用 保护该函数
{
int i = 0;
for (i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0) //利用strcmp函数的返回值判断是否找到了该人
{
return i; //找到了就返回学生信息的角标
}
}
return -1; //没找到
}
void SEARCHstudent(student* pc)
{
char name[MAX_NAME] = { 0 };
printf("请输入要查找学生姓名:\n");
scanf("%s", name);
int end = Findbyname(pc, name);
if (end == -1)
{
printf("你所要查找的人不存在");
}
else
{
printf("%-10s%-10s%-10s%-15s%-30s\n", "姓名", "性别", "年龄", "电话", "地址");
printf("%-10s%-10s%-10d%-15s%-30s\n", //Findbuname函数如果找到对应学生,返回对应下标,输出对应下标学生信息
pc->data[end].name,
pc->data[end].sex,
pc->data[end].age,
pc->data[end].tele,
pc->data[end].addr
);
}
}
void MODIFYstudent(student* pc)
{
char name[MAX_NAME] = { 0 };
printf("请输入要修改学生信息的学生姓名:\n");
scanf("%s", name);
int end = Findbyname(pc, name);
if (end == -1)
{
printf("你所要修改学生信息的人不存在");
}
else
{
printf("请输入性别:\n");
scanf("%s", pc->data[end].sex);
printf("请输入年龄:\n");
scanf("%d", &(pc->data[end].age));
printf("请输入电话:\n");
scanf("%s", pc->data[end].tele);
printf("请输入地址:\n");
scanf("%s", pc->data[end].addr);
printf("修改成功\n");
}
}
void DELETEstudent(student* pc)
{
char name[MAX_NAME] = { 0 };
printf("请输入要删除学生信息的学生姓名:\n");
scanf("%s", name);
int end = Findbyname(pc, name);
if (end == -1)
{
printf("你所要删除学生信息的人不存在");
}
int i = 0;
for (i = end; i < pc->sz - 1; i++)
{
pc->data[i] = pc->data[i + 1];
}
pc->sz--;
printf("删除成功");
}