69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include<malloc.h>
struct tel
{
//char *num;//wrong
char num[15];
int n;
};
void change(char *,int);
int main()
{
int tel_num=0;
int i=0,j=0;
scanf("%d",&tel_num);
struct tel *p_tel=(struct tel*)malloc(sizeof(struct tel)*tel_num);
for(i=0;i<tel_num;i++)//get/deal tel nums
{
//gets(p_tel[i].num);
scanf("%s",p_tel[i].num);
p_tel[i].n=0;
change(p_tel[i].num,strlen(p_tel[i].num));//deal with
}
int q;
for(i=0;i<tel_num-1;i++)//sort of tel_num
{
for(j=0;j<tel_num-1-i;j++)
{
if(strcmp((p_tel[j].num),(p_tel[j+1].num))>0)
{
char m[8];
strcpy(m,p_tel[j].num);
strcpy(p_tel[j].num,p_tel[j+1].num);
strcpy(p_tel[j+1].num,m);
}
else if(strcmp((p_tel[j].num),(p_tel[j+1].num))==0)
{
p_tel[j].n++;
tel_num--;
for(q=j;q<tel_num;q++)
{
strcpy(p_tel[q].num,p_tel[q+1].num);
}
j--;//for three and more same tel_num
}
/*
printf("begin i=%d,j=%d\n",i,j);
for(q=0;q<tel_num;q++)//check of scanf
{
puts(p_tel[q].num);
//puts(change(p_tel[i].num,strlen(p_tel[i].num)));}
}
*/
}
}
for(j=0;j<tel_num;j++)
{
for(i=0;i<3;i++)
{
printf("%c",p_tel[j].num[i]);
}
printf("-");
for(i=3;i<7;i++)
{
printf("%c",p_tel[j].num[i]);
}
printf(" %d\n",p_tel[j].n);
}
free(p_tel);
return 0;
}
void change(char *p,int len)
{
//printf("Len=%d\n",len);
char* ans=(char*)malloc(sizeof(char)*(len+1));
int i=0;
int j=-1;
for(i=0;i<len;i++)
{
switch(p[i])
{
case '1':
j++;
ans[j]='1';
break;
case 'A':
case 'B':
case 'C':
case '2':
j++;
ans[j]='2';
break;
case 'D':
case 'E':
case 'F':
case '3':
j++;
ans[j]='3';
break;
case 'G':
case 'H':
case 'I':
case '4':
j++;
ans[j]='4';
break;
case 'J':
case 'K':
case 'L':
case '5':
j++;
ans[j]='5';
break;
case 'M':
case 'N':
case 'O':
case '6':
j++;
ans[j]='6';
break;
case 'P':
case 'R':
case 'S':
case '7':
j++;
ans[j]='7';
break;
case 'T':
case 'U':
case 'V':
case '8':
j++;
ans[j]='8';
break;
case 'W':
case 'X':
case 'Y':
case '9':
j++;
ans[j]='9';
break;
case '0':
j++;
ans[j]='0';
break;
}
}
ans[++j]=0;
strcpy(p,ans);
free(ans);
}