-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLib.cpp
178 lines (166 loc) · 3.21 KB
/
Lib.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
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
#include "Lib.h"
Date::Date()
{
time_t now = time(NULL);
tm *local = localtime(&now);
year = local->tm_year + 1900;
month = local->tm_mon + 1;
day = local->tm_mday;
week = weekToStr(local->tm_wday);
hour = local->tm_hour;
min = local->tm_min;
sec = local->tm_sec;
}
Date::Date(const vector<string> &list)
{
stringstream buffer;
buffer << list[2];
buffer >> year;
buffer.clear();
buffer << list[3];
buffer >> month;
buffer.clear();
buffer << list[4];
buffer >> day;
buffer.clear();
buffer << list[5];
buffer >> hour;
buffer.clear();
buffer << list[6];
buffer >> min;
buffer.clear();
buffer << list[7];
buffer >> sec;
week = list[8];
}
string Date::weekToStr(int week) const
{
string wk;
switch (week) {
case 0:
wk.assign("Sunday");
return wk;
case 1:
wk.assign("Monday");
return wk;
case 2:
wk.assign("Tuesday");
return wk;
case 3:
wk.assign("Wednesday");
return wk;
case 4:
wk.assign("Thursday");
return wk;
case 5:
wk.assign("Friday");
return wk;
case 6:
wk.assign("Saturday");
return wk;
}
}
ostream& operator<<(ostream &out, const Date *dt)
{
out << setiosflags(ios::right) << setfill('0')
<< dt->year << "-"
<< setw(2) << dt->month << "-"
<< setw(2) << dt->day << " "
<< dt->week << " "
<< setw(2) << dt->hour << ":"
<< setw(2) << dt->min << ":"
<< setw(2) << dt->sec
<< resetiosflags(ios::right) << setfill(' ');
return out;
}
fstream& operator<<(fstream &out, const Date * dt)
{
out << dt->year << ";"
<< dt->month << ";"
<< dt->day << ";"
<< dt->hour << ";"
<< dt->min << ";"
<< dt->sec << ";"
<< dt->week << ";";
return out;
}
bool confirm(void)
{
cout << "Are you sure?[Y/N]" << endl;
string op;
cin >> op;
cin.ignore(0xffffff, '\n');
while (1) {
if (op[0] == 'Y' || op[0] == 'y') {
return 1;
} else if (op[0] == 'N' || op[0] == 'n') {
cout << "Operation canceled." << endl;
return 0;
} else {
cout << "[Y/N]?" << endl;
cin >> op;
cin.ignore(0xffffff, '\n');
}
}
}
bool checkInput(const string &input, int st, int ed)
{
for (string::const_iterator ch = input.begin(); ch != input.end(); ch++) {
if (!('0' <= *ch && *ch <= '9')) {
return false;
}
}
stringstream ss;
int num = 0;
ss << input;
ss >> num;
if (!(st <= num && num <= ed)) {
return false;
}
return true;
}
int askModifyMethod(void)
{
string input;
cout << "1.Change name.\n2.Change telephone number.\n3.Return\nYour choose:\n";
return getNumber(1, 3);
}
void split(vector<string> &list, string &str)
{
auto st = str.begin();
for (auto ch = str.begin(); ch != str.end(); ch++) {
if (*ch == ';') {
string frag(st, ch);
list.push_back(frag);
st = ch + 1;
}
}
}
void checkName(string &input)
{
while (input.find(';') != string::npos) {
cout << "\nName or telephone number can not include \";\", please input again:";
inputLineFromCin(input);
}
}
int getNumber(int st, int ed)
{
string input;
cin >> input;
cin.ignore(0xffffff, '\n');
while (!checkInput(input, st, ed)) {
cout << "Input error! Please enter a correct number." << endl;
cin >> input;
cin.ignore(0xffffff, '\n');
}
stringstream ss;
int num;
ss << input;
ss >> num;
return num;
}
void inputLineFromCin(string &str)
{
getline(cin, str);
//cin.ignore();
}