開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C++ (プログラミング言語)
- g++(コンパイラ)
C++実践プログラミング (スティーブ オウアルライン (著)、Steve Oualline (原著)、Steve Oualline(原著)、望月 康司(翻訳)、クイープ(翻訳) 、オライリー・ジャパン)のⅢ部(高度な型とクラス)の13章(シンプルなクラス)、13.9(プログラミング実習)、実習 13-5.を解いてみる。
その他参考書籍
- C++プログラミング入門 (グレゴリー サティア (著)、ダウグ ブラウン (著)、Gregory Satir (原著)、Doug Brown (原著)、望月 康司 (翻訳)、谷口 功 (翻訳)、オライリージャパン)
実習 13-5.
コード(BBEdit, Emacs)
sample234_5.cpp
#include <iostream>
#include <assert.h>
#include <string>
struct single_card {
std::string question;
std::string answer;
};
const int SIZE = 5;
class flash_card {
private:
int count;
struct single_card cards[SIZE];
public:
flash_card(single_card list[]);
// flash_card(const flash_card& old_flash_card)
// ~flash_card()
// operator = (const flash_card& old_flash_card)
const single_card& get_card();
void right();
void wrong();
bool done();
};
inline flash_card::flash_card(single_card list[])
{
for (count = 0; count < SIZE; ++count) {
cards[count].question = list[count].question;
cards[count].answer = list[count].answer;
}
count = 4;
}
inline const single_card& flash_card::get_card()
{
assert(count >= 0 && count < SIZE);
return cards[count];
}
inline void flash_card::right()
{
std::cout << "正解!\n";
--count;
}
inline void flash_card::wrong()
{
std::cout << "不正解\n";
struct single_card t = cards[count];
int i;
for (i = count; i > 0; --i) {
cards[i] = cards[i-1];
}
cards[i] = t;
}
inline bool flash_card::done()
{
if (count != -1)
return false;
std::cout << "全てのカードを覚えました\n";
return true;
}
int main()
{
single_card list[] = {{"a", "あ"},
{"e", "え"},
{"i", "い"},
{"u", "う"},
{"o", "お"}};
flash_card a_flash_card(list);
struct single_card card;
std::string answer;
while (!a_flash_card.done()) {
card = a_flash_card.get_card();
std::cout << "外国語(英語): " << card.question << '\n';
std::cout << "日本語を入力: ";
std::getline(std::cin, answer);
if (card.answer == answer)
a_flash_card.right();
else
a_flash_card.wrong();
}
return (0);
}
Makefile
#
# FSFのg++コンパイラ用のMakefile
#
CC=g++
CFLAGS=-g -Wall
all: sample234_5
sample234_5: sample234_5.cpp
${CC} ${CFLAGS} -o sample234_5 sample234_5.cpp
clean:
rm sample234_5
入出力結果(Terminal)
$ make && ./sample234_5 g++ -g -Wall -o sample234_5 sample234_5.cpp 外国語(英語): o 日本語を入力: お 正解! 外国語(英語): u 日本語を入力: お 不正解 外国語(英語): i 日本語を入力: い 正解! 外国語(英語): e 日本語を入力: い 不正解 外国語(英語): a 日本語を入力: あ 正解! 外国語(英語): u 日本語を入力: う 正解! 外国語(英語): e 日本語を入力: え 正解! 全てのカードを覚えました $
0 コメント:
コメントを投稿