实现一个通讯录; 通讯录可以用来存储1000个人的信息,每个人的信息包括: 姓名、性别、年龄、电话、住址 提供方法: 1. 添加联系人信息 2. 删除指定联系人信息 3. 查找指定

#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");
 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值