構造体の配列ごとに構造体の配列をソートしようとしています。すなわち、私は が構造の各メンバーによってソートされた1つのリストを印刷したいと思っています。 構造体のメンバが整数の場合は問題ありません。しかし、メンバーの1人は構造体の別の配列である です。また、その構造体の各メンバーによって全体の混乱をソートしたいと考えています。コードは次のとおりです。構造体の配列内で構造体の配列をソートする
#define PROPHET_COUNT 9000
#define MAX_FAITH_COUNT 600
typedef struct s_ProphetStat {
int precursorScore;
int cassandraScore;
int prophetId;} prophetStat;
typedef struct s_FaithStat{
int precursorScore;
int cassandraScore;
int faithId;
prophetStat ProphetStat[PROPHET_COUNT]; } faithStat;
void fauxScoringFunction(faithStat *FaithStat)
{
for (int faithIndex = 0; faithIndex < MAX_FAITH_COUNT; ++faithIndex){
for (int prophetIndex = 0; prophetIndex < PROPHET_COUNT; ++prophetIndex){
int randomNumber = rand();
FaithStat[faithIndex].ProphetStat[prophetIndex].precursorScore += randomNumber;
FaithStat[faithIndex].ProphetStat[prophetIndex].cassandraScore += randomNumber;
FaithStat[faithIndex].precursorScore += randomNumber;
FaithStat[faithIndex].cassandraScore += randomNumber; }}
}
typedef int (*compfn)(const void*, const void*);`enter code here`
int compareFaithPrecursorScores(faithStat *faithA, faithStat *faithB){
if (faithA->precursorScore > faithB->precursorScore) return 1; if (faithA->precursorScore < faithB->precursorScore) return -1; return 0; }
int compareFaithCassandraScores(faithStat *faithA, faithStat *faithB) {
if (faithA->cassandraScore > faithB->cassandraScore) return 1; if (faithA->cassandraScore < faithB->cassandraScore) return -1; return 0; }
int cannotFigureOut(...) { return 0; }
void fakemain(void)
{
faithStat *FaithStat = (faithStat *) calloc(MAX_FAITH_COUNT, sizeof(faithStat));
fauxScoringFunction(FaithStat);
// sort by cumulative precursorScore for each faith
qsort(FaithStat, MAX_FAITH_COUNT, sizeof(faithStat *), (compfn) compareFaithPrecursorScores);
// print results();
// sort by cumulative precursorScore for each faith
qsort(FaithStat, MAX_FAITH_COUNT, sizeof(faithStat *), (compfn) compareFaithCassandraScores);
// print results()
// sort by prophet precursor score
qsort(FaithStat, MAX_FAITH_COUNT * PROPHET_COUNT, sizeof(faithStat *), (compfn) cannotFigureOut);
}
これは、私が作成しようとしている "cannotFigureOut()"比較関数です。 (私はVS2010 C++(私の決定ではない)を使ってCコードをコンパイルしていますので、厄介なcallocキャストがあります)。
編集:単純化しようとして、比較機能を拒否しました。これを修正しました。また、 編集:私は重要な情報を省きました:預言者のセットは、信仰毎に同じです。だから私がしたいのは、 累計の先験譜(そして、預言者の累積的なカサンドラの得点で別々に)で並べ替えることです。それは次のとおりです:Prophet [0] cumulativeScore =(Faith [0] .Prophet [0] .precursorScore + (Faith [1] .ProphetScore ... Faith [MAX_FAITH_COUNT - 1] .Prophet [0]。 precursorScore);
ご質問は明確ではないだろうように
あなたは、単位の信仰値を今回の合計スコアを保持するために、同じ
prophetStat
構造を使用し、ないかもしれません。あなたは例えば次のように並べ替えることができますか? 'faithStat.prohetStat [0] .precursorScore'、' faithStat.prohetStat [0] .cassandraScore'、 'faithStat.prohetStat [1] .precursorScore'などには何がありますか? –はい、後者です:それぞれの信仰はそれぞれの預言者に異なるスコアを与えますが、それらはすべて同じ預言者リストを共有します – PaeneInsula