Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-附加名称的排序编号_C_Arrays_Function_Sorting - Fatal编程技术网

C-附加名称的排序编号

C-附加名称的排序编号,c,arrays,function,sorting,C,Arrays,Function,Sorting,我是一个编程初学者,有人能帮我解决这个问题吗? 这段代码是为公牛和奶牛的游戏,我需要做名人堂的猜测。名人堂还需要有玩家的名字才能进入 for (b = 0; b < ch - 1; b++) { for (c = 0; c < ch - b - 1; c++) { if (array[c] > array[c + 1]) { sort = array[c]; array[c] = array[c + 1];

我是一个编程初学者,有人能帮我解决这个问题吗? 这段代码是为公牛和奶牛的游戏,我需要做名人堂的猜测。名人堂还需要有玩家的名字才能进入

for (b = 0; b < ch - 1; b++) {
    for (c = 0; c < ch - b - 1; c++) {
        if (array[c] > array[c + 1]) {
            sort = array[c];
            array[c] = array[c + 1];
            array[c + 1] = sort;
            //sorting the guessing result in ascending order
        }
    }
}
//printing to a file
printf("Sorted list in ascending order:\n");

for (b = 0; b < ch; b++)
    printf("%d\n", array[b]);

fprintf(file, "Sorting: %s %d\n", user_name, ch);
fclose(file);
(b=0;b数组[c+1]){ 排序=数组[c]; 数组[c]=数组[c+1]; 数组[c+1]=排序; //按升序排序猜测结果 } } } //打印到文件 printf(“按升序排序的列表:\n”); 对于(b=0;b对“带有附加名称的数字”进行排序的一种简单方法是将数字和名称放入一个
结构
中,并拥有一个该
结构的数组
。然后简单地使用标准的
qsort
函数对数组进行排序

可能是这样的:

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

// Define a type to hold the score together with the name of the player
typedef struct
{
    char name[42];
    int score;
} PlayerStats;

// Make a compare function to be used by qsort
int cmpfunc (const void * a, const void * b){
    const PlayerStats* pA = a;
    const PlayerStats* pB = b;
    if (pA->score > pB->score) return -1;
    if (pA->score < pB->score) return 1;
    return 0;
}

// A function for printing the players and their score
void print_stats(PlayerStats *ps, size_t n)
{
    for(size_t i=0; i<n; ++i) printf("%s : score=%d\n", ps[i].name, ps[i].score);
}

int main(){
    // Make an array of players with scores.
    PlayerStats player_stats[3] = {{"p1", 17}, {"p2", 9}, {"p3", 42}};
    size_t numElements = sizeof player_stats / sizeof player_stats[0];
    size_t sizeElement = sizeof player_stats[0];

    printf("Unsorted:\n");
    print_stats(player_stats, numElements);

    // Use the standard qsort for sorting the array
    qsort(player_stats, numElements, sizeElement, cmpfunc);

    printf("Sorted:\n");
    print_stats(player_stats, numElements);

    return 0;
}
请在此处尝试:

如果需要升序排序,只需更改比较函数,如下所示:

int cmpfunc (const void * a, const void * b){
    const PlayerStats* pA = a;
    const PlayerStats* pB = b;
    if (pA->score > pB->score) return 1;   // Change -1 to 1
    if (pA->score < pB->score) return -1;  // Change 1 to -1
    return 0;
}
int-cmpfunc(常数无效*a,常数无效*b){
常数PlayerStats*pA=a;
常量PlayerStats*pB=b;
如果(pA->score>pB->score)返回1;//将-1更改为1
如果(pA->scorescore)返回-1;//将1更改为-1
返回0;
}

能否在问题正文中更明确地描述您的问题?此外,请尝试以下操作:
int cmpfunc (const void * a, const void * b){
    const PlayerStats* pA = a;
    const PlayerStats* pB = b;
    if (pA->score > pB->score) return 1;   // Change -1 to 1
    if (pA->score < pB->score) return -1;  // Change 1 to -1
    return 0;
}