使用 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
我想要类似于
的输出
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 循环遍历每一列并告诉我一列是否大于下一列。如果代码大于 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));
}
}
你应该使用这样的比较函数:
#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;
}
我正在尝试使用 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
我想要类似于
的输出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 循环遍历每一列并告诉我一列是否大于下一列。如果代码大于 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));
}
}
你应该使用这样的比较函数:
#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;
}