From 8ba3cf7f4af7aa1499926df1c4fdda9195f12df9 Mon Sep 17 00:00:00 2001 From: Kermit Mei Date: Tue, 23 Dec 2008 16:21:36 +0800 Subject: [PATCH] Split and Rtimes --- dict/FreeRecite/freeRecite.mgr | 2 +- src/core/Manager.cpp | 74 +++++++++++++++++++----------------------- src/core/Manager.h | 2 +- src/core/Reciter.cpp | 14 ++++++-- src/core/Reciter.h | 8 +++++ src/core/Scanner.h | 16 +++++++-- src/core/Tester.cpp | 9 +++-- src/core/Tester.h | 6 ++++ src/core/WordList.cpp | 6 ++-- src/core/WordList.h | 4 +-- src/ui/Cui.cpp | 14 ++++---- 11 files changed, 94 insertions(+), 61 deletions(-) diff --git a/dict/FreeRecite/freeRecite.mgr b/dict/FreeRecite/freeRecite.mgr index 573541a..15794e0 100644 --- a/dict/FreeRecite/freeRecite.mgr +++ b/dict/FreeRecite/freeRecite.mgr @@ -1 +1 @@ -0 +0,0 diff --git a/src/core/Manager.cpp b/src/core/Manager.cpp index 4420e35..e6cdd5d 100644 --- a/src/core/Manager.cpp +++ b/src/core/Manager.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -80,85 +79,80 @@ bool Manager::hasTask(time_t taskID) { bool Manager::createTask(const std::set &words, - const char *taskName, - unsigned maxLimit) { - + const char *taskName, unsigned maxLimit) { if(words.size()==0) return false; - std::cout << words.size() << std::endl; - unsigned tasksNum = words.size()/maxLimit + (words.size()%maxLimit==0?1:0); + unsigned tasksNum = words.size()/maxLimit + 1; maxLimit = words.size()/tasksNum; - unsigned index = 1; + std::string baseName; + std::string realName; + std::ofstream desFile; std::ostringstream os; + time_t curTime; struct tm * timeinfo; char buffer[20]; std::set::const_iterator itr = words.begin(); - std::ofstream desFile; - std::string baseName; - time(&curTime); - - std::cout << *itr < - if(taskName == 0) - baseName = buffer; - else { + if(taskName != 0) baseName = taskName; + + if(baseName.empty()){ + timeinfo = localtime(&curTime); + strftime(buffer,20,"%Y%m%d%H%M%S",timeinfo); + baseName = buffer; } - - for(int i = 1;i<=tasksNum; ++i) { - os.str(""); + for (int i = 1; i <= tasksNum; ++i) { //[0] memset(buffer,0,20); - + os.str(""); os << "_" << i; - baseName.append(os.str()); + if(tasksNum > 1) + realName = baseName + os.str(); + else + realName = baseName; if(curTime > maxTaskID) maxTaskID = curTime; else ++maxTaskID; - timeinfo = localtime(&maxTaskID); strftime(buffer,20,"%Y%m%d%H%M%S.tkwd",timeinfo); std::string desFileName = configHolder.tasksDir() + buffer; - + desFile.open(desFileName.c_str()); if(!desFile.is_open()) { - std::cout << "can't open file: " << desFileName; + return false; } - while(itr != words.end()) { - if(index%maxLimit>0 && - (words.size()-index)/maxLimit > 0) { - ++index; - break; - }else { - ++index; - } - if(!desFile.good()) + //Write words into file. + int j = 0; + while(j++ < maxLimit && itr != words.end()) { + if(!desFile.good()){ + std::cerr << "error when open file" << std::endl; return false; - std::cout << *itr << " "; - desFile << *itr << std::endl; } - ++itr; + if(itr->empty()) { + ++itr; + continue; + }else { + desFile << *itr << std::endl; + ++itr; + } } - std::cout << std::endl << "Another file" << std::endl; - //Choose the task's name. - Task newTask(maxTaskID,baseName.c_str()); + Task newTask(maxTaskID,realName.c_str()); //Pass a copy of newTask here. allTasks[newTask.getID()] = newTask; activeID.push_back(newTask.getID()); desFile.close(); - } //End of for(...); + } //[!0] return true; } - const std::vector &Manager::getActiveTasks() const{ return activeID; } diff --git a/src/core/Manager.h b/src/core/Manager.h index e9621e5..3c60f49 100644 --- a/src/core/Manager.h +++ b/src/core/Manager.h @@ -79,7 +79,7 @@ public: //Create a new task with the words set. bool createTask(const std::set &words, - const char *taskName = 0, + const char *taskName = 0, unsigned maxLimit = 100000); //Get the number of the tasks that should be reviewed. diff --git a/src/core/Reciter.cpp b/src/core/Reciter.cpp index 9b95170..8d44b91 100644 --- a/src/core/Reciter.cpp +++ b/src/core/Reciter.cpp @@ -16,14 +16,22 @@ bool Reciter::redo(bool Random) { if(wordList != 0) delete wordList; wordList = new WordList(words.size()); + r_times = 0; return true; } void Reciter::test(bool result) { - if(result) + + if(result) { wordList->scan(); - else - wordList->lose(); + --r_times; + } + else { + int var = wordList->lose(); + if(var == 0) + var = 1; + r_times += (var-1); + } } } //namespace freeRecite end diff --git a/src/core/Reciter.h b/src/core/Reciter.h index d9e0ad1..b69b684 100644 --- a/src/core/Reciter.h +++ b/src/core/Reciter.h @@ -44,6 +44,9 @@ public: ~Reciter() { /* Do Nothing Here! */ } + //Reimplement the virtual method from class Scanner. + unsigned times() const; + //Do this test again with an random order. bool redo(bool Random = false); @@ -54,6 +57,11 @@ public: void test(bool result); }; +inline +unsigned Reciter::times() const { + return static_cast(words.size()*5 + r_times); +} + } //namespace freeRecite end #endif //RECITER_H_ diff --git a/src/core/Scanner.h b/src/core/Scanner.h index d891880..f5d5f93 100644 --- a/src/core/Scanner.h +++ b/src/core/Scanner.h @@ -52,10 +52,10 @@ class Scanner { public: Scanner() - :wordList(0) + :r_times(0),wordList(0) { /* Do Nothing Here! */ } - ~Scanner() - { /* Do Nothing Here! */ } + virtual ~Scanner(); + /** * Load the words from the file, success return true. @@ -69,6 +69,9 @@ public: //Return the procent of the current task's progress. unsigned size() const; + //Return the remaining times that must be scan. + virtual unsigned times() const = 0; + /** * Get the next word you should recite. * Before you get it, you must test whether it is valid. @@ -97,11 +100,18 @@ protected: //Mank wors' order be random. bool makeRandom(); + int r_times; WordList *wordList; std::vector words; std::string taskFileName; }; +inline +Scanner::~Scanner() { + if(wordList != 0) + delete wordList; +} + inline const std::string &Scanner::getWord() const { return words[wordList->getNext()]; diff --git a/src/core/Tester.cpp b/src/core/Tester.cpp index e895d1a..6468ae1 100644 --- a/src/core/Tester.cpp +++ b/src/core/Tester.cpp @@ -6,8 +6,13 @@ void Tester::test(bool result) { if(result) { if(wordList->pass() == 0) ++score; - } else - wordList->lose(); + --r_times; + } else { + int var = wordList->lose(); + if( var == 0) + var = 4; + r_times += var-1; + } } diff --git a/src/core/Tester.h b/src/core/Tester.h index 8a6952b..cd4e726 100644 --- a/src/core/Tester.h +++ b/src/core/Tester.h @@ -46,6 +46,7 @@ public: ~Tester() { /* Do Nothing Here! */ } + unsigned times() const; unsigned getScore() const; void test(bool result); protected: @@ -53,6 +54,11 @@ protected: }; inline +unsigned Tester::times() const{ + return words.size() + r_times; +} + +inline unsigned Tester::getScore() const { return score*100/words.size(); } diff --git a/src/core/WordList.cpp b/src/core/WordList.cpp index c75ab2d..da52145 100644 --- a/src/core/WordList.cpp +++ b/src/core/WordList.cpp @@ -19,7 +19,7 @@ WordList::WordList(unsigned init__Size) pos[1] = pos[1]->next; if( i > 0 && i <= 3 ) pos[2] = pos[2]->next; - if( i > 0 && i <= 10) + if( i > 0 && i <= 7) pos[3] = pos[3]->next; } } @@ -55,9 +55,11 @@ int WordList::pass() return status; } //end of function WordList::pass(). -void WordList::lose() +int WordList::lose() { + int status = first->status; first->status = 1; + return status; } void WordList::moveToPos(unsigned i) { diff --git a/src/core/WordList.h b/src/core/WordList.h index 333180a..fc59de7 100644 --- a/src/core/WordList.h +++ b/src/core/WordList.h @@ -80,9 +80,9 @@ public: /** * If the input word is false, then you must call lose(). - * It will also help like the pass() do. + * The return value is the status of the current word. **/ - void lose(); + int lose(); /** diff --git a/src/ui/Cui.cpp b/src/ui/Cui.cpp index ec20f2f..8cd009d 100644 --- a/src/ui/Cui.cpp +++ b/src/ui/Cui.cpp @@ -107,7 +107,10 @@ void CUI::creatNew() { return; } std::set wordSet; - std::string word, cmd; + std::string word, name, cmd; + std::cout << "Give a task name for it?" << std::endl; + std::cout << "(Press Enter will give a default name): "; + getLine(name); while(ifs.good()) { std::getline(ifs,word); if(!dictionary.lookUp(word) && !word.empty()) { @@ -124,11 +127,7 @@ void CUI::creatNew() { } wordSet.insert(word); } - std::cout << "Give a task name for it?" << std::endl; - std::cout << "(Press Enter will give a default name): "; - cmd.clear(); - getLine(cmd); - if(manager.createTask(wordSet,cmd.c_str(),50)) { + if(manager.createTask(wordSet,name.c_str(),30)) { std::cout << "Creat a task SUCCESS!" << std::endl; manager.refresh(); }else @@ -146,7 +145,8 @@ void CUI::showAnswer(Scanner *scanner) const { <<"* " <capability() - <<" Unrecited Num: " << scanner->size() + <<" R_Num: " << scanner->size() + <<" R_Times: "<< scanner->times() << std::endl <<"**********************************************" <