From 512a5c876a41e95fe7e1f357c8db9ddd51f0b8fa Mon Sep 17 00:00:00 2001 From: Kermit Mei Date: Fri, 19 Dec 2008 03:40:51 +0800 Subject: [PATCH] add some header --- src/core/Scanner.cpp | 98 ++++++++++++++++++++++++++++++++++++++ src/core/Scanner.h | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/core/Tester.cpp | 14 ++++++ src/core/Tester.h | 62 ++++++++++++++++++++++++ 4 files changed, 304 insertions(+) create mode 100644 src/core/Scanner.cpp create mode 100644 src/core/Scanner.h create mode 100644 src/core/Tester.cpp create mode 100644 src/core/Tester.h diff --git a/src/core/Scanner.cpp b/src/core/Scanner.cpp new file mode 100644 index 0000000..faa421a --- /dev/null +++ b/src/core/Scanner.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include "ConfigHolder.h" +#include "Reciter.h" +#include "WordList.h" +#include "Scanner.h" + + +namespace freeRecite { + +bool Scanner::loadWords(time_t taskID,bool Random) { + struct tm * timeinfo; + char buffer[20]; + timeinfo = localtime(&taskID); + strftime(buffer,20,"%Y%m%d%H%M%S.tkwd",timeinfo); + + taskFileName = configHolder.tasksDir() + buffer; + + std::ifstream ifs(taskFileName.c_str()); + if(!ifs.is_open()) + return false; + std::string tmpWord; + while(ifs.good()) { + std::getline(ifs,tmpWord); + if(!tmpWord.empty()) + words.push_back(tmpWord); + } + + //If Random is false, then do not call makeRandom(). + if(Random) + if(!makeRandom()) + return false; + + wordList = new WordList(words.size()); + return true; +} + +//Add a word to the current task. +bool Scanner::add(const std::string &word) { + words.push_back(word); + wordList->add(words.size()); + return save(); +} + +//Remove a word from the current task. +bool Scanner::remove(const std::string &word) { + unsigned index = 0; + while( index < words.size() ) { + if(words[index] == word) + break; + else + ++index; + } + if(index == words.size()) //Can't find this word. + return false; + + words[index] = words[words.size()-1]; + words.pop_back(); + wordList->remove(words.size()); + return save(); +} + +bool Scanner::save() { + std::set wordSet; + for(size_t i = 0; i < words.size(); ++i) + if( !wordSet.insert(words[i]).second) + return false; + + std::ofstream ofs(taskFileName.c_str()); + if(!ofs.is_open()) + return false; + + std::set::iterator itr = wordSet.begin(); + while(itr != wordSet.end()) { + if(!ofs.good()) + return false; + else{ + ofs << *itr << std::endl; + ++itr; + } + } + ofs.close(); + return true; +} + +bool Scanner::makeRandom() { + if(words.empty()) + return false; + Random rd; + srand(time(0)); + std::random_shuffle(words.begin(),words.end(),rd); + return true; +} + + +} //End of namespace freeRecite diff --git a/src/core/Scanner.h b/src/core/Scanner.h new file mode 100644 index 0000000..d891880 --- /dev/null +++ b/src/core/Scanner.h @@ -0,0 +1,130 @@ +/** + * FileName: Scanner.h. + * Used to define the class Reciter which is used to test or recite the words. + * + * Copyright (C) 2008 Kermit Mei (中文名:梅延涛). + * All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation. + * + * Written by Kermit Mei + * + * Many of the ideas implemented here are from the author's experiment. + * But the dictionary's format coincide with the other word recite software + * to help the users get more available data. And the review times designed + * by means of the theory named Forgetting Curve which dicoveried by the + * German psychologist named Hermann Ebbinghaus(1850–1909). + * + **/ + +#ifndef SCANNER_H_ +#define SCANNER_H_ + +#include +#include +#include +#include "WordList.h" + +namespace freeRecite { + +//A functor to make the words save in random order. +class Random { +public: + ptrdiff_t operator() (ptrdiff_t max) { + double tmp = static_cast(rand()) / static_cast(RAND_MAX); + return static_cast(tmp * max); + } +}; + +class Scanner +{ +public: + Scanner() + :wordList(0) + { /* Do Nothing Here! */ } + ~Scanner() + { /* Do Nothing Here! */ } + + /** + * Load the words from the file, success return true. + * Whenever you want to use this class, you must call it first. + **/ + bool loadWords(time_t taskID,bool Random = true); + + //The amount of the words in this task. + unsigned capability() const; + + //Return the procent of the current task's progress. + unsigned size() const; + + /** + * Get the next word you should recite. + * Before you get it, you must test whether it is valid. + **/ + const std::string &getWord() const; + + //If return true, then the object is valid. + bool isValid()const; + + //Add a word to the current task. + bool add(const std::string &word); + + //Remove a word from the current task. + bool remove(const std::string &word); + + /** + * Test with the result. The argument 'result' is true + * when the word which the user input is correct. + **/ + virtual void test(bool result) = 0; + +protected: + //Save all the words in vector to the task's file. + bool save(); + + //Mank wors' order be random. + bool makeRandom(); + + WordList *wordList; + std::vector words; + std::string taskFileName; +}; + +inline +const std::string &Scanner::getWord() const { + return words[wordList->getNext()]; +} + +inline +unsigned Scanner::capability() const { + return words.size(); +} + +inline +unsigned Scanner::size() const { + return wordList->size(); +} + +inline +bool Scanner::isValid() const { + if(wordList == 0) + return false; + else + return wordList->isValid(); +} + +} //namespace freeRecite end + +#endif //SCANNER_H_ diff --git a/src/core/Tester.cpp b/src/core/Tester.cpp new file mode 100644 index 0000000..e895d1a --- /dev/null +++ b/src/core/Tester.cpp @@ -0,0 +1,14 @@ +#include "Tester.h" + +namespace freeRecite { + +void Tester::test(bool result) { + if(result) { + if(wordList->pass() == 0) + ++score; + } else + wordList->lose(); +} + + +} //End of namespace freeRecite diff --git a/src/core/Tester.h b/src/core/Tester.h new file mode 100644 index 0000000..8a6952b --- /dev/null +++ b/src/core/Tester.h @@ -0,0 +1,62 @@ +/** + * FileName: Tester.h + * Used to define the class Tester which is used to test whether + * the words be remembered. + * + * Copyright (C) 2008 Kermit Mei (中文名:梅延涛). + * All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation. + * + * Written by Kermit Mei + * + * Many of the ideas implemented here are from the author's experiment. + * But the dictionary's format coincide with the other word recite software + * to help the users get more available data. And the review times designed + * by means of the theory named Forgetting Curve which dicoveried by the + * German psychologist named Hermann Ebbinghaus(1850–1909). + * + **/ + +#ifndef TESTER_H_ +#define TESTER_H_ + +#include "Scanner.h" + +namespace freeRecite { + +class Tester : public Scanner +{ +public: + Tester() + :score(0) + { /* Do Nothing Here! */ } + ~Tester() + { /* Do Nothing Here! */ } + + unsigned getScore() const; + void test(bool result); +protected: + unsigned score; +}; + +inline +unsigned Tester::getScore() const { + return score*100/words.size(); +} + +} //End of namespace freeRecite. + +#endif //TESTER_H_ -- 2.11.4.GIT