电话薄姓名排序c语言,电话号码簿问题中的查找和排序问题求大神帮忙解决!...

已结贴√

问题点数:10 回复次数:4

ca56232b3bbedf9a539d07f37fffb99a.gif

3144d8b7615c79d9f638db40d5689d26.gif

a218af6549b45ee526caf607ebff1358.gif

0f8df0e29816ae721419de940fb833d1.gif

电话号码簿问题中的查找和排序问题求大神帮忙解决!

在实现查找和排序功能时,虽然代码没出错,但是功能并不能实现,又不知道怎么改,现在很急,求大神帮忙啊,我新手一个求带啊!

查找功能的要求是:输入要查找的姓名,若找到则输出这个人的全部信息,若找不到,则将这个人的新的信息插入到电话号码簿中,并且再次排序后打印出来;排序功能就是能够在每次打印前按照手机号的数值大小排序(假设手机号是3位数)

# include

# include

# include

#define ERROR 0

#define OK 1

#define RL 200

typedef struct{

char *name[RL];

char *phonenum[RL];

char *add[RL];

int TD_length;

}telephoneDirectory;

int creat_telephoneDirectory(telephoneDirectory *P, int Length)//创建电话簿

{

int i;

for (i = 0; i

P->phonenum[i] = (char *)malloc(20 * sizeof(char));

printf("电话号码%d:", i + 1);

scanf("%s", P->phonenum[i]);

P->name[i] = (char *)malloc(Length * 20 * sizeof(char));

printf("姓名%d:", i + 1);

scanf("%s", P->name[i]);

P->add[i] = (char *)malloc(Length * 20 * sizeof(char));

printf("家庭住址%d:", i + 1);

scanf("%s", P->add[i]);

}

P->TD_length = Length;

return OK;

}

int Inserti(telephoneDirectory *P)//插入操作

{

int i, j;

char CR_phone[20] = { '\0' }, CR_name[20] = { '\0' }, CR_add[20] = { '\0' };

printf("请输入插入位置:");

scanf("%d", &i);

if (i<1 || i>P->TD_length + 1) return ERROR;

printf("请输入插入的电话号码:");

scanf("%s", CR_phone);

printf("请输入插入的姓名:");

scanf("%s", CR_name);

printf("请输入插入的家庭住址:");

scanf("%s", CR_add);

P->phonenum[P->TD_length] = (char *)malloc(20 * sizeof(char));

P->name[P->TD_length] = (char *)malloc(20 * sizeof(char));

P->add[P->TD_length] = (char *)malloc(20 * sizeof(char));

if (!P->phonenum[P->TD_length] || !P->name[P->TD_length] || !P->add[P->TD_length]) return ERROR;

for (j = P->TD_length; j >= i; j--){

//P->phonenum[j]=P->phonenum[j-1];

//P->name[j]=P->name[j-1];

strcpy(P->phonenum[j], P->phonenum[j - 1]);

strcpy(P->name[j], P->name[j - 1]);

strcpy(P->add[j], P->add[j - 1]);

}

strcpy(P->phonenum[i - 1], CR_phone);

strcpy(P->name[i - 1], CR_name);

strcpy(P->add[i - 1], CR_add);

P->TD_length++;

return OK;

}

int Deletei(telephoneDirectory *P)//删除操作

{

int i, j;

printf("请输入删除位置:");

scanf("%d", &i);

if (i<1 || i>P->TD_length) return ERROR;

for (j = i; j <= P->TD_length; j++){

P->phonenum[j - 1] = P->phonenum[j];

P->name[j - 1] = P->name[j];

P->add[j - 1] = P->add[j];

}

P->TD_length--;

return OK;

}

int PrintfP(telephoneDirectory *P)//输出

{

int i;

printf("电话簿目前存储数量为:%d\n", P->TD_length);

for (i = 0; iTD_length; i++){

printf("电话号码%d:%s   姓名%d:%s   家庭住址%d:%s\n", i + 1, P->phonenum[i], i + 1, P->name[i], i + 1, P->add[i]);

}

return OK;

}

int Sorti(telephoneDirectory *p)//排序操作

{

int i, j;

char temp;

for (i = 0; i <= p->TD_length; i++){

for (j = 0; j <= p->TD_length - j - 1; j++){

if (p->phonenum[i]>p->phonenum[i + 1]){

temp = *p->phonenum[i+1];

p->phonenum[i+1] = p->phonenum[i];

p->phonenum[i] = &temp;

}

}

}

return OK;

}[b]

int Locatei(telephoneDirectory *p, int *pos = NULL)//查找操作

{

int i=0;

char LC_name[20] = { '\0' };

printf("请输入要查找的人物姓名: ");

scanf("%s", LC_name);

for(i=0;i<=p->TD_length;i++)

{

if(p->name[i] != LC_name)

{

Inserti(p);

break;

}

else

{

*pos = i;

PrintfP(p);

}

}

Sorti(p);

PrintfP(p);

return OK;

}

int main()

{

int length, operation;

telephoneDirectory Q;

printf("创建电话簿\n\n请输入电话簿用户数量:");

scanf("%d", &length);

creat_telephoneDirectory(&Q, length);

while (1){

printf("请选择您想对电话簿进行的操作:\n1、locate(查找).\n2、delete(删除).\n3、insert(插入).\n4、sort(排序).\n5、printfP(输出).\n6、end(结束).\n\n");

printf("请选择您要进行的操作:");

scanf("%d", &operation);

printf("\n");

if (operation == 6) break;

if (operation == 5){

PrintfP(&Q);

}

if (operation == 4){

Sorti(&Q);

}

if (operation == 3){

Inserti(&Q);

}

if (operation == 2){

Deletei(&Q);

}

if (operation == 1){

Locatei(&Q);

}

}

//  free(&Q);

return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值