-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcmd_processor.cpp
More file actions
1339 lines (1219 loc) · 42.5 KB
/
cmd_processor.cpp
File metadata and controls
1339 lines (1219 loc) · 42.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "cmd_processor.h"
#include <iostream>
#include <list>
#include <mutex>
#include <sstream>
#include <string>
#include <thread>
#include "task_list.h"
#include "tools.h"
using namespace std;
extern mutex mtx;
bool should_quit(string choice){
return (choice == "q" || choice == "quit");
}
bool CmdProcessor::GetArgv(int argc, char* argv[], int start) {
cmd_.clear();
if (start == argc) {
return false;
}
for (int i = start; i < argc; i++) {
cmd_.push_back(argv[i]);
}
return true;
}
bool CmdProcessor::GetCmd() {
cmd_.clear();
string tmp;
getline(cin, tmp);
istringstream cmd(tmp);
cmd >> tmp;
if (!cmd) return false;
cmd_.push_back(tmp);
while (cmd >> tmp) {
cmd_.push_back(tmp);
}
return true;
}
// check cmd and do sth accordingly; return: 1 -- correct cmd, 0 -- wrong cmd,
// -1 -- quit.
int CmdProcessor::CmdDistributor(TaskList& task_list) const {
const int kSize = cmd_.size();
if (kSize < 1) return 0;
bool flag = false;
string first_cmd = cmd_.front();
to_lower(first_cmd);
if (first_cmd == "addtask" || first_cmd == "add") {
lock_guard<mutex> lck(mtx);
if (kSize == 1)
flag = AddTaskNoOp(task_list);
else
flag = AddTaskOp(task_list, cmd_);
} else if (first_cmd == "modifytask" || first_cmd == "modify") {
lock_guard<mutex> lck(mtx);
if (kSize == 1)
flag = ModifyTaskNoOp(task_list);
else
flag = ModifyTaskOp(task_list, cmd_);
} else if (first_cmd == "deletetask" || first_cmd == "delete") {
lock_guard<mutex> lck(mtx);
if (kSize == 1)
flag = DeleteTaskNoOp(task_list);
else
flag = DeleteTaskOp(task_list, cmd_);
} else if (first_cmd == "showtask" || first_cmd == "show") {
if (kSize == 1)
flag = ShowTaskNoOp(task_list);
else
flag = ShowTaskOp(task_list, cmd_);
} else if (first_cmd == "searchtask" || first_cmd == "search") {
if (kSize == 1)
flag = SearchTaskNoOp(task_list);
else
flag = SearchTaskOp(task_list, cmd_);
} else if (first_cmd == "quit" || first_cmd == "q") {
cout << "Bye.\n";
return -1;
} else if (first_cmd == "login" || first_cmd == "li" ||
first_cmd == "createaccount" || first_cmd == "ca" ||
first_cmd == "changepassword" || first_cmd == "changepw" ||
first_cmd == "cp" || first_cmd == "deleteaccount" ||
first_cmd == "da") {
cout << "Please quit first and then restart the program to \"" << first_cmd
<< "\".\n";
} else {
cout << "\"" << cmd_.front() << "\" is not a valid command.\n";
return 0;
}
return (int)flag;
}
// 用户输入格式:addtask -n name -b begin -p priority -t type -r remind
// (注意:用户输入的参数不能以-开头,priority默认为0,type默认为"
// ",选项的顺序可以打乱)
bool AddTaskOp(TaskList& task_list, list<string> cmd) {
Other task;
task.priority = 0;
task.type = " ";
string begin; // begin用于保存begin_time_并分配唯一的id
list<string>::iterator it = ++cmd.begin(); // 从cmd的第二个string开始参考
while (it != cmd.end()) {
if (*it == "-n") {
if (++it == cmd.end()) //-name是最后一个,后面没有参数
{
cout << "No parameters after -n" << endl;
return false;
}
string str = *(it); // 找到-name的下一位
if (str[0] == '-') // 不符合
{
cout << "No parameters after -n" << endl;
return false;
}
task.name = str;
it++;
} else if (*it == "-b") {
if (++it == cmd.end()) //-begin是最后一个,后面没有参数
{
return false;
cout << "No parameters after -b" << endl;
}
begin = *(it); // 找到-begin的下一位
if (begin[0] == '-') // 不符合
{
cout << "No parameters after -b" << endl;
return false;
}
if (!isValidDate(begin)) // 时间参数不符合要求
{
cout << "The parameter does not meet the requirements" << endl;
return false;
}
task.begin_time = to_time_t(begin);
it++;
} else if (*it == "-p") {
if (++it == cmd.end()) //-priority是最后一个,后面没有参数,这是可以的
break;
string str = *(it); // 找到-priority的下一位
if (str == "-n" || str == "-b" || str == "-t" ||
str == "-r") //-priority后不跟参数,这是可以的
{
it++;
continue;
}
int p = stoi(str);
if (p != 1 && p != 2 && p != 4) {
cout << "The parameters do not meet the requirements";
return false;
}
task.priority = p;
it++;
} else if (*it == "-t") {
if (++it == cmd.end()) //-type是最后一个,后面没有参数,这是可以的
break;
string str = *(it); // 找到-type的下一位
if (str == "-n" || str == "-b" || str == "-p" ||
str == "-r") //-type后不跟参数,这是可以的
{
it++;
continue;
}
task.type = str;
it++;
} else if (*it == "-r") {
if (++it == cmd.end()) //-remind是最后一个,后面没有参数
{
cout << "No parameters after -r" << endl;
return false;
}
string str = *(it); // 找到-remind的下一位
if (str[0] == '-') // 不符合
{
cout << "No parameters after -r" << endl;
return false;
}
if (!isValidDate(str)) // 时间参数不符合要求
{
cout << "The parameters do not meet the requirements";
return false;
}
task.remind_time = to_time_t(str);
it++;
} else {
cout << "Invalid command" << endl;
return false;
}
}
int id = (to_time_t(begin)) % 997; // 得到id后打包进行add
vector<pair<int, Other>>::iterator it1 = task_list.FindTask(id);
while (it1 != task_list.return_end()) {
id = (id + 1) % 997;
it1 = task_list.FindTask(id);
}
if (!task_list.Add(make_pair(id, task))) {
cout << "Faliure" << endl;
return false;
}
return true;
}
// 重要规定:用于查找的选项是大写,用于修改的选项是小写!
// 用户输入格式: modifytask -N name -I id -n new_name -b begin -p priority -t
// type -r
// remind(注意:-N和-I至少有1个,-p,-t后面可以没有参数,-n,-b,-r后面必须有参数,id不可以修改)
bool ModifyTaskOp(TaskList& task_list, list<string> cmd) {
string name = "-"; // 用于查找的name,默认是"-"
int id = -1; // 用于查找的id
Other task;
task.name =
"-"; // 默认是"-",若发现没有通过命令修改,则copy查找到的任务的name,其他的属性同理
task.begin_time = -1;
task.priority = 0;
task.type = " ";
task.remind_time = -1;
list<string>::iterator it = ++cmd.begin(); // 从cmd的第二个string开始参考
while (it != cmd.end()) {
if (*it == "-n") // 修改所用的name
{
if (++it == cmd.end()) //-name是最后一个,后面没有参数
{
cout << "No parameters after -n" << endl;
return false;
}
string str = *(it); // 找到-name的下一位
if (str[0] == '-') // 不符合
{
cout << "No parameters after -n" << endl;
return false;
}
task.name = str;
it++;
} else if (*it == "-N") // 查找所用的name
{
if (++it == cmd.end()) //-name是最后一个,后面没有参数
{
cout << "No parameters after -N" << endl;
return false;
}
string str = *(it); // 找到-name的下一位
if (str[0] == '-') // 不符合
{
cout << "No parameters after -N" << endl;
return false;
}
name = str;
it++;
} else if (*it == "-I") // 查找所用的id
{
if (++it == cmd.end()) //-id是最后一个,后面没有参数
{
cout << "No parameters after -I" << endl;
return false;
}
string str = *(it); // 找到-name的下一位
if (str[0] == '-') // 不符合
{
cout << "No parameters after -I" << endl;
return false;
}
id = stoi(str); // 保存需要处理的id,为之后的查找做准备
it++;
} else if (*it == "-b") {
if (++it == cmd.end()) //-begin是最后一个,后面没有参数
{
return false;
cout << "No parameters after -b" << endl;
}
string str = *(it); // 找到-begin的下一位
if (str[0] == '-') // 不符合
{
cout << "No parameters after -b" << endl;
return false;
}
if (!isValidDate(str)) // 时间参数不符合要求
{
cout << "The parameter does not meet the requirements" << endl;
return false;
}
task.begin_time = to_time_t(str);
it++;
} else if (*it == "-p") {
if (++it == cmd.end()) //-priority是最后一个,后面没有参数,这是可以的
break;
string str = *(it); // 找到-priority的下一位
if (str == "-n" || str == "-b" || str == "-t" || str == "-r" ||
str == "-N" || str == "-I") //-priority后不跟参数,这是可以的
continue;
int p = stoi(str);
if (p != 1 && p != 2 && p != 4) {
cout << "The parameters do not meet the requirements";
return false;
}
task.priority = p;
it++;
} else if (*it == "-t") {
if (++it == cmd.end()) //-type是最后一个,后面没有参数,这是可以的
break;
string str = *(it);
if (str == "-n" || str == "-b" || str == "-p" || str == "-r" ||
str == "-N" || str == "-I") //-type后不跟参数,这是可以的
continue;
task.type = str;
it++;
} else if (*it == "-r") {
if (++it == cmd.end()) //-remind是最后一个,后面没有参数
{
cout << "No parameters after -r" << endl;
return false;
}
string str = *(it); // 找到-remind的下一位
if (str[0] == '-') // 不符合
{
cout << "No parameters after -r" << endl;
return false;
}
if (!isValidDate(str)) // 时间参数不符合要求
{
cout << "The parameters do not meet the requirements";
return false;
}
task.remind_time = to_time_t(str);
it++;
} else {
cout << "Invalid command" << endl;
return false;
}
}
int id1; // 保存修改任务的id值
vector<pair<int, Other>>::iterator it1 = task_list.FindTask(id);
vector<pair<int, Other>>::iterator it2 = task_list.FindTask(name);
bool flag1 = it1 != task_list.return_end();
bool flag2 = it2 != task_list.return_end();
if ((flag1 && flag2 && it1 == it2) || (!flag1 && flag2) ||
(flag1 && !flag2)) // 3种符合查找要求的情况
{
vector<pair<int, Other>>::iterator itt;
if (flag1)
itt = it1;
else
itt = it2;
id1 = itt->first;
if (task.name == "-") task.name = itt->second.name;
if (task.begin_time == -1) task.begin_time = itt->second.begin_time;
if (task.type == " ") task.type = itt->second.type;
if (task.priority == 0) task.priority = itt->second.priority;
if (task.remind_time == -1) task.remind_time = itt->second.remind_time;
if (flag1)
task_list.Erase(id); // 删除对应的任务
else
task_list.Erase(name);
if (!task_list.Add(make_pair(id1, task))) // 添加相应的任务
{
cout << "Faliure" << endl;
return false;
}
task_list.saveFile();
return true;
} else {
task_list.saveFile();
return false;
}
}
// 使用说明: searchtask -N name -I id(-N和-I至少有一个)
bool SearchTaskOp(TaskList& task_list, list<string> cmd) {
string name = "-"; // 保存需要查找的name,默认为"-"
int id = -1;
list<string>::iterator it = ++cmd.begin(); // 从cmd的第二个string开始参考
while (it != cmd.end()) {
if (*it == "-N") // 查找所用的name
{
if (++it == cmd.end()) //-name是最后一个,后面没有参数
{
cout << "No parameters after -N" << endl;
return false;
}
string str = *(it); // 找到-name的下一位
if (str[0] == '-') // 不符合
{
cout << "No parameters after -N" << endl;
return false;
}
name = str;
it++;
} else if (*it == "-I") // 查找所用的id
{
if (++it == cmd.end()) //-id是最后一个,后面没有参数
{
cout << "No parameters after -I" << endl;
return false;
}
string str = *(it);
if (str[0] == '-') // 不符合
{
cout << "No parameters after -I" << endl;
return false;
}
id = stoi(str); // 保存需要处理的id,为之后的查找做准备
it++;
} else {
cout << "Invalid command" << endl;
return false;
}
}
vector<pair<int, Other>>::iterator it1 = task_list.FindTask(id);
vector<pair<int, Other>>::iterator it2 = task_list.FindTask(name);
bool flag1 = it1 != task_list.return_end();
bool flag2 = it2 != task_list.return_end();
if ((flag1 && flag2 && it1 == it2) || (!flag1 && flag2) ||
(flag1 && !flag2)) // 3种符合查找要求的情况,输出任务信息
{
if (flag1)
task_list.FindShow(id);
else
task_list.FindShow(name);
return true;
} else
return false;
}
// 使用说明:showtask -S start -E end -P priority -T type -A
// i(id)/b(begin)/p(priority)/r(remind) -D
// (其中-S,-E,-P,-T均可以缺省,-A,-D至多有1个,后面必须有参数)
bool ShowTaskOp(TaskList& task_list, list<string> cmd) {
list<string>::iterator it = ++cmd.begin(); // 从cmd的第二个string开始
int begin_time = -1;
int end_time = -1;
int priority = 0;
string type = " ";
static bool (*func)(pair<int, Other> task1, pair<int, Other> task2) =
LessBegin;
while (it != cmd.end()) {
if (*it == "-S") {
if (++it ==
cmd.end()) //-S是最后一个,后面没有参数,因为begin有缺省值,因此这是可以的
break;
string str = *(it); // 找到-S的下一位
if (str == "-E" || str == "-P" || str == "-T" || str == "-A" ||
str == "-D") //-S后不跟参数,这是可以的
continue;
if (!isValidDate(str)) // 时间参数不符合要求
{
cout << "The parameter does not meet the requirements" << endl;
return false;
}
begin_time = to_time_t(str);
it++;
} else if (*it == "-E") {
if (++it ==
cmd.end()) //-E是最后一个,后面没有参数,因为end有缺省值,因此这是可以的
break;
string str = *(it); // 找到-S的下一位
if (str == "-S" || str == "-P" || str == "-T" || str == "-A" ||
str == "-D") //-S后不跟参数,这是可以的
continue;
if (!isValidDate(str)) // 时间参数不符合要求
{
cout << "The parameter does not meet the requirements" << endl;
return false;
}
end_time = to_time_t(str);
it++;
} else if (*it == "-P") {
if (++it == cmd.end()) //-P是最后一个,后面没有参数,这是可以的
break;
string str = *(it);
if (str == "-S" || str == "-E" || str == "-T" || str == "-A" ||
str == "-D") //-P后不跟参数,这是可以的
continue;
int p = stoi(str);
if (p != 1 && p != 2 && p != 3 && p != 4 && p != 5 && p != 6 && p != 7) {
cout << "The parameters do not meet the requirements";
return false;
}
priority = p;
it++;
} else if (*it == "-T") {
if (++it == cmd.end()) //-T是最后一个,后面没有参数,这是可以的
break;
string str = *(it);
if (str == "-S" || str == "-E" || str == "-P" || str == "-A" ||
str == "-D") //-T后不跟参数,这是可以的
continue;
type = str;
it++;
} else if (*it == "-A") {
if (++it == cmd.end()) {
cout << "No parameters after -A" << endl;
return false;
}
string str = *(it);
if (str[0] == '-') // 不符合
{
cout << "No parameters after -A" << endl;
return false;
}
if (str != "b" && str != "begin" && str != "i" && str != "id" &&
str != "p" && str != "priority" && str != "r" && str != "remind") {
cout << "The parameter does not meet the requirements" << endl;
return false;
}
if (str == "b" || str == "begin")
func = GreaterBegin;
else if (str == "i" || str == "id")
func = GreaterId;
else if (str == "p" || str == "priority")
func = GreaterPriority;
else
func = GreaterRemind;
it++;
} else if (*it == "-D") {
if (++it == cmd.end()) {
cout << "No parameters after -D" << endl;
return false;
}
string str = *(it);
if (str[0] == '-') // 不符合
{
cout << "No parameters after -D" << endl;
return false;
}
if (str != "b" && str != "begin" && str != "i" && str != "id" &&
str != "p" && str != "priority" && str != "r" && str != "remind") {
cout << "The parameter does not meet the requirements" << endl;
return false;
}
if (str == "b" || str == "begin")
func = LessBegin;
else if (str == "i" || str == "id")
func = LessId;
else if (str == "p" || str == "priority")
func = LessPriority;
else
func = LessRemind;
it++;
} else {
cout << "Invalid command" << endl;
return false;
}
}
if (begin_time == -1) begin_time = 0;
if (end_time == -1) end_time = pow(2, 31) - 1;
if (priority == 0) priority = 7;
if (type == " ")
task_list.Show(begin_time, end_time, priority, func);
else
task_list.Show(type, begin_time, end_time, priority, func);
return true;
}
// 使用说明:deltask -n name -i id -a(-n,-i可以均没有,-a代表删除全部任务)
bool DeleteTaskOp(TaskList& task_list, list<string> cmd) {
string name = "-"; // 保存需要查找的name,默认为"-"
int id = -1;
list<string>::iterator it = ++cmd.begin(); // 从cmd的第二个string开始参考
bool flagForAll = false;
while (it != cmd.end()) {
if (*it == "-n") // 查找所用的name
{
if (++it == cmd.end()) //-n是最后一个,后面没有参数
{
cout << "No parameters after -n" << endl;
return false;
}
string str = *(it); // 找到-name的下一位
if (str[0] == '-') // 不符合
{
cout << "No parameters after -n" << endl;
return false;
}
name = str;
it++;
} else if (*it == "-i") // 查找所用的id
{
if (++it == cmd.end()) //-id是最后一个,后面没有参数
{
cout << "No parameters after -i" << endl;
return false;
}
string str = *(it); // 找到-name的下一位
if (str[0] == '-') // 不符合
{
cout << "No parameters after -i" << endl;
return false;
}
id = stoi(str); // 保存需要处理的id,为之后的查找做准备
it++;
} else if (*it == "-a") {
list<string>::iterator it1 = ++it;
if (it1 == cmd.end() || *it1 == "-n" || *it1 == "-i")
flagForAll = true;
else
return false;
it = it1;
} else {
cout << "Invalid command" << endl;
return false;
}
}
vector<pair<int, Other>>::iterator it1 = task_list.FindTask(id);
vector<pair<int, Other>>::iterator it2 = task_list.FindTask(name);
bool flag1 = it1 != task_list.return_end();
bool flag2 = it2 != task_list.return_end();
bool flagForErase = (flag1 && flag2 && it1 == it2) || (!flag1 && flag2) ||
(flag1 && !flag2); // 指令-n和-i正确
if (flagForErase && flagForAll) // 删除全部任务
{
task_list.Clear();
task_list.saveFile();
return true;
} else if (flagForErase && !flagForAll) {
if (flag1)
task_list.Erase(id);
else
task_list.Erase(name);
task_list.saveFile();
return true;
} else {
task_list.saveFile();
return false;
}
}
bool AddTaskNoOp(TaskList& task_list){
// 添加任务名字
Other newtask;
std::cout << "Please input the name of the task\nEnter q or quit to quit!" << endl;
std::getline(std::cin, newtask.name);
if(should_quit(newtask.name)){
cout << "addtask quit!" << endl;
return false;
}
// 添加任务的开始时间
std::cout << "Please input the begin time of the task\nEnter q or quit to quit!" << endl;
std::cout << "Time format should be 2022/02/02/03:00:00" << endl;
string begin_t;
bool valid_input = false;
while (!valid_input)//检查任务的开始时间是不是%Y/%M/%D/%h:%m:%s的形式
{
std::cin >> begin_t;
if(should_quit(begin_t)){
cout << "addtask quit!" << endl;
return false;
}
if (isValidDate(begin_t))//如果是退出循环
valid_input = true;
else
std::cout << "Invalid format. Please try again.\nEnter q or quit to quit!" << endl;//否则一直输入直到是为止
}
newtask.begin_time = to_time_t(begin_t);//将用户输入的形式转化成time_t格式保存
// 添加任务的优先级
std::cout << "Please input the priority of the task" << endl;
std::cout << "-1-quit addtask, 1-low, 2-medium, 4-high, default value or other value-low" << endl;
std::cin >> newtask.priority;
// 检查优先级是不是符合1,2,4
while(!std::cin){
std::cin.clear(); // 清除输入流状态标志
std::cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略剩余的输入
std::cout << "Invalid input. Please enter a valid priority: \nEnter -1 to quit!" << endl;
std::cin >> newtask.priority;
}
if(newtask.priority == -1){
cout << "addtask quit!" << endl;
return false;
}
else if(newtask.priority != 1 && newtask.priority != 2 && newtask.priority != 4)
newtask.priority = 1;
// 添加任务的种类
std::cout << "Please input the type of the task, such as: entertainment sport study routine" << endl;
std::cout << "If you want a default type, please enter -.\nEnter q or quit to quit!" << endl;
std::cin >> newtask.type;
if(should_quit(newtask.type)){
cout << "addtask quit!" << endl;
return false;
}
else if(newtask.type[0] == '-')
newtask.type = "default";
// 添加任务的提醒时间
std::cout << "Please input the remind time of the task\nEnter q or quit to quit!" << endl;
std::cout << "Time format should be 2022/02/02/03:00:00" << endl;
string remind_t;
valid_input = false;
//检查任务的开始时间是不是%Y/%M/%D/%h:%m:%s的形式
while (!valid_input)
{
std::cin >> remind_t;
if(should_quit(remind_t)){
cout << "addtask quit!" << endl;
return false;
}
if (isValidDate(remind_t))
valid_input = true;
else
std::cout << "Invalid format. Please try again.\nEnter q or quit to quit!" << endl;
}
newtask.remind_time = to_time_t(remind_t);//将用户输入的形式转化成time_t格式保存
//id和开始时间一样
int id = to_time_t(begin_t)%1000;
while(task_list.FindTask(id) != task_list.return_end()){
id++;
}
int res = task_list.Add(make_pair(id, newtask));
while ( res!= 0){//如果用户输入的开始时间或名字重复了,返回值不是0
if(res==-1){
std::cout << "The begin time repeats! Please enter another begin time: \nEnter q or quit to quit!" << endl;
bool valid_input = false;
while (!valid_input)//检查任务的开始时间是不是%Y/%M/%D/%h:%m:%s的形式
{
std::cin >> begin_t;
if(should_quit(begin_t)){
cout << "addtask quit!" << endl;
return false;
}
if (isValidDate(begin_t))//如果是退出循环
valid_input = true;
else
std::cout << "Invalid format. Please try again.\nEnter q or quit to quit!" << endl;//否则一直输入直到是为止
}
newtask.begin_time = to_time_t(begin_t);//将用户输入的形式转化成time_t格式保存
id = to_time_t(begin_t)%1000;
while(task_list.FindTask(id) == task_list.return_end()){
id++;
}
res = task_list.Add(make_pair(id, newtask));
}
else if(res==1){
std::cout << "The name repeats! Please enter another name: \nEnter q or quit to quit!" << endl;
std::getline(std::cin,newtask.name);
if(should_quit(newtask.name)){
cout << "addtask quit!" << endl;
return false;
}
res = task_list.Add(make_pair(id, newtask));
}
}
return true;
}
bool DeleteTaskNoOp(TaskList& task_list){
std::cout << "Delete tasks based on name or ID" << endl;
std::cout << "-1 represents quit\n1 represents deletion based on ID\nwhile the remaining represents deletion based on name" << endl;
int choice;
std::cin >> choice;
if(choice == -1){
cout << "deletetask quit!" << endl;
return false;
}
while (!std::cin) // 检查输入是否有效
{
std::cin.clear(); // 清除输入流状态标志
std::cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略剩余的输入
std::cout << "Invalid input. Please enter a valid choice: \nEnter -1 to quit!" << endl;
std::cin >> choice;
if(choice == -1){
cout << "deletetask quit!" << endl;
return false;
}
}
if (choice == 1)
{
std::cout << "Please input the ID of the task to be deleted" << endl
<< "Enter -1 to quit!" << endl;
long long id;
std::cin >> id;
if(id == -1){
cout << "deletetask quit!" << endl;
return false;
}
while (!std::cin || !task_list.Erase(id)) // 检查输入是否有效以及id是否存在
{ //在输入有效的情况下才会执行Erase并判断返回值
if(!std::cin){
std::cin.clear(); // 清除输入流状态标志
std::cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略剩余的输入
std::cout << "Invalid input. Please enter a valid ID: \nEnter -1 to quit!" << endl;
std::cin >> id;
if(id == -1){
cout << "deletetask quit!" << endl;
return false;
}
}
else if(!task_list.Erase(id)){
std::cin.clear(); // 清除输入流状态标志
std::cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略剩余的输入
std::cout << "Can not find this id. Please enter a valid ID: \nEnter -1 to quit!" << endl;
std::cin >> id;
if(id == -1){
cout << "deletetask quit!" << endl;
return false;
}
}
}
}
else
{
std::cout << "Please input the name of the task to be deleted\nEnter -1 to quit!" << endl; // 根据名称删除任务
string taskName;
std::getline(cin, taskName);
if(should_quit(taskName)){
cout << "deletetask quit!" << endl;
return false;
}
while (!task_list.Erase(taskName))
{
std::cin.clear(); // 清除输入流状态标志
std::cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略剩余的输入
std::cout << "Can not find this name. Please enter a valid name: \nEnter q or quit to quit!" << endl;
std::getline(cin, taskName);
if(should_quit(taskName)){
cout << "deletetask quit!" << endl;
return false;
}
}
}
task_list.saveFile();//保存文件
return true;
}
bool ShowTaskNoOp( TaskList& task_list){
std::cout << "If you want to quit, input -1!" << endl;
std::cout << "If you want to see all the tasks, input 0" << endl;
std::cout << "If you need to select according to the importance of the task, input 1" << endl;
std::cout << "If you need to select according to the time of the task, input 2" << endl;
std::cout << "If you need to select according to both the time and importance of the task, input 3" << endl;
std::cout << "default: show all" << endl;
int option;
std::cin >> option;
if(option == -1){
cout << "showtask quit!" << endl;
return false;
}
while (!std::cin) // 检查输入是否有效
{
std::cin.clear(); // 清除输入流状态标志
std::cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略剩余的输入
std::cout << "Invalid input. Please enter a valid choice: " << endl;
std::cin >> option;
if(option == -1){
cout << "showtask quit!" << endl;
return false;
}
}
int priority = 7;
int startTime = 0;
string startTimeStr = "";
string endTimeStr = "";
string c;//由用户决定是否要设定时间
int endTime = pow(2, 31) - 1;
switch (option)
{
case 0:
task_list.Show(); // 显示所有任务
break;
case 1:
std::cout << "1 for low, 2 for medium, 4 for high" << endl;
std::cout << "3 for low and medium, 6 for medium and high, 5 for low and high" << endl;
std::cout << "7 for all" << endl << "-1 for quit" << endl;
std::cin >> priority;
if(priority == -1){
cout << "showtask quit!" << endl;
return false;
}
while (priority > 7 || priority < 1 || !std::cin) // 检查输入是否有效
{
std::cin.clear(); // 清除输入流状态标志
std::cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略剩余的输入
std::cout << "Invalid input. Please enter a valid choice: " << endl;
std::cin >> priority;
if(priority == -1){
cout << "showtask quit!" << endl;
return false;
}
}
task_list.Show(0, pow(2, 31) - 1, priority); // 根据优先级显示任务
break;
case 2:
std::cout << "If you want to see all the tasks before the specified date, enter y/n" << endl;
std::cout << "format: 2022/02/02/10:00:00" << endl;
std::cout << "Enter q or quit to quit!" << endl;
std::cin >> c;
if(should_quit(c)){
cout << "showtask quit!" << endl;
return false;
}
if (c == "y" || c == "Y") {
std::cout << "Please input the date\nEnter q or quit to quit!" << endl;
std::cin >> startTimeStr;
if(should_quit(startTimeStr)){
cout << "showtask quit!" << endl;
return false;
}
while (!isValidDate(startTimeStr))
{
std::cout << "Invalid input, please try again\nEnter q or quit to quit!" << endl;
std::cin >> startTimeStr;
if(should_quit(startTimeStr)){
cout << "showtask quit!" << endl;
return false;
}
}
}
if (startTimeStr != "")
startTime = to_time_t(startTimeStr);
std::cout << "If you want to see all the tasks after the specified date, enter y/n" << endl;
std::cout << "format: 2022/02/02/10:00:00" << endl;
std::cout << "Enter q or quit to quit!" << endl;
std::cin >> c;
if(should_quit(c)){
cout << "showtask quit!" << endl;
return false;
}
if (c == "y" || c == "Y")
{
std::cout << "Please input the date\nEnter q or quit to quit!" << endl;
std::cin >> endTimeStr;
if(should_quit(endTimeStr)){
cout << "showtask quit!" << endl;
return false;
}
while (!isValidDate(endTimeStr))
{
std::cout << "Invalid input, please try again\nEnter q or quit to quit!" << endl;