您的位置:首页 > 其它

读取一组电话号码簿(由姓名和手机号码组成),将重复出现的项删除(姓名和电话号码都相同的项为重复项,只保留第一次出现的项),并对姓名相同手机号码不同的项进行如下整理。

2017-09-06 23:13 3739 查看
 
  读取一组电话号码簿(由姓名和手机号码组成),将重复出现的项删除(姓名和电话号码都相同的项为重复项,只保留第一次出现的项),并对姓名相同手机号码不同的项进行如下整理:首次出现的项不作处理,第一次重复的姓名后面加英文下划线字符_和数字1,第二次重复的姓名后面加英文下划线字符_和数字2,依次类推。号码簿中姓名相同的项数最多不超过10个。最后对整理后的电话号码簿按照姓名进行从小到大排序。若输入没有指定排序算法,则只输出排序后的电话号码簿;若输入指定了快速排序算法,则按指定算法按排序键值先输出进行第一趟快速排序后的结果(即只输出姓名),然后输出排序后的电话号码簿。

代码如下:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

typedef struct Phone_struct

{
char sName[20];
char sTel[12];

}PHONE, *PPHONE;

//输出函数

int output_phone(PPHONE pMyPhone, int num)

{
int i = 0;
printf("\n
name  phone\n");
for (int i = 0; i < num; i++)
{
printf("%22s %-15s\n", (pMyPhone[i]).sName, (pMyPhone[i]).sTel);
}
return 0;

}

//delete repert   index < len

int delete_by_index(PPHONE * pMyPhone, int *Len, int index)

{
int i = 0;
PPHONE pMy_Phone = *pMyPhone;
PPHONE tmp_phone = NULL;
int len = (*Len);

if (index = len - 1)
{
tmp_phone = (PPHONE)&(pMy_Phone[index]);
strcpy(tmp_phone->sName,"");
strcpy(tmp_phone->sTel, ""); 
(*Len)--; //总数减一

return 0;
}

for (i = index; i+1 < len; i++)
{
strcpy(pMy_Phone[i].sName, pMy_Phone[i + 1].sName);
strcpy(pMy_Phone[i].sTel, pMy_Phone[i + 1].sTel);
}

tmp_phone = (PPHONE)&(pMy_Phone[index]);
strcpy(tmp_phone->sName, "");
strcpy(tmp_phone->sTel, "");
(*Len)--; //总数减一

return 0;

}

//去重

int clear_repeat(PPHONE *pMyPhone, int *Num)

{
PPHONE pMy_Phone = *pMyPhone;
int i = 0;
int j = 0;
int repeat_count = 0; //计数,是否超过10次
int num = (*Num);
int move_flag = 0;
char sTmp[10] = "";

if (num < 2)
{
return 0;
}

for (i = 0; i < num; i++)
{
//pMyPhone[i]
repeat_count = 0;
for (j = i + 1; j < num; j++)
{
if (move_flag == 1)
{
j--;
move_flag = 0;
}

if ( strcmp(pMy_Phone[i].sName, pMy_Phone[j].sName) == 0) // sName same
{
if (strcmp(pMy_Phone[i].sTel, pMy_Phone[j].sTel) == 0)// sName same && sTel same
{
//delete
delete_by_index(pMyPhone, Num, j);
move_flag = 1;
}
else  // sName same ,but sTel no same
{
//add _1 _2
repeat_count++;
if (repeat_count > 10)  //重复超过10个
{
//delete
delete_by_index(pMyPhone, Num, j);
move_flag = 1;
}
else
{
strcpy(sTmp,"");
sprintf(sTmp,"_%d",repeat_count);
strcat(pMy_Phone[j].sName,sTmp);
}
}
}
}
}
return 0;

}

int phone_change(PPHONE phone1, PPHONE phone2)

{
char sName_tmp[21] = "";
char sTel_tmp[12] = "";

strcpy(sName_tmp, phone2->sName);
strcpy(sTel_tmp, phone2->sTel);

strcpy(phone2->sName, phone1->sName);
strcpy(phone2->sTel, phone1->sTel);

strcpy(phone1->sName, sName_tmp);
strcpy(phone1->sTel, sTel_tmp);

return 0;

}

//冒泡排序

int my_maopao_sort(PPHONE* pMyPhone, int iNum)

{
PPHONE pMy_Phone = (*pMyPhone);
int i = 0;
int j = 0;

for (i = 0; i < iNum-1; i++)
{
for (j = 0; j < iNum -1 -i;j++)
{
if (strcmp(pMy_Phone[j].sName, pMy_Phone[j+1].sName) > 0)
{
phone_change(&(pMy_Phone[j]), &(pMy_Phone[j + 1]));
}
}
}
return 0;

}

int phone_copy(PPHONE dis_phone, PPHONE src_phone)

{
strcpy(dis_phone->sName, src_phone->sName);
strcpy(dis_phone->sTel,src_phone->sTel);

return 0;

}

int phone_cmp(PPHONE phone1, PPHONE phone2)

{
if (strcmp(phone1->sName, phone2->sName) > 0)
return 1;
else if (strcmp(phone1->sName, phone2->sName) == 0) 
return 0;
else  //(strcmp(phone1->sName, phone2->sName) < 0)
return -1;

}

//qsort

int my_qsort(PPHONE *pMyPhone, int left, int right, int first_flag = 0)

{
PPHONE pMy_Phone = (*pMyPhone);
PHONE phone_flag;
int iRight = right;
int iLeft = left;

if (left < right)
{
phone_copy(&phone_flag, &(pMy_Phone[iLeft])); //

while (iLeft < iRight)
{
while ((iLeft < iRight) &&
((phone_cmp(&pMy_Phone[iRight], &phone_flag) >0) || (phone_cmp(&pMy_Phone[iRight], &phone_flag) == 0)))
{
iRight--;
}
if (iLeft < iRight)
{
phone_change(&pMy_Phone[iLeft], &pMy_Phone[iRight]);
iLeft++;
}

while (iLeft <iRight &&
((phone_cmp(&pMy_Phone[iLeft], &phone_flag) < 0) || (phone_cmp(&pMy_Phone[iLeft], &phone_flag) == 0)))
{
iLeft++;
}
if (iLeft < iRight)
{
phone_change(&pMy_Phone[iRight], &pMy_Phone[iLeft]);
iRight--;
}
}
phone_copy(&pMy_Phone[iLeft], &phone_flag); //

if (first_flag == 1)
{
printf("---");
output_phone(*pMyPhone, right + 1);
first_flag = 0;
}
my_qsort(pMyPhone, left, iLeft-1 );
my_qsort(pMyPhone, iLeft+1 , right);

}
return 0;

}

int main()

{
PPHONE pMyPhone;
int iSort_flag = 0;
int iNum = 0;
int i = 0;
int input_erro_flag = 0;

//输入 数量和排序算法
printf("please input num and sort method(1 is qsort,other is default sort method):\n");
scanf("%d %d", &iNum, &iSort_flag);

//开辟空间
pMyPhone = (struct Phone_struct*)malloc(sizeof(struct Phone_struct) * iNum); 

printf("start input:\n");
//开始等待输入数据并存数据
for (i = 0; i < iNum; i++)
{
scanf_s("%s %s", pMyPhone[i].sName, 21, pMyPhone[i].sTel, 12);
fflush(stdin);
}
printf("input end\n");

//输出
//output_phone(pMyPhone,iNum);

//去重
clear_repeat(&pMyPhone, &iNum);

//排序
if (iSort_flag == 0)
{
my_maopao_sort(&pMyPhone, iNum);
}
else
{
my_qsort(&pMyPhone,0,iNum-1,1);
}

//输出
output_phone(pMyPhone, iNum);

free(pMyPhone);
pMyPhone = NULL;

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐