C言語でヒープソートがうまく実装できません。

リンク内容### 実現したいこと
ヒープソートが動作するようにする。

発生している問題・分からないこと

変数point_diffやGFの比較が実行されない

###J_result.csvの内容
Albirex,11,12,11,36,40
Antlers,14,10,10,43,34
Avispa,15,6,13,37,43
Bellmare,8,10,16,40,56
Cerezo,15,4,15,39,34
Consadole,10,10,14,56,61
F.C.Tokyo,12,7,15,42,46
F.Marinos,19,7,8,63,40
Frontale,14,8,12,51,45
Gamba,9,7,18,38,61
Grampus,14,10,10,41,36
Reds,15,12,7,42,27
Reysol,6,15,13,33,47
Sagan,9,11,14,43,47
Sanfrecce,17,7,10,42,28
SangaF.C.,12,4,18,40,45
Vissel,21,8,5,60,29
YokohamaFC,7,8,19,31,58

該当のソースコード

C

1#include <stdio.h>2#include <stdlib.h>3 4#define TEAM_NUM 185#define NAME_LENGTH 156#define DATA_LEN 1007 8/* チームスコア構造体 */9struct team_score{10 char name[NAME_LENGTH]; /* チーム名 */11 int win; /* 勝ち */12 int draw; /* 分け */13 int loss; /* 負け */14 int GF; /* 得点 */15 int GA; /* 失点 */16 int score; /* 勝点 */17 int point_diff; /* 得失点差 */18};19 20typedef struct team_score SC; /*「strcut score」を「SC」という型名で定義し直す */21 22/* 関数のプロトタイプ宣言 */23void read_data(FILE *fin, SC table[]);24void calc_score(SC *p_team);25void rank_score_heap(SC table[]);26void swap_SC(SC *p_team1, SC *p_team2);27void write_data(FILE *fout, SC table[]);28void chkheap(int nods, int nodf, SC table[]);29 30int main(void)31{32 /* 変数宣言 */33 FILE *fin; //読み込み用ファイルポインタ34 FILE *fout; //書き込み用ファイルポインタ35 SC table[TEAM_NUM]; /* 全チームの結果を格納するSC構造体配列 */36 int i;37 38 /* 処理内容 */39 /* 読み込み用ファイルのオープン */40 //if((fin = fopen("J_result_test.csv","r"))==NULL){ // test用データでも動作確認すること(確認済み) 41 if((fin = fopen("J_result.csv","r"))==NULL){ // 入力ファイルを開く42 printf("Can't open file\n");43 exit(1);44 }45 46 /* 書き込み用ファイルのオープン */47 fout = fopen("J_score3.txt","w"); // 出力ファイルを開く48 49 /* (1)スコアファイルの読み込み */50 read_data(fin, table);51 52 /* (2)スコアの計算 */53 for(i=0; i< TEAM_NUM; i++){54 calc_score(&table[i]);55 }56 57 /* (3)スコアに基づき順位付け */58 rank_score_heap(table);59 60 /* (4)順位順にスコアを書き込み */61 write_data(fout, table);62 63 /* 終了処理 */64 fclose(fin); /* 入力ファイルをクローズ */65 fclose(fout); /* 出力ファイルをクローズ */66 67 return(0);68}69 70/* スコアファイルの読み込み */71void read_data(FILE *fin, SC table[])72{73 int i = 0;74 char buff[DATA_LEN];75 while(fgets(buff,sizeof(buff),fin) != NULL){76 sscanf(buff,"%[^,],%d,%d,%d,%d,%d", table[i].name, &table[i].win, &table[i].draw, &table[i].loss, &table[i].GF, &table[i].GA);77 i++;78 }79}80 81/* 勝ち点&得失点差の計算用関数 */82void calc_score(SC *team)83{84 team->score = team->win*3 + team->draw;85 team->point_diff = team->GF - team->GA; 86}87 88/* スコアに基づき順位付け(ヒープソート)*/89void rank_score_heap(SC table[])90{91 int nods,nodf;92 nods = (TEAM_NUM/2) - 1;93 nodf = TEAM_NUM - 1;94 while(nods >= 0){95 chkheap(nods, nodf, table);96 nods = nods - 1;97 }98 while(nodf >= 1){99 swap_SC(&table[0],&table[nodf]);100 101 nodf = nodf - 1;102 chkheap(0,nodf,table);103 }104}105 106/*ヒープを生成するための関数*/107void chkheap(int nods,int nodf,SC table[]){108 int i = nods;109 int j = 2*i + 1;110 111 if(j <= nodf){112 if((j < nodf)&&(table[j].score > table[j+1].score)){113 j = j+1;114 }else if((j < nodf)&&(table[i].score == table[j].score) && (table[i].point_diff > table[j].point_diff)){115 j = j+1;116 }else if((j < nodf)&&(table[i].score == table[j].score) && (table[i].point_diff == table[j].point_diff) && (table[i].GF > table[j].GF)){117 j = j+1;118 }119 120 if((table[i].score == table[j].score) && (table[i].point_diff > table[j].point_diff)){121 swap_SC(&table[i],&table[j]);122 }else if((table[i].score == table[j].score) && (table[i].point_diff == table[j].point_diff) && (table[i].GF > table[j].GF)){123 swap_SC(&table[i],&table[j]);124 }else if(table[i].score > table[j].score){125 swap_SC(&table[i],&table[j]);126 }127 128 chkheap(j, nodf, table);129 } 130}131 132/* 構造体の中身を入れ替える関数 */133void swap_SC(SC *team1, SC *team2)134{135 SC temp;136 temp = *team1;137 *team1 = *team2;138 *team2 = temp;139}140 141/* 順位表の書き込み */142void write_data(FILE *fout, SC table[])143{144 int i;145 printf("順位 チーム名 勝ち点 得失点差\n");146 fprintf(fout,"順位 チーム名 勝ち点 得失点差\n");147 for(i = 0; i < TEAM_NUM; i++){148 /*ディスプレイ表示*/149 printf("%2d %12s %8d %8d\n", i+1 , table[i].name, table[i].score, table[i].point_diff);150 /*ファイルに記録*/151 fprintf(fout,"%2d %12s %8d %8d\n", i+1 , table[i].name, table[i].score, table[i].point_diff);152 }153}

このプログラム内のpoint_diffとGFを比較する部分が実行されません。

試したこと・調べたこと

上記の詳細・結果

様々変更はしようと試みたが、else ifからの部分が実行されない

補足

特になし

コメントを投稿

0 コメント