repair print
[FreeRecite.git] / src / Cui.cpp
blobd98635022dffddcc6e2a9a1fe44db4f6e629f357
1 #include <set>
2 #include <fstream>
3 #include <cstdlib>
4 #include <iostream>
5 #include <string>
6 #include "Cui.h"
7 #include "Manager.h"
8 #include "Dict.h"
9 #include "Task.h"
10 #include "Reciter.h"
12 namespace freeRecite {
14 CUI::CUI(const std::string &dir)
16 manager = new Manager;
17 if(!manager->load(dir)) {
18 std::cerr << "Can't load the configure file from " << dir << std::endl
19 << "The program exit!\n";
20 exit(EXIT_FAILURE);
22 dict = new Dict(dir + "freeRecite.dict");
23 if(!dict->loadDict()) {
24 std::cerr << "Can't load the dic from " << dir << std::endl
25 << "The program exit!\n";
26 exit(EXIT_FAILURE);
28 showActive();
31 CUI::~CUI()
33 delete manager;
34 delete dict;
38 void CUI::run() {
39 std::cout << "Welcome to Free Recite!"<< std::endl;
40 std::cout << "Type \"help\" for help;p" << std::endl;
41 while(true) {
42 switch(getCmd()) {
43 case 'e':{ //eixt
44 exit(EXIT_SUCCESS);
46 case 'n':{ //creat new task
47 creatNew();
48 break;
50 case 't':{ //test
51 test();
52 break;
54 case 'r': { //recite for the new works.
55 recite();
56 break;
58 case 'm':{
59 modify();
60 break;
62 case 's':{
63 showActive();
64 break;
66 case'h':{
67 help();
68 break;
70 default:{
71 std::cerr<< "Bad Command!"<<std::endl;
72 break;
74 } //End of swithch().
75 } //End of while().
76 } //End of run().
78 char CUI::getCmd() {
79 std::string cmd;
80 std::cout << "~_~Free Recite:> ";
81 std::cout.flush();
82 getLine(cmd);
84 if(cmd == "exit")
85 return 'e';
86 else if(cmd == "new")
87 return 'n';
88 else if(cmd == "test")
89 return 't';
90 else if(cmd == "recite")
91 return 'r';
92 else if(cmd == "modify")
93 return 'm';
94 else if(cmd == "showall")
95 return 's';
96 else if(cmd == "help")
97 return 'h';
98 else
99 return '*';
102 void CUI::creatNew() {
103 std::string fileName,taskName;
104 std::cout<<"Input the filename of the words. File Name: ";
105 getLine(fileName);
106 std::ifstream ifs(fileName.c_str());
107 if(!ifs.is_open()) {
108 std::cout << "Can't find the file.";
109 return;
111 std::set<std::string> wordSet;
112 std::string word, cmd;
113 while(ifs.good()) {
114 std::getline(ifs,word);
115 if(!dict->lookUp(word) && !word.empty()) {
116 std::cout << '\"' << word << '\"'
117 << " can't be fond in your dict"<<std::endl
118 << "Remove(R) or Modify(M) it? (R/m)" << std::endl;
119 getLine(cmd);
121 if(cmd == "M" || cmd == "m")
122 while(!modify(word))
124 else
125 continue;
127 wordSet.insert(word);
129 std::cout << "Give a task name for it?" << std::endl;
130 std::cout << "(Press Enter will give a default name): ";
131 cmd.clear();
132 getLine(cmd);
133 if(manager->creatTask(wordSet,cmd.c_str())) {
134 std::cout << "Creat a task SUCCESS!" << std::endl;
135 manager->refresh();
136 }else
137 std::cerr << "Can't creat a task :}" << std::endl;
140 void CUI::testProcess(Reciter &tester) {
141 clear();
142 time_t starTime = 0;
143 time_t endTime = 0;
144 time(&starTime);
145 char c;
146 std::cin.get(c);
147 std::string inputStr;
148 bool testResult;
149 std::cout << "Step 1: interpretation To key!" << std::endl;
150 while(tester.isValid()) {
151 if(dict->lookUp(tester.getWord())){
152 std::cout<<"[W]: "<<dict->word()<<std::endl;
153 std::cout<<"Press Enter to show the interpretation..."<<std::endl;
154 getLine(inputStr);
155 if(inputStr == "\\modify")
156 modify(tester.getWord());
157 else if(inputStr == "\\stop")
158 return;
159 else if(inputStr == "\\exit")
160 exit(EXIT_FAILURE);
162 std::cout<<std::endl
163 <<"********************ANSWER********************"<<std::endl
164 <<"*"<<std::endl
165 <<"* " <<dict->word()<< std::endl
166 <<"*" << std::endl
167 <<"* ["<<dict->phonetics() << "]"<<std::endl
168 <<"* " <<dict->translation() << std::endl
169 <<"*" << std::endl
170 <<"* Amount: "<< tester.capability()<<" Unrecited Num: "
171 <<(tester.size()-1) << std::endl
172 <<"**********************************************"<<std::endl
173 <<std::endl;
174 std::cout<<std::endl<<std::endl
175 <<"Have you remembered it(Y/n)?";
176 getLine(inputStr);
177 if(inputStr != "N" || inputStr != "n") {
178 std::cout<<" ** * * "<<std::endl;
179 std::cout<<" * * * * "<<std::endl;
180 std::cout<<" * * ** "<<std::endl;
181 std::cout<<" * * * * "<<std::endl;
182 std::cout<<" ** * * "<<std::endl;
183 tester.test(true);
185 else {
186 std::cout<<"##### #### #### "<<std::endl;
187 std::cout<<"# # # # # "<<std::endl;
188 std::cout<<"##### #### #### "<<std::endl;
189 std::cout<<"# # # # # "<<std::endl;
190 std::cout<<"##### # # # # "<<std::endl;
191 tester.test(false);
193 clear();
194 }else {
195 std::cout <<"**" << tester.getWord()<<"** can't find in the Dictionary."
196 <<"modify(M) or remove(R) it? M/r"<<std::endl;
197 modify();
198 tester.test(true);
199 clear();
202 tester.redo();
203 clear();
204 std::cout << "Step 2: interpretation To key!" << std::endl;
205 while(tester.isValid()) {
206 if(dict->lookUp(tester.getWord())){
207 std::cout<<"[M]: "<<dict->translation()<<std::endl;
208 std::cout<<"Input: ";
209 getLine(inputStr);
210 if(inputStr == "\\modify")
211 modify(tester.getWord());
212 else if(inputStr == "\\stop")
213 return;
214 else if(inputStr == "\\exit")
215 exit(EXIT_FAILURE);
216 testResult = ( inputStr == tester.getWord() ? true : false );
218 std::cout<<std::endl
219 <<"********************ANSWER********************"<<std::endl
220 <<"*"<<std::endl
221 <<"* " <<dict->word()<< std::endl
222 <<"*" << std::endl
223 <<"* ["<<dict->phonetics() << "]"<<std::endl
224 <<"* " <<dict->translation() << std::endl
225 <<"*" << std::endl
226 <<"* Amount: "<< tester.capability()<<" Unrecited Num: "
227 << (tester.size()-1) << std::endl
228 <<"**********************************************"<<std::endl
229 <<std::endl;
230 if(testResult) {
231 std::cout<<" ** * * "<<std::endl;
232 std::cout<<" * * * * "<<std::endl;
233 std::cout<<" * * ** "<<std::endl;
234 std::cout<<" * * * * "<<std::endl;
235 std::cout<<" ** * * "<<std::endl;
237 else {
238 std::cout<<"##### #### #### "<<std::endl;
239 std::cout<<"# # # # # "<<std::endl;
240 std::cout<<"##### #### #### "<<std::endl;
241 std::cout<<"# # # # # "<<std::endl;
242 std::cout<<"##### # # # # "<<std::endl;
244 tester.test(testResult);
245 clear();
246 }else {
247 std::cout <<"**" << tester.getWord()<<"** can't find in the Dictionary."
248 <<"modify(M) or remove(R) it? M/r"<<std::endl;
249 modify();
250 tester.test(true);
251 clear();
254 time(&endTime);
255 starTime = endTime - starTime;
256 std::cout << std::endl << "Used Time: "
257 << starTime/60 << "minutes" << std::endl;
260 void CUI::test() {
261 showActive();
262 if((manager->getActiveTasks()).size() == 0){
263 std::cout << "There's no tasks to review!" << std::endl;
264 return;
266 unsigned i;
267 std::cout<< "Which do you want to test [ 0 by default]: ";
268 std::cin >> i;
269 if(i >= (manager->getActiveTasks()).size()) {
270 std::cerr<< "Error task!" << std::endl;
271 return;
274 Reciter tester;
275 time_t taskID = (manager->getActiveTasks())[i];
277 if(!tester.loadWords(taskID,manager->getDir().c_str())) {
278 std::cerr << "error when load words!" << std::endl;
279 return;
281 testProcess(tester);
283 if(manager->test(taskID,tester.getScore())) {
284 std::cout<<"Your score is " << tester.getScore() << std::endl;
285 std::cout<<"You passed it!"<<std::endl;
287 else {
288 std::cout<<"Your score is " << tester.getScore() << std::endl
289 <<"You haven't passed it :} "<<std::endl;
292 time_t nextTime = manager->getNextTime(taskID);
293 if(manager->getTaskStep(taskID) == 8)
294 std::cout << "Congratulations! You have complish this task!" << std::endl;
295 else {
296 struct tm * timeinfo;
297 char buffer[30];
298 timeinfo = localtime(&nextTime);
299 strftime(buffer,30,"%Y.%m.%d %H:%M:%S",timeinfo);
300 std::cout << "Then next reviewing time is: " << buffer
301 << std::endl << std::endl;
303 showActive();
304 clear();
309 void CUI::recite() {
310 showActive();
311 if((manager->getActiveTasks()).size() == 0)
312 return;
313 unsigned i;
314 std::cout<< "Which do you want to test: ";
315 std::cin >> i;
316 if(i >= (manager->getActiveTasks()).size()) {
317 std::cerr<< "Error task!" << std::endl;
318 return;
321 Reciter tester;
322 if(!tester.loadWords((manager->getActiveTasks())[i],manager->getDir().c_str())) {
323 std::cerr << "error when load words!" << std::endl;
324 return;
326 testProcess(tester);
328 std::cout << "Do you want to do it again?(y/N)";
329 std::string YN;
330 while(std::cin>>YN,YN=="Y"||YN=="y") {
331 tester.redo();
332 testProcess(tester);
336 bool CUI::modify(const char *word) {
337 std::string newItem,inputStr;
338 if(word == 0) {
339 std::cout<<"[W]: ";
340 getLine(inputStr);
341 newItem.append("[W]"+inputStr);
342 }else {
343 newItem.append("[W]");
344 newItem.append(word);
347 std::cout<<"θ_ɑ_ʌ_ә_є_æ_ɔ_ʃ_ð_ŋ_ʒ"<<std::endl;
348 std::cout<<"0_1_2_3_4_5_6_7_8_9_="<<std::endl;
349 std::cout<<"[T]: ";
350 getLine(inputStr);
351 newItem.append("[T]"+inputStr);
352 std::cout<<"[M]: ";
353 getLine(inputStr);
354 newItem.append("[M]"+inputStr);
356 if(!dict->modify(newItem)) {
357 std::cout<<"ERROR!" << std::endl;
358 return false;
360 std::cout<<"SUCCESS!"<<std::endl;
361 return true;
365 bool CUI::modify(const std::string &word) {
366 std::string newItem,inputStr;
367 if(word.empty()) {
368 std::cout<<"[W]: ";
369 getLine(inputStr);
370 newItem.append("[W]"+inputStr);
371 }else
372 newItem.append("[W]"+word);
374 std::cout<<"θ_ɑ_ʌ_ә_є_æ_ɔ_ʃ_ð_ŋ_ʒ"<<std::endl;
375 std::cout<<"0_1_2_3_4_5_6_7_8_9_="<<std::endl;
376 std::cout<<"[T]: ";
377 getLine(inputStr);
378 newItem.append("[T]"+inputStr);
379 std::cout<<"[M]: ";
380 getLine(inputStr);
381 newItem.append("[M]"+inputStr);
383 if(!dict->modify(newItem)) {
384 std::cout<<"ERROR!" << std::endl;
385 return false;
387 std::cout<<"SUCCESS!"<<std::endl;
388 return true;
391 void CUI::showActive() {
392 clear();
393 if(!manager->refresh()) {
394 std::cout << "The configure file is broken!" << std::endl;
395 exit(EXIT_FAILURE);
397 if(manager->getActiveTaskNum() == 0) {
400 std::cout << "There's no new task should be review,type \"new\" to creat one."
401 << std::endl;
403 if(manager->getNextTime() != 0) {
404 time_t nextTime = manager->getNextTime();
405 struct tm * timeinfo;
406 char buffer[30];
407 timeinfo = localtime(&nextTime);
408 strftime(buffer,30,"%Y.%m.%d %H:%M:%S",timeinfo);
409 std::cout << "The nearest reviewing time is at " << buffer
410 << std::endl
411 << "Please use FreeRecite at that time!"
412 << std::endl << std::endl;
415 else {
416 std::cout << "There's " << manager->getActiveTaskNum()
417 << " tasks should review." << std::endl;
418 std::cout << "They are:" << std::endl;
420 for(int i = 0; i < manager->getActiveTaskNum(); ++i) {
422 std::cout << i << ": "
423 << manager->getTaskName(manager->getActiveTasks()[i])
424 << std::endl;
427 std::cout << std::endl << std::endl << std::endl;
431 void CUI::clear() {
432 for(int i = 0; i < 10; ++i)
433 std::cerr << std::endl;
436 void CUI::getLine(std::string &input) {
438 input.clear();
439 char t;
440 while(std::cin.get(t),t != '\n')
441 input += t;
444 void CUI::help() {
445 std::cout << "Welcome to use Free Recite!" << std::endl
446 << "There's server command here:" << std::endl
447 << " exit: Exit Free Recite normally." << std::endl
448 << " new: Creat a new tasks."<<std::endl
449 << " test: Test the tasks." << std::endl
450 << " recite: Recite the task without any mark set."<<std::endl
451 << " help: Get help like this;p"<<std::endl;
454 } // namespace freeRecite End.