アルゴリズムとデータ構造:ソート、サーチの数値以外への応用
ここまでに出て来たソート、サーチのプログラムは全て数値のみを対象として
操作されていたが、もちろん配列でも同じように書けるよ的な応用。
本の検索がお題。
[book_seach.c]
#include <stdio.h> #include <stdlib.h> #define N 5 typedef struct { char *title; char *author; int bookID; int available; } book; book *bookarray[N]; void initdata(void){ int n; for(n = 0; n < N; n++){ bookarray[n] = (book*)malloc(sizeof(book)); } bookarray[0]->title = "book0"; bookarray[1]->title = "book1"; bookarray[2]->title = "book2"; bookarray[3]->title = "book3"; bookarray[4]->title = "book4"; bookarray[0]->author = "author0"; bookarray[1]->author = "author1"; bookarray[2]->author = "author2"; bookarray[3]->author = "author3"; bookarray[4]->author = "author4"; bookarray[0]->bookID = 1000; bookarray[1]->bookID = 502; bookarray[2]->bookID = 731; bookarray[3]->bookID = 628; bookarray[4]->bookID = 1; bookarray[0]->available = 1; bookarray[1]->available = 0; bookarray[2]->available = 0; bookarray[3]->available = 1; bookarray[4]->available = 1; } void cleanupdata(void){ int n; for (n = 0; n < N; n++) { free(bookarray[n]); } } void sortbook(int bottom, int top){ int lower, upper, div; book *temp; if (bottom >= top) { return; } div = bookarray[bottom]->bookID; for (lower = bottom, upper = top; lower < upper;) { while (/*lower < upper && */ bookarray[lower]->bookID < div) { lower++; } while (/*lower < upper && */ bookarray[upper]->bookID > div) { upper--; } if (lower < upper) { temp = bookarray[lower]; bookarray[lower] = bookarray[upper]; bookarray[upper] = temp; lower++; upper--; } sortbook(bottom, upper); sortbook(upper + 1, top); } } int searchbook(book *books[],int size, int key){ int left, mid, right; left = 0; right = size; while (left < right) { mid = (left + right) / 2; if (books[mid]->bookID < key){ left = mid + 1; } else { right = mid; } } if (books[left]->bookID == key) { return left; } return -1; } int main(void) { int key, position; initdata(); sortbook(0, N - 1); while (1) { printf("検索する本の番号を入力" "(0で終了):"); scanf("%d", &key); if (key == 0) break; position = searchbook(bookarray, N - 1, key); if (position != -1) { printf("-- 次の本が見つかりました --\n" "[タイトル]%s [著者]%s [管理番号]%d \n", bookarray[position]->title, bookarray[position]->author, bookarray[position]->bookID ); if (bookarray[position]->available != 0) { puts("この本は貸し出し可能です\n"); } else { puts("この本は貸し出し中です\n"); } } else { puts("お探しの本は見つかりませんでした\n"); } } cleanupdata(); return 0; }
ちょっとしか変わらないけどC++版。
C++に全くわかりませんが演算子のオーバーロードってよくあるの?
個人的には好きじゃないけど、世間では当たり前でしょ、って感じだったら慣れようか。
[book_seach.cpp]
#include <stdio.h> #include <stdlib.h> class CBook { public: char *title; char *author; int bookID; int available; bool operator<(CBook& book) { return bookID < book.bookID; } bool operator>(CBook& book) { return bookID > book.bookID; } operator int() { return bookID; } }; #define N 5 CBook *bookarray[N]; void initdata(void){ int n; for(n = 0; n < N; n++){ //bookarray[n] = (book*)malloc(sizeof(book)); bookarray[n] = new CBook; } bookarray[0]->title = "book0"; bookarray[1]->title = "book1"; bookarray[2]->title = "book2"; bookarray[3]->title = "book3"; bookarray[4]->title = "book4"; bookarray[0]->author = "author0"; bookarray[1]->author = "author1"; bookarray[2]->author = "author2"; bookarray[3]->author = "author3"; bookarray[4]->author = "author4"; bookarray[0]->bookID = 1000; bookarray[1]->bookID = 502; bookarray[2]->bookID = 731; bookarray[3]->bookID = 628; bookarray[4]->bookID = 1; bookarray[0]->available = 1; bookarray[1]->available = 0; bookarray[2]->available = 0; bookarray[3]->available = 1; bookarray[4]->available = 1; } void cleanupdata(void){ int n; for (n = 0; n < N; n++) { delete bookarray[n]; } } void sortbook(int bottom, int top){ int lower, upper; CBook *div, *temp; if (bottom >= top) { return; } div = bookarray[bottom]; for (lower = bottom, upper = top; lower < upper;) { while (*bookarray[lower] < *div) { lower++; } while (*bookarray[upper] > *div) { upper--; } if (lower < upper) { temp = bookarray[lower]; bookarray[lower] = bookarray[upper]; bookarray[upper] = temp; lower++; upper--; } sortbook(bottom, upper); sortbook(upper + 1, top); } } int searchbook(CBook *books[],int size, int key){ int left, mid, right; left = 0; right = size; while (left < right) { mid = (left + right) / 2; if (*books[mid] < key){ left = mid + 1; } else { right = mid; } } if (*books[left] == key) { return left; } return -1; } int main(void) { int key, position; initdata(); sortbook(0, N - 1); while (1) { printf("検索する本の番号を入力" "(0で終了):"); scanf("%d", &key); if (key == 0) break; position = searchbook(bookarray, N - 1, key); if (position != -1) { printf("-- 次の本が見つかりました --\n" "[タイトル]%s [著者]%s [管理番号]%d \n", bookarray[position]->title, bookarray[position]->author, bookarray[position]->bookID ); if (bookarray[position]->available != 0) { puts("この本は貸し出し可能です\n"); } else { puts("この本は貸し出し中です\n"); } } else { puts("お探しの本は見つかりませんでした\n"); } } cleanupdata(); return 0; }