列值报告字符串数组C

列值报告字符串数组C,c,arrays,string,algorithm,C,Arrays,String,Algorithm,我制作了这个字符串的2d数组示例 char *strings[][4] = {{"Sport", "gender", "country", "medal"}, {"Cycling", "Womens", "China", "first"}, {"Swimming", "Womens", "China", "second"}, {"Swimming", "Wome

我制作了这个字符串的2d数组示例

char *strings[][4] = {{"Sport", "gender", "country", "medal"},
                      {"Cycling", "Womens", "China", "first"}, 
                      {"Swimming", "Womens", "China", "second"}, 
                      {"Swimming", "Womens", "Indonesia", "third"}, 
                      {"Cycling", "Womens", "New Zealand", "second"},   
                      {"Cycling", "Womens", "New Zealand", "third"}, 
                      {"Swimming", "Womens", "New Zealand", "first"}}
已经根据列
3 1
对其进行了预排序,首先按国家(第3列)对其进行排序,然后当该列有联系时,再根据体育(第1列)对其进行排序

我正在尝试做一些简单的分层报告,顶部有标题,类似这样:

Country
    Sport   Count
-----------------
China
    Cycling     1
    Swimming    1

Indonesia
    Swimming    1

New Zealand
    Cycling     2
    Swimming    1
{{"Sport", "gender", "country", "medal"},
 {"Cycling", "Womens", "China", "first"}, 
 {"Swimming", "Womens", "China", "second"}, 
 {"Swimming", "Womens", "Indonesia", "third"}, 
 {"Cycling", "Womens", "New Zealand", "second"},   
 {"Cycling", "Womens", "New Zealand", "third"}, 
 {"Swimming", "Womens", "New Zealand", "first"}}
int sport_compare(void const *x, void const *y) {
    typedef char const * const row[4];
    row *fu = x, *ba = y;
    return strcmp(fu[0], ba[0]);
}

int gender_compare(void const *x, void const *y) {
    typedef char const * const row[4];
    row *fu = x, *ba = y;
    return strcmp(fu[1], ba[1]);
}
如果数据首先按国家分组(第3列),则对每个国家的体育项目进行计数,每个国家不得重复任何体育项目

我还想使其多样化,假设数组是在列
3 1 2
上预先排序的,那么它将如下所示:

Country
    Sport   Count
-----------------
China
    Cycling     1
    Swimming    1

Indonesia
    Swimming    1

New Zealand
    Cycling     2
    Swimming    1
{{"Sport", "gender", "country", "medal"},
 {"Cycling", "Womens", "China", "first"}, 
 {"Swimming", "Womens", "China", "second"}, 
 {"Swimming", "Womens", "Indonesia", "third"}, 
 {"Cycling", "Womens", "New Zealand", "second"},   
 {"Cycling", "Womens", "New Zealand", "third"}, 
 {"Swimming", "Womens", "New Zealand", "first"}}
int sport_compare(void const *x, void const *y) {
    typedef char const * const row[4];
    row *fu = x, *ba = y;
    return strcmp(fu[0], ba[0]);
}

int gender_compare(void const *x, void const *y) {
    typedef char const * const row[4];
    row *fu = x, *ba = y;
    return strcmp(fu[1], ba[1]);
}
但是分层报告看起来会有点不同:

China
    Cycling
        Womens 1
    Swimming
        Womens 1

Indonesia
    Swimming
        Womens 1

New Zealand
    Cycling
        Womens 2
    Swimming
        Womens 1
我已经搞清楚了分类,我的问题只是让这种分级报告发挥作用

对于列规范,我刚刚使用了命令行参数,到目前为止,我的程序如下所示:

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

#define MAXSIZE 10

int
main(int argc, char **argv) {
    char *strings[][4] = {{"Sport", "gender", "country", "medal"},
                          {"Cycling", "Womens", "China", "first"}, 
                          {"Swimming", "Womens", "China", "second"}, 
                          {"Swimming", "Womens", "Indonesia", "third"}, 
                          {"Cycling", "Womens", "New Zealand", "second"},   
                          {"Cycling", "Womens", "New Zealand", "third"}, 
                          {"Swimming", "Womens", "New Zealand", "first"}};

    int count, start_index, i;
    int columns[MAXSIZE];

    /* printing command line args out */
    for (i  = 0; i < count; i++) {
        printf("%d ", columns[i]);
    }

    return 0;
}
#包括
#包括
#包括
#定义最大尺寸10
int
主(内部argc,字符**argv){
char*strings[][4]={{“体育”、“性别”、“国家”、“奖牌”},
{“自行车”、“女子”、“中国”、“第一”},
{“游泳”、“女子”、“中国”、“第二”},
{“游泳”、“女子”、“印度尼西亚”、“第三”},
{“自行车”、“女子”、“新西兰”、“第二”},
{“自行车”、“女子”、“新西兰”、“第三”},
{“游泳”、“女子”、“新西兰”、“第一”};
整数计数,起始索引,i;
int列[MAXSIZE];
/*打印命令行args out*/
对于(i=0;i

我有没有办法实现这种分级报告?我需要为此实现某种特殊算法吗?

如果您对数组进行排序并使用for循环,那么您将几乎完全获得所需的输出:

China 
    Cycling first
    Swimming second
 Indonesia 
    Swimming third
 New Zealand 
    Cycling second
    Swimming first
代码

#包括
#包括
#定义最大尺寸10
int main(int argc,字符**argv){
char*a[][4]={{“体育”、“性别”、“国家”、“奖牌”},
{“自行车”、“女子”、“中国”、“第一”},
{“游泳”、“女子”、“中国”、“第二”},
{“游泳”、“女子”、“印度尼西亚”、“第三”},
{“自行车”、“女子”、“新西兰”、“第二”},
{“自行车”、“女子”、“新西兰”、“第三”},
{“游泳”、“女子”、“新西兰”、“第一”};
整数计数,起始索引,i;
int列[MAXSIZE];
/*在数组中存储命令行参数*/
起始指数=1;
计数=0;
对于(i=0;ifor(int j=1;j如果您对数组进行排序并使用for循环,那么您将几乎完全获得所需的输出:

China 
    Cycling first
    Swimming second
 Indonesia 
    Swimming third
 New Zealand 
    Cycling second
    Swimming first
代码

#包括
#包括
#定义最大尺寸10
int main(int argc,字符**argv){
char*a[][4]={{“体育”、“性别”、“国家”、“奖牌”},
{“自行车”、“女子”、“中国”、“第一”},
{“游泳”、“女子”、“中国”、“第二”},
{“游泳”、“女子”、“印度尼西亚”、“第三”},
{“自行车”、“女子”、“新西兰”、“第二”},
{“自行车”、“女子”、“新西兰”、“第三”},
{“游泳”、“女子”、“新西兰”、“第一”};
整数计数,起始索引,i;
int列[MAXSIZE];
/*在数组中存储命令行参数*/
起始指数=1;
计数=0;
对于(i=0;i对于(int j=1;j),这可能会给你一些想法。函数需要考虑列的深度(1, 2…)。如果这些列匹配,则增加计数。否则打印计数并将计数重置为1。<代码> COL[]/COD>是列的排序顺序。假定行0是列标题而不是数据。

void print_array(char str[][COLS][MAX_CH], int nrows, int ncols, int depth, int col[]) {
    int i, j;
    int count = 1;
    int width = 0;
    int same = 0;
    int wide = 0;
    int widest = 0;
    int order[COLS] = { 0};

    for (i = 1; i < nrows; i++) {
        for (j = 0; j < ncols; j++) {
            if ( strlen ( str[i][j]) > widest) {
                widest = strlen ( str[i][j]);//the widest element
            }
        }
    }
    widest += 2;

    for (i = 1; i < nrows; i++) {
        same = 0;
        for (j = 0; j < ncols; j++) {
            if ( j < depth) {
                if ( strcmp ( str[i][order[j] - 1], str[i - 1][order[j] - 1]) == 0) {
                    same++;//number of considered columns that are the same
                }
                else {
                    break;//stop on the first difference
                }
            }
        }
        if ( same == depth) {
            count++;//all considered columns are the same, add to count
        }
        else {
            if ( i > 1) {
                printf ( "%*d\n", widest - wide, count);//print the alligned count on last printed column but not on first iteration
            }
            count = 1;
            if ( same == 0 && i > 1) {
                printf ( "\n");//print extra newline when no columns are the same, after first iteration
            }
        }
        if ( count == 1) {//print the columns
            for (j = 0; j < ncols; j++) {
                if ( ( same - j) > 0 && ( strcmp (str[i][order[j] - 1], str[i - 1][order[j] - 1]) ==  0)) {
                    continue;//skip columns that are the same
                }
                if ( j < depth) {//the columns considered by depth
                    wide = strlen ( str[i][order[j] - 1]);
                    width = wide + j * 4;//for leading spaces
                    printf("%*s", width, str[i][order[j] - 1]);//print aligned column
                    if ( j < depth - 1) {
                        printf ( "\n");//print a newline except for last col so count can be printed later
                    }
                }
            }
        }
    }
    printf ( "%*d\n", widest - wide, count);//print count on last row
}
void print_数组(字符str[][COLS][MAX_CH],int nrows,int ncols,int depth,int col[])){
int i,j;
整数计数=1;
整数宽度=0;
int-same=0;
int宽=0;
int最宽=0;
整数阶[COLS]={0};
对于(i=1;i最宽){
最宽=strlen(str[i][j]);//最宽的元素
}
}
}
最宽+=2;
对于(i=1;i1){
printf(“%*d\n”,最宽-最宽,count);//在上次打印的列上打印所有的计数,但在第一次迭代时不打印
}
计数=1;
如果(相同==0&&i>1){
printf(“\n”);//在第一次迭代后,如果没有相同的列,则打印额外的换行符
}
}
如果(count==1){//打印列
对于(j=0;j0&(strcmp(str[i][order[j]-1],str[i-1][order[j]-1])==0)){
continue;//跳过相同的列