#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"
void InitContact(struct con* pc)
{
assert(pc != NULL);
pc->data = (struct people*)malloc(sizeof(struct people) * DEFAULT_SZ);
if(pc->data == NULL)
{
perror("InitContact()");
return;
}
pc->sz = 0;
pc->capacity = DEFAULT_SZ;
}
void DestroyContact(struct con* pc)
{
assert(pc != NULL);
free(pc);
pc->data = NULL;
pc->sz = 0;
pc->capacity = 0;
}
static int check_capacity(struct con* pc)
{
assert(pc != NULL);
if(pc->sz == pc->capacity)
{
pc->data = (struct people*)realloc(pc->data,sizeof(struct people)*(pc->capacity + INC_SZ));
if(pc->data == NULL)
{
perror("check_capacity()");
return 0;
}
pc->capacity += INC_SZ;
printf("增容成功\n");
return 1;
}
else
{
return 1;
}
}
void AddContact(struct con* pc)
{
assert(pc != NULL);
if(0 == check_capacity(pc))
{
return;
}
printf("请输入联系人的姓名:>\n");
scanf("%s",pc->data[pc->sz].name);
printf("请输入联系人的电话:>\n");
scanf("%s",pc->data[pc->sz].tele);
printf("请输入联系人的地址:>\n");
scanf("%s",pc->data[pc->sz].addr);
printf("请输入联系人的性别:>\n");
scanf("%s",pc->data[pc->sz].sex);
printf("请输入联系人的年龄:>\n");
scanf("%d",&(pc->data[pc->sz].age));
pc->sz++;
printf("成功添加新的联系人\n");
}
void ShowContact(struct con* pc)
{
int i = 0;
printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n","姓名","性别","年龄","号码","地址");
for(i=0;i<pc->sz;i++)
{
printf("%-20s\t%-5s\t%-5d\t%-12s\t%-30s\n",pc->data[i].name,
pc->data[i].sex,
pc->data[i].age,
pc->data[i].tele,
pc->data[i].addr);
}
}
int Findpeople(struct con* pc,char name[])
{
int i = 0;
for(i=0;i<pc->sz;i++)
{
if(0 == strcmp(pc->data[i].name,name))
{
return i;
}
}
return -1;
}
void DelContact(struct con* pc)
{
char name[Max_name];
int ret = 0;
int i = 0;
printf("请输入你要删除的联系人的名字\n");
scanf("%s",name);
//查找该联系人
ret = Findpeople(pc,name);
if(ret == -1)
{
printf("找不到该联系人\n");
}
else
{
//删除
for(i = ret;i<pc->sz-1;i++)
{
pc->data[i] = pc->data[i+1];
}
pc->sz--;
printf("指定联系人已经删除\n");
}
}
void SearchContact(struct con* pc)
{
char name[Max_name];
int ret = 0;
printf("输入想要被查找的联系人\n");
scanf("%s",&name);
ret = Findpeople(pc,name);
if(ret == -1)
{
printf("找不到\n");
}
else
{
printf("%-20s\t%-5s\t%-5s\t%-12s\t%--30s\t\n","姓名","性别","年龄","电话","地址");
printf("%-20s\t%-5s\t%-5d\t%-12s\t%-30s\t\n",pc->data[ret].name,
pc->data[ret].sex,
pc->data[ret].age,
pc->data[ret].tele,
pc->data[ret].addr);
}
}
void ModifyContact(struct con* pc)
{
char name[Max_name];
int ret = 0;
printf("请输入要修改的联系人的名字\n");
scanf("%s",&name);
ret = Findpeople(pc,name);
if(ret == -1)
{
printf("不存在该联系人\n");
}
else
{
printf("找到该联系人,请依次输入信息进行修改\n");
printf("请输入该联系人的姓名:>\n");
scanf("%s",pc->data[ret].name);
printf("请输入该联系人的电话:>\n");
scanf("%s",pc->data[ret].tele);
printf("请输入该联系人的地址:>\n");
scanf("%s",pc->data[ret].addr);
printf("请输入该联系人的性别:>\n");
scanf("%s",pc->data[ret].sex);
printf("请输入该联系人的年龄:>\n");
scanf("%d",&(pc->data[ret].age));
printf("修改联系人成功\n");
}
}
void menu()
{
printf("*****************************************\n");
printf("****** 1.name 2.sex ******\n");
printf("****** 3.age 4.addr ******\n");
printf("****** 5.tele 0.exit ******\n");
printf("*****************************************\n");
}
//以名字顺序排序
int com_by_name(const void* str1,const void* str2)
{
return strcmp(((struct people*)str1)->name,((struct people*)str2)->name);
}
//以性别顺序排序
int com_by_sex(const void* str1,const void* str2)
{
return strcmp(((struct people*)str1)->sex,((struct people*)str2)->sex);
}
//以电话顺序排序
int com_by_tele(const void* str1,const void* str2)
{
return strcmp(((struct people*)str1)->tele,((struct people*)str2)->tele);
}
//以地址顺序排序
int com_by_addr(const void* str1,const void* str2)
{
return strcmp(((struct people*)str1)->addr,((struct people*)str2)->addr);
}
//以年龄顺序排序
int com_by_age(const void* str1,const void* str2)
{
return ((struct people*)str1)->age - ((struct people*)str2)->age;
}
void SortContact(struct con* pc)
{
int input = 0;
again:
menu();
printf("请选择以什么方式排序\n");
scanf("%d",&input);
switch(input)
{
case 1:
qsort(pc->data,pc->sz,sizeof(struct people),com_by_name);
break;
case 2:
qsort(pc->data,pc->sz,sizeof(struct people),com_by_sex);
break;
case 3:
qsort(pc->data,pc->sz,sizeof(struct people),com_by_age);
break;
case 4:
qsort(pc->data,pc->sz,sizeof(struct people),com_by_addr);
break;
case 5:
qsort(pc->data,pc->sz,sizeof(struct people),com_by_tele);
break;
case 0:
break;
default:
printf("选择错误,请重新选择\n");
goto again;
break;
}
if(input != 0)
{
printf("排序成功\n");
}
else
{
printf("退出排序成功\n");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
C语言编程的心得和感想.zip
共106个文件
c:98个
h:4个
cpp:2个
5星 · 超过95%的资源 需积分: 10 1 下载量 128 浏览量
2022-10-18
07:15:59
上传
评论 2
收藏 103KB ZIP 举报
温馨提示
C语言编程的心得和感想.zipC语言编程的心得和感想.zipC语言编程的心得和感想.zipC语言编程的心得和感想.zipC语言编程的心得和感想.zipC语言编程的心得和感想.zipC语言编程的心得和感想.zip
资源详情
资源评论
资源推荐
收起资源包目录
C语言编程的心得和感想.zip (106个子文件)
contact.c 5KB
contact.c 4KB
game.c 2KB
game.c 2KB
test.c 1KB
test.c 1KB
test.c 1KB
test.c 1KB
test.c 1KB
test.c 1KB
test.c 1009B
test.c 1008B
test.c 987B
test.c 983B
test.c 950B
test.c 927B
test.c 886B
test.c 872B
test.c 872B
test.c 850B
test.c 848B
test.c 845B
test.c 839B
test.c 833B
test.c 831B
test.c 813B
test.c 803B
test.c 744B
test.c 688B
test.c 687B
test.c 687B
test.c 677B
test.c 667B
test.c 664B
test.c 661B
test.c 660B
test.c 660B
test.c 620B
test.c 615B
test.c 615B
test.c 609B
test.c 590B
test.c 577B
test.c 561B
test.c 555B
test.c 527B
test.c 512B
test.c 490B
test.c 485B
test.c 469B
test.c 453B
test.c 453B
test.c 445B
test.c 440B
test.c 439B
test.c 430B
test.c 426B
test.c 423B
test.c 422B
test.c 411B
test.c 410B
test.c 404B
test.c 400B
test.c 386B
test.c 380B
test.c 379B
test.c 359B
test.c 352B
test.c 332B
test.c 329B
test.c 329B
test.c 328B
test.c 327B
test.c 326B
test.c 323B
test.c 322B
test.c 310B
test.c 308B
test.c 305B
test.c 305B
test.c 303B
test 7-16.c 301B
test.c 296B
test.c 282B
test.c 281B
test.c 241B
tets.c 234B
test.c 232B
test.c 227B
test.c 219B
test.c 196B
test.c 195B
test.c 190B
test.c 189B
test.c 186B
test.c 185B
test.c 185B
test.c 178B
关机游戏.cpp 378B
输入密码2.cpp 377B
共 106 条
- 1
- 2
不安分的小女孩
- 粉丝: 9263
- 资源: 2015
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1