#define LEN 3
struct people
{
char name[10];
char sex[3];
char number[12];
int age;
};
struct contact
{
struct people *human;
int capacity;
size_t size;
};
void Init(struct contact *con)
{
con->capacity = LEN;
con->human = (struct people*)malloc(LEN*sizeof(struct people));
if (con->human == NULL)
{
printf("申请失败\n");
return;
}
con->size = 0;
}
void Check_cal(struct contact* con)
{
if (con->size >= con->size)
{
struct people *p = { 0 };
p = (struct people*)realloc(con->human, (con->capacity + 3)*sizeof(struct people));
if (p == NULL)
{
printf("error\n");
return;
}
con->human = p;
con->capacity += 3;
}
}
void Insert(struct contact* con,char*name,char* sex,char* number,int age)
{
Check_cal(con);
con->human[con->size].age = age;
strcpy((con->human[con-> size].sex), sex);
strcpy((con->human[con->size].number), number);
strcpy((con->human[con->size].name), name);
con->size++;
}
void display(struct contact con)
{
for (int i = 0; i < con.size; i++)
{
printf("%s ", con.human[i].name);
printf("%d ", con.human[i].age);
printf("%s", con.human[i].sex);
printf("%s ", con.human[i].number);
printf("\n");
}
}
int find(struct contact *con,char*name)
{
for (int i = 0; i < con->size; i++)
{
if (strcmp(name, con->human[i].name)==0)
return i;
}
return -1;
}
void erase(struct contact *con,char*name)
{
int i=find(con,name);
for (int j = i; j < con->size - 1; j++)
{
con->human[j] = con->human[j + 1];
}
con->size--;
}
void alter(struct contact *con,char*name)
{
int i = find(con,name);
}
void bubbsort(struct contact *con)
{
for (int i = 0; i < con->size; i++)
{
int k = i;
for (int j = i+1; j < con->size; j++)
{
if (strcmp(con->human[k].name, con->human[j ].name))
{
k = j;
}
}
swap(con->human[i],con->human[k]);
}
}
int main()
{
struct contact l;
Init(&l);
Insert(&l, "雷宇飞", "男", "18309233241", 22);
Insert(&l, "尚石资", "男", "18309233242", 22);
Insert(&l, "李乐", "男", "18309233243", 22);
Insert(&l, "找小路", "男", "18309233244", 22);
Insert(&l, "李玲", "男", "18309233245", 22);
Insert(&l, "张蓉", "男", "18309233246", 22);
Insert(&l, "刘蕾", "男", "18309233247", 22);
Insert(&l, "马浩", "男", "18309233248", 22);
Insert(&l, "马晓晨", "男", "18309233249", 22);
Insert(&l, "崔浩本", "男", "1830923324q", 22);
Insert(&l, "马鹏质", "男", "1830923324r", 22);
display(l);
printf("\n");
bubbsort(&l);
erase(&l,"刘蕾");
display(l);
system("pause");
}
实现一个通讯录; 通讯录可以用来存储1000个人的信息,每个人的信息包括: 姓名、性别、年龄、电话、住址 提供方法: 1. 添加联系人信息 2. 删除指定联系人信息 3. 查找指定
最新推荐文章于 2021-05-20 18:12:34 发布