名簿管理のプログラムを作成しています.
学校の課題なので,stdlib.hのqsort()を使わずに実装したいです.
c言語で引数に応じて構造体の要素でクイックソートを行いたいのですが,name,birthday,adress,commentのソートつまり文字列のソートがうまく行きませんどこがだめなのでしょうか?
修正してほしいです.
profile_deta_store[]という配列に構造体のデータを格納しています
該当のソースコード
c言語
ソースコードの一部
struct profile profile_deta_store[MAX_PROFILES]; struct date{ int y; int m; int d; }; struct profile { int id; char name[MAX_STR_LEN+1]; struct date birthday; char address[MAX_STR_LEN+1]; char *comment; }; void swap_profile(struct profile *a, struct profile *b) { struct profile tmp; tmp = *b; *b = *a; *a = tmp; } int compare_date(struct date *d1, struct date *d2) { if (d1->y != d2->y) return d1->y - d2->y; if (d1->m != d2->m) return d1->m - d2->m; return d1->d - d2->d; } void quick_sort(struct profile *pivot, int left, int right,int colum) { struct profile *p1,*p2; int i, j; if(left >= right) return; pivot=&profile_deta_store[left]; i = left; j = right; p1 = &profile_deta_store[i]; p2 = &profile_deta_store[j]; while(1) { switch(colum) { case 1: /* pivot以上のidを左側から探索 */ while (p1->id < pivot->id) { i++; p1 = &profile_deta_store[i]; } /* pivot以下のidを右側から探索 */ while (p2->id > pivot->id) { j--; p2 = &profile_deta_store[j]; } break; case 2: while (strcmp(p1->name, pivot->name)<0) { i++; p1 = &profile_deta_store[i]; } while (strcmp(p2->name, pivot->name)>0) { j--; p2 = &profile_deta_store[j]; } break; case 3: while (compare_date(&p1->birthday,&pivot->birthday)<0) { i++; p1 = &profile_deta_store[i]; } while (compare_date(&p2->birthday,&pivot->birthday)<0) { j--; p2 = &profile_deta_store[j]; } // fprint_profile_csv(stderr,p2); break; case 4: while (strcmp(p1->address, pivot->address) < 0) { i++; p1 = &profile_deta_store[i]; } while (strcmp(p2->address, pivot->address) > 0) { j--; p2 = &profile_deta_store[j]; } break; case 5: while (strcmp(p1->comment, pivot->comment) < 0) { i++; p1 = &profile_deta_store[i]; } while (strcmp(p2->comment, pivot->comment) > 0) { j--; p2 = &profile_deta_store[j]; } break; default: fprintf(stderr,"%%S:error: Please enter a number between 1 and 5.\n"); return ; } if (i >= j) break; swap_profile(p1,p2); i++; j--; } quick_sort(p1, left, i-1 ,colum); quick_sort(p2, j+1 , right,colum); }
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。

0 コメント