From 019ba6920279a0fff2adbb3f4d46ac35b6a908a1 Mon Sep 17 00:00:00 2001 From: kou029w Date: Tue, 28 Aug 2012 23:07:07 +0900 Subject: [PATCH] =?UTF-8?q?aimaina=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cpp/aimaina/Makefile | 12 ++ cpp/aimaina/README.md | 14 +++ cpp/aimaina/aimaina.c | 31 +++++ cpp/aimaina/aimaina.o | Bin 0 -> 1360 bytes cpp/aimaina/book.c | 239 +++++++++++++++++++++++++++++++++++++++ cpp/aimaina/book.h | 34 ++++++ cpp/aimaina/book.o | Bin 0 -> 7968 bytes cpp/aimaina/booklist.csv | 7 ++ cpp/aimaina/search.c | 90 +++++++++++++++ cpp/aimaina/search.h | 16 +++ cpp/aimaina/search.o | Bin 0 -> 2164 bytes cpp/aimaina/util.c | 71 ++++++++++++ cpp/aimaina/util.h | 15 +++ cpp/aimaina/util.o | Bin 0 -> 2476 bytes 14 files changed, 529 insertions(+) create mode 100644 cpp/aimaina/Makefile create mode 100644 cpp/aimaina/README.md create mode 100755 cpp/aimaina/aimaina.c create mode 100644 cpp/aimaina/aimaina.o create mode 100644 cpp/aimaina/book.c create mode 100644 cpp/aimaina/book.h create mode 100644 cpp/aimaina/book.o create mode 100755 cpp/aimaina/booklist.csv create mode 100644 cpp/aimaina/search.c create mode 100644 cpp/aimaina/search.h create mode 100644 cpp/aimaina/search.o create mode 100644 cpp/aimaina/util.c create mode 100644 cpp/aimaina/util.h create mode 100644 cpp/aimaina/util.o diff --git a/cpp/aimaina/Makefile b/cpp/aimaina/Makefile new file mode 100644 index 0000000..4a76752 --- /dev/null +++ b/cpp/aimaina/Makefile @@ -0,0 +1,12 @@ +OBJS = aimaina.o book.o search.o util.o + +aimaina:$(OBJS) + $(CC) -o $@ $^ + +.c.o: + $(CC) -c $< + +aimaina.o: aimaina.c util.h book.h +book.o: book.c util.h search.h book.h +search.o: search.c search.h +util.o: util.c diff --git a/cpp/aimaina/README.md b/cpp/aimaina/README.md new file mode 100644 index 0000000..0746d0a --- /dev/null +++ b/cpp/aimaina/README.md @@ -0,0 +1,14 @@ +蔵書管理プログラム aimaina +========================= + +できること +-------- +- 書籍情報の追加/検索/削除 +- 書籍情報を"booklist.csv"に記録 +- プログラミングIIの単位を取る + +ライセンス +-------- +©2012 [@kou029w](http://twitter.com/kou029w) - [MIT license][MIT] + +[MIT]: http://kou029w.appspot.com/mit-license.txt \ No newline at end of file diff --git a/cpp/aimaina/aimaina.c b/cpp/aimaina/aimaina.c new file mode 100755 index 0000000..c96c334 --- /dev/null +++ b/cpp/aimaina/aimaina.c @@ -0,0 +1,31 @@ +/* +aimaina.c +蔵書管理プログラム +(C)2012 B11T3074C, B11T3011K - MIT License +*/ + +#include +#include +#include +#include "util.h" +#include "book.h" + +int main(){ + char buff[1024]; + openlist("booklist.csv"); + for(;;){ + printf("追加[a] 検索[s] 終了[q] "); + input_line(buff, 1024); + strtolower(buff); + + if(strcmp(buff, "a") == 0){ + regist_data(); + }else if(strcmp(buff, "s") == 0){ + search(); + }else if(strcmp(buff, "q") == 0){ + savelist("booklist.csv"); + exit(0); + } + } + return 0; +} \ No newline at end of file diff --git a/cpp/aimaina/aimaina.o b/cpp/aimaina/aimaina.o new file mode 100644 index 0000000000000000000000000000000000000000..133aded0232b4eec978241297a4d5d281ed439bf GIT binary patch literal 1360 zcmb7EO=uHQ5T4XxtF}o5DYdw;2gOr@trb)d6p1V%B2uLg<8zJMCYZk_YYZNw3JFB4 zc=X_*h==0ILj@6PbMq*8@mj&!C>6X3DvjT~oyEAK;K00ZzL|O7zTNlsz5e!jqmL-G zi7>z!=Q%Lpu$}OO%>QDSq}CGjIW&o#_1Khhoto>#9-l}|PWqTrrU_0k} zX_mn^rP39*oGnaCLaE>Ne4R0=Kk4yjdKjQspOaBjWjpV9^`M?9?{`#WRv_TN8>hAr zWu&Bm^@rgLvi|3InFrwLqcd=}&EnkK58nd+Upr?tSFKy?CgiQTBRu<6;s-;Uhh}~K zd#59Xr(Vu_sXr{L*4)w;l$-T7M%^zNi4U-Zq^xFr#qZLOiPX2Esyjbk(0$$QI{!!C z_&@uGbX8B%)0e^J0f#%C&gEk9YA#!G zMI$+rGRdY2&G2;Ep+}tU8)qq0i-w{Def{^bm%EYQ1#fLY8s?uDIu?#LdX5kB-mtxc z_Y=k50tbO_I7wu*Fhsx?f%x{u0x%5Dd(8Q}Ky1A+3mgD1Nj?R{ejDe3QRGhovG1`s z5N9K{7l`Ea!zjw9~RyaJ|g@cb1cw5*PM<> z4Iy1*j=KNvPl9)q^}W!|`Csqm{LgmtLjis|z)uADXn^kta8h|YTOd^|IR!oyq)NCA z+$mDoLg|{Ta@m4I3Ri?%%oVRYWgVyUB~oQ)8vW{u?b=8>b~&8^+EvGsacWtJv3T6c Y&?yy%hQO%cnBacFaY|U;-La2;0%xVx#{d8T literal 0 HcmV?d00001 diff --git a/cpp/aimaina/book.c b/cpp/aimaina/book.c new file mode 100644 index 0000000..99b3bf3 --- /dev/null +++ b/cpp/aimaina/book.c @@ -0,0 +1,239 @@ +/* +book.c +蔵書管理のための関数群 +(C)2012 B11T3074C, B11T3011K - MIT License +*/ + +#include +#include +#include +#include "util.h" +#include "search.h" +#include "book.h" + +// リストの先頭アドレス +Bookdata *booklist_head = NULL; + +// キーボードから本のデータを入力 +void input_data(Bookdata *target){ + target->title = input_string("タイトル>", target->title); + target->author = input_string("著者>", target->author); + target->publisher = input_string("出版元>", target->publisher); + target->year = input_string("発行年>", target->year); + target->pages = input_string("ページ数>", target->pages); + target->isbn = input_string("ISBN>", target->isbn); + target->note = input_string("備考>", target->note); +} + +// 本のデータを表示 +void show_data(Bookdata *target){ + printf("タイトル:\t%s\n", target->title); + printf("著者:\t\t%s\n", target->author); + printf("出版元:\t\t%s\n", target->publisher); + printf("発行年:\t\t%s\n", target->year); + printf("ページ数:\t%s\n", target->pages); + printf("ISBN:\t\t%s\n", target->isbn); + printf("備考:\t\t%s\n", target->note); +} + +// 本のリストからデータを削除 +void delete_data(Bookdata *target){ + if(target->prev == NULL && target->next == NULL){ + // 最後の1冊の場合 + booklist_head = NULL; + }else if(target->prev == NULL){ + // 先頭の場合 + booklist_head = target->next; + (target->next)->prev = NULL; + }else if(target->next == NULL){ + // 末尾の場合 + (target->prev)->next = NULL; + }else{ + (target->next)->prev = target->prev; + (target->prev)->next = target->next; + } + + free(target->title); // タイトル + free(target->author); // 著者 + free(target->publisher); // 出版元 + free(target->year); // 発行年 + free(target->pages); // ページ数 + free(target->isbn); // ISBN + free(target->note); // 備考 + free(target); +} + +// リストの末尾にnewdataを追加 +void add_data(Bookdata *new_data){ + Bookdata *tail; + + new_data->next = NULL; + + if(booklist_head == NULL) { + // 最初の1冊の場合 + booklist_head = new_data; + new_data->prev = NULL; + return; + } + + // 末尾を検索 + for(tail = booklist_head; tail->next != NULL; tail = tail->next); + + // 末尾の後ろにつなぐ + tail->next = new_data; + new_data->prev = tail; +} + +// リストをすべて表示 +void show_all(){ + Bookdata *tail; + + puts("---"); + for(tail = booklist_head; tail != NULL; tail = tail->next){ + show_data(tail); + puts("---"); + } +} + +// リストをCSVで保存 +void savelist(char *filename){ + FILE *fp; + Bookdata *tail; + + // filenameが""ならば標準出力をオープンする + if(filename[0] == '\0'){ + fp = stdout; + }else{ + fp = fopen(filename, "w"); + } + + fputs("title, author, publisher, year, pages, isbn, note\n", fp); + for(tail = booklist_head; tail != NULL; tail = tail->next){ + fputs(tail->title, fp); // タイトル + fputs(", ", fp); + fputs(tail->author, fp); // 著者 + fputs(", ", fp); + fputs(tail->publisher, fp); // 出版元 + fputs(", ", fp); + fputs(tail->year, fp); // 発行年 + fputs(", ", fp); + fputs(tail->pages, fp); // ページ数 + fputs(", ", fp); + fputs(tail->isbn, fp); // ISBN + fputs(", ", fp); + fputs(tail->note, fp); // 備考 + fputs("\n", fp); + } + fclose(fp); +} + +// リスト(CSVファイル)を開く +int openlist(char *filename){ + char buff[7][1024]; + int i, j; + FILE *fp; + + // filenameが空なら標準入力をオープンする + if(filename[0] == '\0'){ + fp = stdin; + }else{ + fp = fopen(filename, "r"); + } + + if(fp == NULL){ + fprintf(stderr, "\"%s\"が開けません\n", filename); + return -1; + } + + // 1行目はスルー + fscanf(fp, "%[^\n]\n", buff[6]); + + while(EOF != fscanf(fp, "%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^\n]\n", + buff[0], buff[1], buff[2], buff[3], buff[4], buff[5], buff[6])){ + Bookdata book; + Bookdata *bp; + + for(i=0; i<7; i++){ + // ","の後のスペースを削除 + if(buff[i][0] == ' ') strcpy(buff[i], &buff[i][1]); + } + + book.title = strdup(buff[0]); + book.author = strdup(buff[1]); + book.publisher = strdup(buff[2]); + book.year = strdup(buff[3]); + book.pages = strdup(buff[4]); + book.isbn = strdup(buff[5]); + book.note = strdup(buff[6]); + bp = malloc(sizeof(Bookdata)); + *bp = book; + add_data(bp); + } + fclose(fp); + return 0; +} + +// bookを初期化 +void init_data(Bookdata *book){ + book->title = strdup(""); + book->author = strdup(""); + book->publisher = strdup(""); + book->year = strdup(""); + book->pages = strdup(""); + book->isbn = strdup(""); + book->note = strdup(""); +} + +// 追加する +void regist_data(){ + Bookdata tmp; + Bookdata *tail; + init_data(&tmp); + input_data(&tmp); + tail = malloc(sizeof(Bookdata)); + *tail = tmp; + add_data(tail); +} + +// 検索する +void search(){ + char buff[1024]; + char pattern[1024]; + Bookdata *tail; + Bookdata *matches[1024]; + int count; + int i; + + printf("/"); + input_line(pattern, 1024); + + count = 0; + for(tail = booklist_head; tail != NULL; tail = tail->next){ + if(smatch(pattern, tail->title) + |smatch(pattern, tail->author) + |smatch(pattern, tail->publisher) + |smatch(pattern, tail->note)){ + puts("---"); + matches[count++] = tail; + show_data(tail); + } + } + + if(count <= 0) return; // 見つからないので、戻る + puts("---"); + printf("削除[d] 戻る[x] "); + input_line(buff, 1024); + strtolower(buff); + + if(strcmp(buff, "d") == 0){ + printf("削除します。よろしいですか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V=JT09 z!~AOI&oM7#-bEa9x*5qpLjXmCPxhX}-5JA7S3iJkGp@`6tX@Wd0R#B#QdI zJ{`V@kMi&T>k)$s@r=Qa$|I<|px0TCGe>hlr~V(Ym^f^@@KCv*?Y(U8CXT0r$Jft# zH#R>NP@xwDA6r;I%<*=zz5~flqmf8?q>Z-j{h`d5GBel9K74XUv%(@gCBA5 zsDt0{;DZkSfP>%Y;KL4{i{O*vr)`nU|Lx#kIQVxRyxhTWclclF;OiayxTF1QhhFnn z*3>mb{N;g2K-KvlVg7JM?G9Gd;a_8st(w0)R27PZSh%IOc00Z~MEn(@K)Hthc7;Nk zKTuxIYSBDURi*jSL0I#L1NVf;9RK_Cha=^+4UsxDs;vvv0Hn#*U{!4xPDnUm8VGLp z2P?Mww>1Rn(T`}2sJRxZuO~k$!mZ(8pazkWReiATUTTTHBlYDCFsnv9wLxI08aipE z+;0t3BG$^9N;Mnxp=~reIW=K?H40X!DXOZhL73s{K!jxQ9;vOW-4Uu6^xQB4x;9%x7G} zxRmiK#;X~xVJu)=#+a9@D#~M?a+#-m<|(In%4?o-o2UHdDaU!rbH4JNuRQ1H(~4Ch oHEKf1%PR+#ue!@u-Q}z9@>O^Fs=NFG&8N$M(B`IeN)v|iUrTUIGXMYp literal 0 HcmV?d00001 diff --git a/cpp/aimaina/booklist.csv b/cpp/aimaina/booklist.csv new file mode 100755 index 0000000..f343184 --- /dev/null +++ b/cpp/aimaina/booklist.csv @@ -0,0 +1,7 @@ +title, author, publisher, year, pages, isbn, note +脳と仮想, 茂木健一郎, 新潮社, 2007-2009, 264, ISBN9784101299525, +思考の整理学, 外山滋比古, 筑摩書房, 1986-2011, 223, ISBN4480020470, +恐山, 南直哉, 新潮社, 2012, 207, ISBN9784106104640, +電気回路, 小沢孝夫, 昭晃堂, 1996, 198, ISBN9784785611910, +電気電子工学概論, 酒井善雄, 丸善, 1995, 236, ISBN9784621040935, +アナログ電子回路, 藤井信生, 昭晃堂, 1984-2011, 255, ISBN4785611405, diff --git a/cpp/aimaina/search.c b/cpp/aimaina/search.c new file mode 100644 index 0000000..178b0d7 --- /dev/null +++ b/cpp/aimaina/search.c @@ -0,0 +1,90 @@ +/* +search.c +検索するための関数群 +(C)2012 B11T3074C, B11T3011K - MIT License +*/ + +#include +#include +#include "search.h" + +// stringにpatternが含まれていれば1、それ以外で0を返す +int match(char *pattern, const char *string){ + const char *p; + size_t pat_len = strlen(pattern); + for(p=string; *p; p++){ + if(!strncmp(pattern, p, pat_len)){ + return 1; + } + } + + return 0; +} + +// 破壊的AND-match : "pattern1 pattern2" => "pattern1" AND "pattern2" +int andmatch(char *pattern, const char *string){ + char *p; + int match_flag = 1; + + for(p = strtok(pattern, " "); p != NULL; p = strtok(NULL, " ")){ + match_flag &= match(p, string); + } + + return match_flag; +} + +// 破壊的OR-match : "pattern1|pattern2" => "pattern1" OR "pattern2" +int ormatch(char *pattern, const char *string){ + char *p; + int match_flag = 0; + + for(p = strtok(pattern, "|"); p != NULL; p = strtok(NULL, "|")){ + match_flag |= match(p, string); + } + + return match_flag; +} + +// 再帰可能なstrtok +char *strtok_r(char *str, const char *delim, char **saveptr){ + size_t len; + char *head; + + if(str == NULL){ + head = *saveptr; + }else{ + head = str; + } + len = strcspn(head, delim); + if(len == 0){ + head += strspn(head, delim); + len = strcspn(head, delim); + } + if(*head == '\0'){ + return NULL; + } + + *saveptr = head + len + strspn(head + len, delim); + *(head + len) = '\0'; + + return head; +} + +// Smart-match : 破壊的でない +// "pattern1 pattern2|pattern3" => "pattern1" AND ("pattern2" OR "pattern3") +int smatch(const char *pattern, const char *string){ + char *p; + char *nextp; + int match_flag; + char *tmp; + + tmp = strdup(pattern); + match_flag = 1; + for(p = strtok_r(tmp, " ", &nextp); p != NULL; p = strtok_r(NULL, " ", &nextp)){ + match_flag &= ormatch(p, string); + if(match_flag == 0) break; + } + free(tmp); + + return match_flag; +} diff --git a/cpp/aimaina/search.h b/cpp/aimaina/search.h new file mode 100644 index 0000000..d93fc12 --- /dev/null +++ b/cpp/aimaina/search.h @@ -0,0 +1,16 @@ +/* +search.h +検索するための関数群 +(C)2012 B11T3074C, B11T3011K - MIT License +*/ + +#ifndef SEARCH_H +#define SEARCH_H + +int match(char *pattern, const char *string); +int andmatch(char *pattern, const char *string); +int ormatch(char *pattern, const char *string); +char *strtok_r(char *str, const char *delim, char **saveptr); +int smatch(const char *pattern, const char *string); + +#endif \ No newline at end of file diff --git a/cpp/aimaina/search.o b/cpp/aimaina/search.o new file mode 100644 index 0000000000000000000000000000000000000000..72511b9d12de84ff11743ed324523acca99fbae7 GIT binary patch literal 2164 zcmbVNO=uHA6rO0Ssn%}&uVOKx2qKXR3I(zLE)HT)#G)5nOPX5I{*We6#Xw72aajvJ z=-q>;H$8d~tyJ6I%t>+x60|}BD!o}tZT-HP+5QA8>LYL8y!USIs3x7Io}^`FcX-MsRm1h z*hsKMbkX?YIMI$o{A%kJ6L6e``TRVl9r4~D82%DJ(d_(Kv(prBXo=}SalVl0D`)sQ zE1;{OT(5tXdg*?aJzr|1n;MsH zGB3TP84pg9rF5C1 z@O)-w1~6RV*;BITP0ht2YpXEzL$V(mMQUmSZH$lDV``>Hci3+zFS}D}Z1wIV*-N3{ zr0&;?jBR`EM#khT=Ap>`{WPGsrp*5`&sUOwmX$&<5nL}Q(-%4 z#pi()W>XfP;7o8ORm8Ki`e25{EvA9++oJ?-o)v-4%0OlL^56hE+kw~?=-?C*KvE=8 zj#O0MUUF;|K3gT^9fwx^)Vv!y{9E32VC{dwy9eH=;eGoDfndwS z-*MsKZV7+W7hClZEbf!=LleJi;(f3d;&rfVU?;H_6|f&+=?@t{3cD8mC0P8bZ9K3i z;hiSF(!@WZFY-R<{;J=?lE;1-_e}f{>}vSyVewUjAHY_i2mhXaZ2T%-h>)ky3XJ`G zm;XZ+YIea=+MpKj%Z3j~;lwC0DDhf@@1{=@6~gmD{Eop-(+AeB*Gb$rc*fwouT*_A z|Bk_*8Qg(yAdG~lEAr15`N1NeH^uzZ6Dg*N)1<;VEYi Q5M%ZVTk1d88oY-11>Vim5dZ)H literal 0 HcmV?d00001 diff --git a/cpp/aimaina/util.c b/cpp/aimaina/util.c new file mode 100644 index 0000000..ec8b175 --- /dev/null +++ b/cpp/aimaina/util.c @@ -0,0 +1,71 @@ +/* +util.c +文字列操作のための関数群 +(C)2012 B11T3074C, B11T3011K - MIT License +*/ + +#include +#include +#include +#include + +// 文字列版tolower() +char *strtolower(char *s){ + char *p; + + for(p=s; *p != EOF; p++){ + *p = tolower(*p); + } + return s; +} + +// 1行入力 +void input_line(char *buff, size_t size){ + char *p; + if(fgets(buff, size, stdin) == NULL){ + exit(1); + } + // 改行コードを削除 + for(p=buff; *p != '\n'; p++); + *p = '\0'; +} + +// "y", "n"で選択("n"ならば0を返す) +int select_y(char ch){ + char buff[1024]; + + for (;;){ + input_line(buff, 1024); + strtolower(buff); + + if (strcmp(buff, "y") == 0){ + return 1; + }else if (strcmp(buff, "n") == 0){ + return 0; + }else{ + switch(ch){ + case 'y': + case 'Y': + return 1; + case 'n': + case 'N': + return 0; + } + fputs("yまたはnを入力してください\n", stderr); + } + } +} + +// 文字列を入力(defstr:入力がないときデフォルトで返す文字列) +char *input_string(char *prompt, char *defstr){ + char buff[1024]; + + printf("%s", prompt); + input_line(buff, 1024); + if(buff[0] != '\0'){ + // 空でない場合 + free(defstr); + return strdup(buff); + }; + return defstr; +} diff --git a/cpp/aimaina/util.h b/cpp/aimaina/util.h new file mode 100644 index 0000000..be347c7 --- /dev/null +++ b/cpp/aimaina/util.h @@ -0,0 +1,15 @@ +/* +util.h +文字列操作のための関数群 +(C)2012 B11T3074C, B11T3011K - MIT License +*/ + +#ifndef UTIL_H +#define UTIL_H + +char *strtolower(char *s); +void input_line(char *buff, size_t size); +int select_y(char ch); +char *input_string(char *prompt, char *defstr); + +#endif \ No newline at end of file diff --git a/cpp/aimaina/util.o b/cpp/aimaina/util.o new file mode 100644 index 0000000000000000000000000000000000000000..f31f4c0fd60b105da36e01d757154f9335e7ab09 GI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