-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiary.cpp
123 lines (122 loc) · 3.26 KB
/
diary.cpp
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
//
// Created by SwJ on 2018/4/23.
//
//confusions:
//1.move; 2.ctor single parameter
#include "diary.h"
typedef std::vector<Note*>::size_type vnint;
using namespace std;
/* Note ****************************************************/
string Note::Getdate()const{
return date;
}
string Note::Getline(vsint i)const{
return content[i];
}
void Note::Setdate(string d){
date = move(d);
}
void Note::Setline(string s){
content.push_back(s);
}
vsint Note::num_lines(){
return content.size();
}
/* Diary ****************************************************/
Diary::Diary(){
Note *mynote;
string date;
string line;
ifstream fp(fpath);
while(fp.peek()!=EOF){//read from diary.txt
getline(fp,date);
mynote = new Note;
mynote->Setdate(date);
getline(fp,line);
while(line!="."){
mynote->Setline(line);
getline(fp,line);
}
notes.push_back(mynote);
}
fp.close();
}
void Diary::pdadd(string d){
Note *mynote;
vnint i;
string line;
mynote = new Note;
mynote->Setdate(d);
getline(cin,line);
while(line!="."){// read inputs from sdtin
mynote->Setline(line);
if(!getline(cin,line))
break;
}
for(i = 0; i < notes.size(); i++){
if(d == notes[i]->Getdate()){//update
notes[i] = mynote;
break;
}
else if(d.compare(notes[i]->Getdate())<0){//insert
notes.insert(notes.begin()+i,mynote);
break;
}
}
if(i==notes.size())//push into the last one
notes.push_back(mynote);
writeback();
}
void Diary::pdlist(){
vector<Note*>::iterator p;
for(p = notes.begin(); p < notes.end(); p++){
cout << (*p)->Getdate() << endl;
cout << (*p)->Getline(0) << endl;
}
}
void Diary::pdlist(string begin, string end){
vector<Note*>::iterator p;
for(p = notes.begin(); p < notes.end(); p++)
if((*p)->Getdate().compare(begin)>=0&&(*p)->Getdate().compare(end)<=0){
cout << (*p)->Getdate() << endl;
cout << (*p)->Getline(0) << endl;
}
}
void Diary::pdshow(string d){
vector<Note*>::iterator p;
vsint i;
for(p = notes.begin(); p < notes.end(); p++)
if(d == (*p)->Getdate())
break;
if(p == notes.end())
cout << "Nothing is written on that date!" << endl;
else{
cout << (*p)->Getdate() << endl;
for(i = 0; i < (*p)->num_lines(); i++)
cout << (*p)->Getline(i) << endl;
}
}
void Diary::pdremove(string d){
vector<Note*>::iterator p;
vsint i;
for(p = notes.begin(); p < notes.end(); p++)
if(d == (*p)->Getdate())
break;
if(p == notes.end())
cout << "Nothing is written on that date!" << endl;
else
notes.erase(p);
writeback();
}
void Diary::writeback(){
ofstream fp(fpath);
vsint i;
vector<Note*>::iterator p;
for(p = notes.begin(); p < notes.end(); p++){//write into diary.txt
fp << (*p)->Getdate() << endl;
for(i = 0; i < (*p)->num_lines(); i++)
fp << (*p)->Getline(i) << endl;
fp << "." << endl;
}
fp.close();
}