电话号码簿问题中的查找和排序问题求大神帮忙解决!
在实现查找和排序功能时,虽然代码没出错,但是功能并不能实现,又不知道怎么改,现在很急,求大神帮忙啊,我新手一个求带啊!查找功能的要求是:输入要查找的姓名,若找到则输出这个人的全部信息,若找不到,则将这个人的新的信息插入到电话号码簿中,并且再次排序后打印出来;排序功能就是能够在每次打印前按照手机号的数值大小排序(假设手机号是3位数)
# include<stdio.h>
# include<stdlib.h>
# include<string.h>
#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<Length; 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; i<P->TD_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;
}