#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100000 //电话号码最多个数
typedef struct {char elem[8];int time;} item;//电话号码结构体,包括号码字符串、同样串出现次数
item ph[MAX];//记录实际输入的号码数
/*
A, B, and C map to 2
D, E, and F map to 3
G, H, and I map to 4
J, K, and L map to 5
M, N, and O map to 6
P, R, and S map to 7
T, U, and V map to 8
W, X, and Y map to 9
*/转换规则
char hash(char ch)//把字符转成数字的ASCII码eg:7+‘0’表示7的ASCII码
{
if(ch<'P') return (char)((ch-'A')/3+2+'0');
else if(ch>'S'&&ch<'Z') return (char)((ch-'T')/3+8+'0');
else return (char)(7+'0');
}
void trans(char * str,int ii)//把原始输入串转成一般规范数字形式存入ph数组中
{
int i = 0,j = 0;
while(str[i]!='/0')
{
if(str[i]=='-') {i++; continue;}
else if(str[i]>='A'&&str[i<'Z']) ph[ii].elem[j++]=hash(str[i++]);
else ph[ii].elem[j++]=str[i++];
}
ph[ii].elem[j]='/0';
ph[ii].time = 1;
}
int cmp(const void * a,const void * b)
{
return strcmp(((item *)a)->elem,((item *)b)->elem);
}
void main()
{
int i=0,j=0,n;
int fg=1;
char s[100];
scanf("%d",&n);
getchar();
while(i<n)
{
scanf("%s",s);
trans(s,i);
i++;
}
qsort(ph,n,sizeof(item),cmp);
//ph中的各个串已排好序
i=0,j=0;
while(i++<n)
{
if(strcmp(ph[i].elem,ph[j].elem)!=0)
{
if(ph[j].time >1)
{
printf("%c%c%c-%c%c%c%c %d/n",ph[j].elem[0],ph[j].elem[1],ph[j].elem[2],
ph[j].elem[3],ph[j].elem[4],ph[j].elem[5],ph[j].elem[6],ph[j].time);
fg=0;
}
j=i;
}
else ph[j].time+=ph[i].time;
}
//j指向前面的,i指向后面的,在已经排序的数组中,i、j所指串相同则把j的time加上i的,然后j再从i的位置继续往后走
if(fg) printf("No duplicates./n");
}
本文介绍了一个C语言程序,用于处理电话号码数据,通过将字母转换为对应的数字,并统计每个电话号码出现的次数。该程序首先定义了电话号码的结构体,并实现了一套转换规则将包含字母的电话号码标准化为纯数字格式,最后通过排序和比较来统计重复号码的数量。

1179

被折叠的 条评论
为什么被折叠?



