麻烦帮我编写个程序,用C语言,谢谢
发布网友
发布时间:2024-10-08 02:49
我来回答
共1个回答
热心网友
时间:2024-12-15 04:58
#include<stdio.h>
FILE *fin,*fout;
void main()
{
char s1[50][20],s2[50][20],s[50][20],temp[20];
int len,NN=0;
int i1,i2,i3,i4,j1,j2,j3,j4,j5;
int i,j;
if ( (fin=fopen("mac.txt","r"))==NULL){
printf("open mac.txt error\n");
exit(0);
};
if ( (fout=fopen("mac.out","w"))==NULL){
printf("open mac.out error\n");
exit(0);
};
NN=0;
while(1)
{
j = fscanf(fin,"%s %s",&s1[NN],&s2[NN]);
if (j==EOF) break;
NN++;
}
// printf("%d ",NN);
for (j=0;j<NN;j++){
strcpy(s[j],s1[j]);
len=strlen(s[j]);
for (i=0;i<len;i++) if (s[j][i]=='.') s[j][i]=' ';
sscanf(s[j],"%d %d %d %d",&i1,&i2,&i3,&i4);
sprintf(s[j],"%03d%03d%03d%03d\0", i1,i2,i3,i4);
}
for (i=0;i<NN-1;i++){
for (j=i+1;j<NN;j++){
if (strcmp(s[i], s[j]) > 0) {
strcpy(temp,s[i]); strcpy(s[i],s[j]); strcpy(s[j],temp);
strcpy(temp,s1[i]); strcpy(s1[i],s1[j]); strcpy(s1[j],temp);
strcpy(temp,s2[i]); strcpy(s2[i],s2[j]); strcpy(s2[j],temp);
}; // end if
};
}; // end for
for (j=0;j<NN;j++){
len = strlen(s2[j]);
for (i=0;i<len;i++) if (s2[j][i] == ':') s2[j][i]='-';
fprintf(fout,"%s=%s|Y-%03d\n",s2[j],s1[j],j);
}
fclose(fin);fclose(fout);
printf("\007Output in mac.out");
}
输入文件:
192.168.0.121 00:E0:18:08:30:77
162.168.0.85 00:E0:2B:0B:1C:FE
192.168.0.1 00:17:31:35:0C:21
192.168.0.2 00:17:31:35:0B:B0
192.168.0.3 00:E0:18:08:30:77
192.168.0.4 00:17:31:AC:63:83
192.168.0.5 00:17:31:35:11:F3
192.168.0.6 00:17:31:35:0C:0C
192.168.0.7 00:17:31:AC:90:43
192.168.0.8 00:17:31:AC:89:6E
排队输出:
00-E0-2B-0B-1C-FE=162.168.0.85|Y-000
00-17-31-35-0C-21=192.168.0.1|Y-001
00-17-31-35-0B-B0=192.168.0.2|Y-002
00-E0-18-08-30-77=192.168.0.3|Y-003
00-17-31-AC-63-83=192.168.0.4|Y-004
00-17-31-35-11-F3=192.168.0.5|Y-005
00-17-31-35-0C-0C=192.168.0.6|Y-006
00-17-31-AC-90-43=192.168.0.7|Y-007
00-17-31-AC-89-6E=192.168.0.8|Y-008
00-E0-18-08-30-77=192.168.0.121|Y-009
===========