c语言中对N个IP地址进行排序

来源:百度知道 编辑:UC知道 时间:2024/07/06 15:42:31
就是你想输入几个就对几个进行排序,还有他们是怎么比较大小,能不能用直接插入法进行排序 谢谢 100分啊
我的排序有什么问题么 我是要按从小到大排序
void sort(char g_fwmTrustedIpTab[FWM_TRUSTED_IP_TAB_MAX_SIZE][FWM_IP_ADDR_LEN])
{
char temp[FWM_IP_ADDR_LEN];
int i,j;
for(i=0;i<g_fwmTrustedIpNum;i++){
for(j=i+1;j<g_fwmTrustedIpNum;j++){
if(strcmp(g_fwmTrustedIpTab[i],g_fwmTrustedIpTab[j])>0)
strcpy(temp,g_fwmTrustedIpTab[i]);
strcpy(g_fwmTrustedIpTab[i],g_fwmTrustedIpTab[j]);
strcpy(g_fwmTrustedIpTab[j],temp);
}
}
}

#include <stdio.h>
#include <stdlib.h>

typedef unsigned char BYTE ;

typedef struct _IP {
BYTE seg[4];
struct _IP *next;
}IP;

void main()
{
int i, found;
IP *pip, *ptem, *pfind;
BYTE tem_ip[4];
pip = (IP *)malloc(sizeof(IP));
pip->next = pip;

printf("Input IP address like:\n192.168.1.11\n10.10.128.11\n......\nInput '0.0.0.0' to end input\n");
while (1) {
scanf("%d.%d.%d.%d", &tem_ip[0], &tem_ip[1], &tem_ip[2], &tem_ip[3]);
if (!(tem_ip[0] || tem_ip[1] || tem_ip[2] || tem_ip[3]))
break;
ptem = (IP *)malloc(sizeof(IP));
for (i = 0; i < 4; i++)
ptem->seg[i] = tem_ip[i];

found = 0;
pfind = pip;
while (pfind->next != pip) {
for (i = 0; i < 4; i++) {
if (ptem->seg[i] < pfind->next->seg[i]) {
found = 1;