第10回 基礎プログラミング II レポート課題 氏名:小関文紀 学籍番号:c1090638 語学クラス:中国語 1 コース(系):地域共創コース 1.筆記問題 A pstoreとは、Rubyの中で作成したデータを外部に存在するファイルへ保存するこ とで第三者も書き込むことが出来るようにしたデータベースのことである。注意 点としてはディレクトリを指定せずに公開すると、製作者が許可していない場所 にも書き込まれる点である。 2.プログラム問題 ホームページ http://roy/~c109063/advertise2/rubycgi/r2_cgi_best.html 作成したプログラム 個人発表広告ページの感想を書き込んでもらうプログラム #!/usr/koeki/bin/ruby require 'cgi' #CGIを使う require 'pstore' cgi = CGI.new("html4") db = PStore.new("bss2.db") #データをためる name = cgi["namae"] hyouka = cgi["poster"] comment = cgi["comment"] print("Content-type: text/html; charset=EUC-JP\n\n") print("\n") print("\n") print("\n") print("個人発表広告ページのアンケート\n") print("\n") print("

結果

\n") num_iine = 0 num_hutu = 0 num_hidoi = 0 open("syukeikekka.txt" , "r") do |read| while line = read.gets if /(\d+)\s+(\d+)\s+(\d+)/ =~ line num_iine = $1.to_i num_hutu = $2.to_i num_hidoi = $3.to_i end end end if hyouka == "いいね" num_iine += 1 elsif hyouka == "ふつー" num_hutu += 1 else num_hidoi += 1 end printf("

いいね %d 人

\n",num_iine) printf("

ふつー %d 人

\n",num_hutu) printf("

ひどい %d 人

\n",num_hidoi) db.transaction do if db == nil db[name] = Hash.new end db[name] = comment nikkuname = db.roots for shimei in nikkuname printf("

%s %s

\n",db[shimei] ,shimei) end end print("\n") print("\n") open("syukeikekka.txt","w") do |write| write.printf("%d\t%d\t%d\n",num_iine ,num_hutu ,num_hidoi) end 実行結果 人数と感想が表示されたため正しく機能していることがわかる。 考察 pstoreを使うことで評価を書き込んでもらえる事がわかった。 感想 最高傑作と言うことでどのようなプログラムを作るか迷ったが、アンケートが締 めのプログラムにふさわしいと思い今回のアンケートを作った。自由課題では今 回以上の作品を提案し、作成した。 CGIで興味のあるスクリプトは横山広樹君のプログラムである。 http://roy/~c109149/advertise2/rubycgi/fork.html 前回のプログラムからも分かるように、内容の質が非常に高い作品だからである ある。 参考文献 基礎プログラミング II 第10回 CGIことおぼえ 第 10 回目次 講義ノート http://roy/~madoka/2010/r2/10/ 基礎プログラミング II 第10回 CGIことおぼえ 「CGI で使うことのできるデー タベース」 (講義ノート) http://roy/~madoka/2010/r2/10/10_01_theme_01_pstore.html 共同学習者 後藤輝久