V0.01 beat3
[FreeRecite.git] / Reciter.h
blob7926547acc214b0feddd17b9b759ac21585a34ab
1 /**
2 * FileName: Reciter.h.
3 * Used to define the class Reciter which is used to test or recite the words.
5 * Copyright (C) 2008 Kermit Mei (中文名:梅延涛).
6 * All Rights Reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation.
22 * Written by Kermit Mei <kermit.mei@gmail.com>
24 * Many of the ideas implemented here are from the author's experiment.
25 * But the dictionary's format coincide with the other word recite software
26 * to help the users get more available data. And the review times designed
27 * by means of the theory named Forgetting Curve which dicoveried by the
28 * German psychologist named Hermann Ebbinghaus(1850–1909).
30 **/
32 #ifndef RECITER_HPP
33 #define RECITER_HPP
35 #include <vector>
36 #include <string>
37 #include <cstdlib>
38 #include "WordList.h"
40 namespace freeRecite {
42 class Reciter
44 public:
45 Reciter()
46 :score(0),isRedo(false),wordList(0)
47 { /* Do Nothing Here! */ }
48 ~Reciter()
49 { /* Do Nothing Here! */ }
51 /**
52 * Load the words from the file, success return true.
53 * Whenever you want to use this class, you must call it first.
54 **/
55 bool loadWords(time_t taskID,const char *configDir);
57 //The amount of the words in this task.
58 unsigned capability() const;
60 //The number of the unremembered (or untested) words.
61 unsigned size() const;
63 /**
64 * Get the next word you should recite.
65 * Before you get it, you must test whether it is valid.
66 **/
67 const std::string &getWord() const;
69 //If return true, then the object is valid.
70 bool isValid()const;
72 //Do this test again with an random order.
73 bool redo();
75 //Add a word to the current task.
76 bool addWord(const std::string &word);
78 //Remove a word from the current task.
79 bool removeWord(const std::string &word);
81 //Get the number of the words which you answered right.
82 unsigned getScore() const;
84 /**
85 * Test with the result. The argument 'result' is true when the word
86 * which the user input is correct.
87 **/
88 void test(bool result);
90 protected:
91 //Reload the words from the file.
92 bool reload();
94 //Make words be random.
95 bool makeRandom();
97 unsigned score;
98 bool isRedo;
99 WordList *wordList;
100 std::vector<std::string> words;
101 std::string taskFileName;
104 class Random {
105 public:
106 ptrdiff_t operator() (ptrdiff_t max) {
107 double tmp = static_cast<double>(rand()) / static_cast<double>(RAND_MAX);
108 return static_cast<ptrdiff_t>(tmp * max);
112 inline
113 const std::string &Reciter::getWord() const {
114 return words[wordList->getNext()];
118 inline
119 unsigned Reciter::capability() const {
120 return words.size();
123 inline
124 unsigned Reciter::size() const{
125 if(wordList == 0)
126 return 0;
127 if(wordList->isValid())
128 return wordList->size();
129 else
130 return 0;
133 inline
134 bool Reciter::isValid() const {
135 if(wordList == 0)
136 return false;
137 else
138 return wordList->isValid();
141 inline
142 unsigned Reciter::getScore() const {
143 return score*100/words.size();
146 } //namespace freeRecite end
147 #endif