2012-02-26 36 views
0

構造体の配列ごとに構造体の配列をソートしようとしています。すなわち、私は が構造の各メンバーによってソートされた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);

+0

ご質問は明確ではないだろうように

あなたは、単位の信仰値を今回の合計スコアを保持するために、同じprophetStat構造を使用し、ないかもしれません。あなたは例えば次のように並べ替えることができますか? 'faithStat.prohetStat [0] .precursorScore'、' faithStat.prohetStat [0] .cassandraScore'、 'faithStat.prohetStat [1] .precursorScore'などには何がありますか? –

+0

はい、後者です:それぞれの信仰はそれぞれの預言者に異なるスコアを与えますが、それらはすべて同じ預言者リストを共有します – PaeneInsula

答えて

0

まず、return (a,b);はちょうどbを返し、あなたのcompareFaithPrecursorScorescompareFaithCassandraScoresの両方で、あなたはおそらく-,を交換したい最後のものに並べ替える必要がありますので、今、あなただけ、あなたのメインに割り当てられた信仰を持っています。前と同じ長さの同じFaithStatMAX_FAITH_COUNTでなく、MAX_FAITH_COUNT * PROPHET_COUNT

あなたの署名はまだ同じであるので、は、あなただけの、以前のように、2 faithStatsを比較:

int cannotFigureOut(faithStat *faithA, faithStat *faithB){ 
.... } 

(編集:) [OK]を、ので、(あなたの明確化の後に)それは累積は全く、それは誤解だと呼ばれていません。あなたはの合計をとしました。あなたの最後の追加は、別の配列を並べて、今度は9000人の預言者(そして600人の信仰ではない)を並べ替えたいと言います。それぞれの預言者の得点は、すべての信仰における彼の得点の合計になります。預言者の配列を作成した後に記入して、いつものように並べ替える機能を作ってください。署名が

int cannotFigureOut(prophetStat *prophetA, prophetStat *prophetB){ 
.... } 
+0

投稿の比較機能を修正しました。 – PaeneInsula

+0

@ user994179私は自分の答えを更新しました。 –

+0

ブラボー...ありがとう。 – PaeneInsula