繁体   English   中英

使用 qsort 按列从最小到最大对二维数组进行排序

[英]Using qsort to sort a 2 dimensional array by columns from least to greatest

我正在尝试使用qsort对每一列从最小到最大进行排序。 作为一个输入的例子

 168.12.110.25
 64.113.134.35
 217.158.91.183
 102.130.129.146
 215.116.26.223
 81.162.78.0
 19.204.25.222
 245.124.138.157
 137.249.183.201
 106.61.236.67
 106.71.236.60
 106.81.240.63
 168.14.111.27
 168.17.111.27
 215.116.26.220
 137.249.111.202
 137.246.111.202

我想要一个类似于的 output

19.204.25.222
64.113.134.35
.
.
.

106.61.236.67
106.71.236.60
.
.
.
137.246.111.202
137.246.111.202

我正在尝试使用嵌套的 for 循环遍历每一列,并告诉我一列是否大于下一列。 并且代码将通过每个 go 并让我现在如果它大于 qsort 将检查并相应地移动它们。

我想知道我是否正确使用了qsort以及我的代码是否有意义。

#include <stdio.h>
#include <stdlib.h>
 
//declare other functions/files to be used in the program
void print_fun(void);
void read_fun(void);
static int compare(const void *a, const void *b, int arg, unsigned char networks[arg][4]);
 
//read command line input and store the information
int main(int argc, char **argv) {
    //declar variable
    int arg = 0;
 
    //make argv into an int
    arg = atoi(argv[1]);
    //assign size to networks
    unsigned char networks[arg][4];
 
    //assign input to networks
    for (int j = 0; j < 1; ++j) {
        if (argc == 1) {
            printf("ERROR ERROR, you messed up\n");
        } else {
            // hold network addresses in a 2-d array, with 4 unsigned char
 
            for (int k = 0; k < arg; k++) {
                for (int i = 0; i < 4; i++) {
                    scanf("%hhu.", &networks[k][i]);
                    //checks to see if scanf was working properly
                    //printf(" %hhu", networks[k][i]);
                }
                //printf("\n");
            }
        }
    }
    return (0);
}
 
 
static int compare(const void *a, const void *b, int arg, unsigned char networks[arg][4]) {
    const event *ae = a, *be = b;
    for (int i = 0; i < arg; i++) {
        for (int j = 0; j < 4; j++) {
            if (ae->networks[i][j] < be->networks [i+1][j])
                return -1;
            else 
            if (ae->networks[i][j] > be->networks[i+1][j])
                return 1;
        }
 
        void qsort(void networks, size_t arg, size_t 4,
                   int(*compar)(const void*a, const void *b));
 
    }
}

您应该像这样使用比较 function :

#include <string.h>

int compare_quads(const void *a, const void *b) {
    return memcmp(a, b, 4);
}

并像这样使用它:

//read command line input and store the information
int main(int argc, char *argv[]) {
    //declare variable
    int arg = 0;
 
    //make argv into an int
    if (argc < 2) {
        printf("usage: %s <number>\n", argv[0]);
        return 2;
    }
    arg = atoi(argv[1]);
    //assign size to networks
    unsigned char networks[arg][4];
 
    //assign input to networks
    for (int k = 0; k < arg; k++) {
        for (int i = 0; i < 4; i++) {
             scanf("%hhu.", &networks[k][i]);
        }
    }
    qsort(networks, arg, sizeof(networks[0]), compare_quads);
    //print the networks
    for (int k = 0; k < arg; k++) {
        printf("%d.%d.%d.%d\n", networks[k][0], networks[k][1],
                                networks[k][2], networks[k][3]);
    }
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM