ラックナンバーリサーチの実行時間を新しい順に並べるコードを書いたのですが
もう少しカッコイイ、コードに書き直せますか。教えていただければありがたいです。
表示は正常に動きます。char dtfile[]="LACKNUM.DAT"からTIME_DATA lst[]にデータを
取り込むコードを示していただければ、いいのですが。
//jikan_sort4.c #include <stdio.h> #include <stdlib.h> #include <time.h> #define MAXCNT 100 #define NDATA ((sizeof(lst))/(sizeof(TIME_DATA))) //char dtfile[]="LACKNUM.DAT"; typedef struct // 構造体の宣言 { int tm_year; int tm_mon; int tm_mday; int tm_hour; int tm_min; int tm_sec; double score; }TIME_DATA; int main() { TIME_DATA lst[]= {{2017, 9,16, 8,18,23,10.6}, {2015,12, 7,19,59, 8, 8.0}, {2016, 1, 3, 1, 9,11, 7.5}, {2016, 4, 5,19,16, 8, 9.0}, {2015, 6, 4,19, 2,44, 7.5}, {2016, 8, 7,22,31,52, 9.3}, {2014, 1, 4,23,43,50,10.2}, {2016, 5,31,21, 9,53, 7.0}, {2014, 2,12,28,31,49,13.0}, {2016, 2,28, 2,27,20, 7.8}, {2015, 5, 2,16,43,32, 6.3}, {2017, 9,14,10,21,18, 6.0}, {2017, 9,16, 8,18,9, 8.6}}; int i; printf("NDATA:%d\n",NDATA); printf( "■並べ替え前\n" ); for(i=0; i<NDATA; i++){ printf("%4d年 %2d月 %2d日 %2d時 %2d分 %2d秒 \n所要時間(score):%.1f\n\n", // 構造体配列への読み出し lst[i].tm_year,lst[i].tm_mon,lst[i].tm_mday, lst[i].tm_hour,lst[i].tm_min,lst[i].tm_sec,lst[i].score); } // 並べ替え for ( int i=0; i<NDATA; i++ ) { for ( int j=i+1; j<NDATA; j++ ){ if (lst[i].tm_year*100+lst[i].tm_mon == lst[j].tm_year*100+lst[j].tm_mon){ if(lst[i].tm_mday*100+lst[i].tm_hour == lst[j].tm_mday*100+lst[j].tm_hour){ if(lst[i].tm_min == lst[j].tm_min){ if (lst[i].tm_sec < lst[j].tm_sec){ TIME_DATA temp = lst[i]; lst[i] = lst[j]; lst[j] = temp; } }else{ if(lst[i].tm_min < lst[j].tm_min){ TIME_DATA temp = lst[i]; lst[i] = lst[j]; lst[j] = temp; } } }else{ if(lst[i].tm_mday*100+lst[i].tm_hour < lst[j].tm_mday*100+lst[j].tm_hour){ TIME_DATA temp = lst[i]; lst[i] = lst[j]; lst[j] = temp; } } }else{ if (lst[i].tm_year*100+lst[i].tm_mon < lst[j].tm_year*100+lst[j].tm_mon){ TIME_DATA temp = lst[i]; lst[i] = lst[j]; lst[j] = temp; } } } } // 並べ替え後表示 printf( "■並べ替え後\n" ); for(i=0; i<NDATA; i++){ printf("%4d年 %2d月 %2d日 %2d時 %2d分 %2d秒 \n所要時間(score):%.1f\n\n", // 構造体配列への読み出し lst[i].tm_year,lst[i].tm_mon,lst[i].tm_mday, lst[i].tm_hour,lst[i].tm_min,lst[i].tm_sec,lst[i].score); } return 0; } /* C:\MinGW\users\chap09\kadai>gcc -I. -o jikan_sort4 jikan_sort4.c -Wall C:\MinGW\users\chap09\kadai>jikan_sort4 NDATA:13 ■並べ替え前 2017年 9月 16日 8時 18分 23秒 所要時間(score):10.6 2015年 12月 7日 19時 59分 8秒 所要時間(score):8.0 2016年 1月 3日 1時 9分 11秒 所要時間(score):7.5 2016年 4月 5日 19時 16分 8秒 所要時間(score):9.0 2015年 6月 4日 19時 2分 44秒 所要時間(score):7.5 2016年 8月 7日 22時 31分 52秒 所要時間(score):9.3 2014年 1月 4日 23時 43分 50秒 所要時間(score):10.2 2016年 5月 31日 21時 9分 53秒 所要時間(score):7.0 2014年 2月 12日 28時 31分 49秒 所要時間(score):13.0 2016年 2月 28日 2時 27分 20秒 所要時間(score):7.8 2015年 5月 2日 16時 43分 32秒 所要時間(score):6.3 2017年 9月 14日 10時 21分 18秒 所要時間(score):6.0 2017年 9月 16日 8時 18分 9秒 所要時間(score):8.6 ■並べ替え後 2017年 9月 16日 8時 18分 23秒 所要時間(score):10.6 2017年 9月 16日 8時 18分 9秒 所要時間(score):8.6 2017年 9月 14日 10時 21分 18秒 所要時間(score):6.0 2016年 8月 7日 22時 31分 52秒 所要時間(score):9.3 2016年 5月 31日 21時 9分 53秒 所要時間(score):7.0 2016年 4月 5日 19時 16分 8秒 所要時間(score):9.0 2016年 2月 28日 2時 27分 20秒 所要時間(score):7.8 2016年 1月 3日 1時 9分 11秒 所要時間(score):7.5 2015年 12月 7日 19時 59分 8秒 所要時間(score):8.0 2015年 6月 4日 19時 2分 44秒 所要時間(score):7.5 2015年 5月 2日 16時 43分 32秒 所要時間(score):6.3 2014年 2月 12日 28時 31分 49秒 所要時間(score):13.0 2014年 1月 4日 23時 43分 50秒 所要時間(score):10.2 C:\MinGW\users\chap09\kadai> */
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。