-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
411 lines (368 loc) · 11.9 KB
/
main.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
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
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <iomanip>
#include <chrono>
#include <set>
#include <queue>
#include <fstream>
#include <iomanip>
#include <unistd.h>
#include <cstdio>
#include <stack>
#include <regex>
using namespace std;
#define ll long long int
#define pii pair<int, int>
#define psi pair<string, int>
#define pss pair<string, string>
#define vi vector <int>
#define vll vector <ll>
#define vd vector <double>
#define vs vector <string>
#define vpii vector <pii>
#define vpsi vector <psi>
#define vpss vector <pss>
#define ull unsigned long long
#define fff(itr, lim) for (int itr = 0; itr < int(lim); ++itr)
#define ffff(itr, lim) for (int itr = 1; itr <= int(lim); ++itr)
#define rrr(itr, lim) for (int itr = int(lim)-1; itr > -1; --itr)
const unsigned ll oo = 1e9 +5;
const int mx = 55;
string path_of(int id) {
return ("files/" + to_string(id) + ".txt");
}
pss find(string &s) {
int f;
fff(i, s.length()){
if (s[i] == ';'){
f = i;
break;
}
}
return make_pair(s.substr(0, f), s.substr(f+2, s.length()-f-2));
}
void append_to (string path, vs &lines) {
ofstream file(path, (ios::out | ios::app));
fff(i, lines.size()) {
file << lines[i];
file << '\n';
}
file.close();
}
int get_id (string path) {
int id;
ifstream id_file(path); id_file >> id; id_file.close();
fstream _uid(path, (ios::out | ios::trunc)); _uid << id+1; _uid.close();
return id;
}
//bool remove_from(string _path, int pos, int _id, int _uid/*int _id, int pos, int _uid*/) {
// ifstream file_to_in(_path); int id, uid; string line; vs lines;
// while (getline(file_to_in, line)) {
// istringstream s(line);
// if (pos == 1) {
// s >> id >> uid;
// if (id == _id) {
// if (uid != _uid) {
// cout << "*********************" << endl;
// cout << "****not yours ;*)****" << endl;
// cout << "*********************" << endl;
// file_to_in.close();
// return false;
// }
// } else {
// lines.push_back(line);
// }
// } else {
// fff(i, 3) s >> id;
// if (id != _id) lines.push_back(line);
// }
// }
// file_to_in.close();
// fstream file_to_out(_path, (ios::out | ios::trunc));
// fff(i, lines.size()) {
// file_to_out << lines[i];
// file_to_out << '\n';
// }
// file_to_out.close();
// return true;
//}
bool belong_to(string path, int id, int u_id, bool skip_fr) {
ifstream file(path);
string line;
int _id, _u_id;
if (skip_fr) getline(file, line);
while (getline(file, line)) {
istringstream s(line); s >> _id >> _u_id;
if (id == _id) {
file.close();
return (u_id == _u_id);
}
}
if (file.is_open()) file.close();
return false;
}
void remove_from(string path, int id, bool skip_fr) {
ifstream file(path); string line; int _id; vs lines;
if (skip_fr) {
getline(file, line);
lines.push_back(line);
}
while (getline(file, line)) {
istringstream s(line); s >> _id;
if (id != _id) lines.push_back(line);
}
file.close();
fstream file_to_out(path, (ios::out | ios::trunc));
fff(i, lines.size()) {
file_to_out << lines[i];
file_to_out << '\n';
}
file_to_out.close();
}
void put_to (string path, string line) {
ofstream file(path);
file << line;
file << '\n';
file.close();
}
struct reply {
int id, user_id, question_id;
string body;
reply (int _id, int _user_id, int _question_id, string _body) {
body = _body;
user_id = _user_id;
id = _id;
question_id = _question_id;
}
};
struct question {
int id, user_id;
string subject, body;
vector<reply> replies;
question(int _id, int _user_id, string _subject, string _body) {
subject = _subject;
body = _body;
user_id = _user_id;
id = _id;
}
question() {
id = -1;
}
};
struct user {
int id;
string name, email, password;
vector <question> questions;
user(){
name = "not found";
email = "undefined";
password = "***";
id = -1;
}
user(int _id, string _name, string _email, string _password) {
name = _name;
email = _email;
password = _password;
id = _id;
}
user(string _name, string _email, string _password) {
name = _name;
email = _email;
password = _password;
}
void save() {
id = get_id("files/user_id.txt");
vs lines; string line;
line = to_string(id) + ' ' + name + ' ' + email + ' ' + password;
lines.push_back(line);
append_to("files/users.txt", lines);
}
bool log_in(string _email, string _password) {
ifstream file("files/users.txt"); string line;
while (getline(file, line)) {
istringstream str(line);
str >> id >> name >> email >> password;
if (email == _email && password == _password) return true;
}
return false;
}
};
class contaner {
public:
user user_on;
contaner () {
user_on = user();
char c{};
do {
cout << "===================================================>>>>" << endl;
cout << "0: quit\n" << ((user_on.id != -1) ?
"1: myfeed\n2: specific feed\n3: ask question\n4: reply to\n5: remove reply\n6: remove thred\n7: get all feed\n8: log out"
: "1: log in\n2: sign up") << "\n>> ";
cin >> c;
if (user_on.id != -1) {
if (c == '1')
show_my_feed(0);
else if (c == '2')
show_my_feed(-1);
else if (c == '3')
add_question();
else if (c == '4')
reply_to();
else if (c == '5')
remove_reply();
else if (c == '6')
remove_thred();
else if (c == '7')
show_all_feed();
else if (c == '8')
logout();
} else {
if (c == '1')
user_on = login();
else if (c == '2')
user_on = sign_up();
}
cout << "===================================================>>>>" << endl;
} while (c != '0');
}
private:
user login () {
string _email, _password;
user _user_on;
while (true) {
cout << "inter your email & password ?> ";
cin >> _email >> _password;
if (_user_on.log_in(_email, _password)) {
cout << "**>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << endl;
cout << "****hello " << _user_on.name << " your id is " << _user_on.id << "****" << endl;
cout << "**>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << endl;
return _user_on;
}
cout << "Not found son of a bitch .. try fuckin' again" << endl;
}
}
void logout () {
user_on.id = -1;
cout << "*********************" << endl;
cout << "****you loged out****" << endl;
cout << "*********************" << endl;
}
user sign_up () {
string email, password, name;
cout << "inter your name, email & password ?> ";
cin >> name >> email >> password;
user _user_on(name, email, password);
_user_on.save();
cout << "**>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << endl;
cout << "****hello " << _user_on.name << " your id is " << _user_on.id << "****" << endl;
cout << "**>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << endl;
return _user_on;
}
void show_all_feed () {
string line; ifstream questions("files/questions.txt");
int q_id, u_id, ann;
while (getline(questions, line)) {
istringstream s(line); s >> q_id >> u_id >> ann;
if (ann == 0 || u_id == user_on.id || ann == user_on.id) {
ifstream ques(path_of(q_id)); getline(ques, line);
cout << "+++----question-----+++" << endl;
cout << ">>> " << line << endl;
cout << "-------replies--------" << endl;
while (getline(ques, line)) cout << "> " << line << endl;
ques.close();
cout << "\n\n" << endl;
}
}
questions.close();
}
void show_my_feed (int f) {
string line; ifstream questions("files/questions.txt");
int q_id, u_id, _u_id;
if (f == 0) _u_id = user_on.id;
else {
cout << "enter a user id ?> ";
cin >> _u_id;
}
while (getline(questions, line)) {
istringstream s(line); s >> q_id >> u_id;
if (_u_id == u_id) {
ifstream ques(path_of(q_id)); getline(ques, line);
cout << "+++----question-----+++" << endl;
cout << ">>> " << line << endl;
cout << "-------replies--------" << endl;
while (getline(ques, line)) cout << "> " << line << endl;
ques.close();
cout << "\n\n" << endl;
}
}
questions.close();
}
void add_question() {
string ques, path; int id, ann;
id = get_id("files/question_id.txt");
cout << "enter id for ann 0 for all then your question_subject; question_body ?> ";
cin >> ann;
getline(cin, ques);
vs lins; lins.push_back(to_string(id) + " " + to_string(user_on.id) + " " + to_string(ann));
append_to("files/questions.txt", lins);
ques = to_string(id) + " " + to_string(user_on.id) + " " + to_string(ann) + ' ' + ques;
path = path_of(id);
put_to(path, ques);
cout << ">>>>> you added question of id " << id << " <<<<<<<" << endl;
}
void reply_to() {
string rep; int id = get_id("files/reply_id.txt"), q_id;
cout << "enter your question_id & question_reply ?> ";
cin >> q_id;
getline(cin, rep);
vs lines; lines.push_back(to_string(id) + " " + to_string(user_on.id) + " " + to_string(q_id) + rep);
rep = path_of(q_id);
append_to(rep, lines);
cout << "+++----you added comment of id " << id << " to question id " << q_id << "-----+++" << endl;
}
void remove_reply() {
int r_id, q_id;
cout << "to remove reply enter your question id & reply id ?> ";
cin >> q_id >> r_id;
string path = path_of(q_id);
char c;
if (belong_to(path, r_id, user_on.id, true)) {
cout << "line 390 >> ";
cin >> c;
remove_from(path, r_id, true);
cout << "*****you removed reply " << r_id << "******" << endl;
} else {
cout << "line 395 >> ";
cin >> c;
cout << "*********************" << endl;
cout << "****not yours ;*)****" << endl;
cout << "*********************" << endl;
}
}
void remove_thred() {
int q_id;
cout << "enter your thred id ?> "; cin >> q_id;
string path = path_of(q_id);
if (belong_to("files/questions.txt", q_id, user_on.id, false)) {
remove(path.c_str());
remove_from("files/questions.txt", q_id, false);
cout << "*****you removed thred " << q_id << "******" << endl;
} else {
cout << "*********************" << endl;
cout << "****not yours ;*)****" << endl;
cout << "*********************" << endl;
}
}
};
int main() {
cout << " welcome" << endl;
contaner st;
return 0;
}