-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV4.cpp
More file actions
286 lines (244 loc) · 8.32 KB
/
Copy pathV4.cpp
File metadata and controls
286 lines (244 loc) · 8.32 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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <limits>
#include <cstdlib> // for rand
#include <ctime> // for seeding rand
using namespace std;
// FlashCard class - holds individual Q&A and stats
class FlashCard {
public:
string question;
string answer;
int difficultyScore; // Lower (even negative) = harder to recall
FlashCard() : difficultyScore(0) {}
FlashCard(string q, string a) : question(q), answer(a), difficultyScore(0) {}
void show() const {
cout << "Q: " << question << endl;
}
void showAnswer() const {
cout << "A: " << answer << endl;
}
};
// Deck manager
class FlashCardDeck {
private:
vector<FlashCard> cards;
public:
void addCard(const FlashCard& card) {
cards.push_back(card);
}
vector<FlashCard>& getCards() {
return cards;
}
const vector<FlashCard>& getCards() const {
return cards;
}
void listCards() {
if (cards.empty()) {
cout << "No cards available." << endl;
return;
}
cout << "Total flashcards: " << cards.size() << endl;
for (size_t i = 0; i < cards.size(); ++i) {
cout << i + 1 << ". " << cards[i].question << endl;
}
}
};
// File manager class
class FileManager {
public:
static void save(const FlashCardDeck& deck, const string& filename) {
ofstream out(filename, ios::binary);
if (!out) {
cout << "Error saving file!" << endl;
return;
}
const vector<FlashCard>& cards = deck.getCards();
size_t size = cards.size();
out.write((char*)&size, sizeof(size));
for (const auto& card : cards) {
size_t qlen = card.question.size();
size_t alen = card.answer.size();
out.write((char*)&qlen, sizeof(qlen));
out.write(card.question.c_str(), qlen);
out.write((char*)&alen, sizeof(alen));
out.write(card.answer.c_str(), alen);
out.write((char*)&card.difficultyScore, sizeof(int));
}
out.close();
cout << "Cards saved successfully!" << endl;
}
static void load(FlashCardDeck& deck, const string& filename) {
ifstream in(filename, ios::binary);
if (!in) {
cout << "File not found." << endl;
return;
}
size_t size;
in.read((char*)&size, sizeof(size));
for (size_t i = 0; i < size; ++i) {
size_t qlen, alen;
in.read((char*)&qlen, sizeof(qlen));
string q(qlen, ' ');
in.read(&q[0], qlen);
in.read((char*)&alen, sizeof(alen));
string a(alen, ' ');
in.read(&a[0], alen);
int diff;
in.read((char*)&diff, sizeof(int));
FlashCard card(q, a);
card.difficultyScore = diff;
deck.addCard(card);
}
in.close();
cout << "Cards loaded successfully!" << endl;
}
};
// Review engine with spaced repetition
class SpacedRepetitionEngine {
public:
static void reviewCards(FlashCardDeck& deck) {
vector<FlashCard>& cards = deck.getCards();
if (cards.empty()) {
cout << "No cards to review!" << endl;
return;
}
srand(time(nullptr));
random_shuffle(cards.begin(), cards.end()); // Shuffle for variety
// Sort by increasing difficultyScore (more negative = harder)
sort(cards.begin(), cards.end(), [](FlashCard& a, FlashCard& b) {
return a.difficultyScore < b.difficultyScore;
});
for (size_t i = 0; i < cards.size(); ++i) {
cout << "\nCard " << i + 1 << " of " << cards.size() << endl;
cards[i].show();
cout << "Press Enter to see the answer...";
string dummy;
getline(cin, dummy);
cards[i].showAnswer();
cout << "Did you remember this card? (y/n): ";
char choice;
cin >> choice;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (choice == 'n' || choice == 'N') {
cards[i].difficultyScore = max(cards[i].difficultyScore - 1, -10);
} else {
cards[i].difficultyScore = min(cards[i].difficultyScore + 1, 10);
}
}
}
};
// User interaction handler
class FlashCardApp {
private:
FlashCardDeck deck;
const string filename = "flashcards.dat";
public:
void run() {
int choice;
do {
showMenu();
cin >> choice;
cin.ignore();
switch (choice) {
case 1:
addFlashCard();
break;
case 2:
deck.listCards();
break;
case 3:
FileManager::save(deck, filename);
break;
case 4:
FileManager::load(deck, filename);
break;
case 5:
SpacedRepetitionEngine::reviewCards(deck);
break;
case 6:
deleteFlashCard();
break;
case 7:
editFlashCard();
break;
case 8:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid option! Try again.\n";
}
} while (choice != 8);
}
void showMenu() {
cout << "\n=== Digital Flash Card App ===\n";
cout << "1. Add New Flashcard\n";
cout << "2. List All Flashcards\n";
cout << "3. Save Flashcards to File\n";
cout << "4. Load Flashcards from File\n";
cout << "5. Review Flashcards\n";
cout << "6. Delete Flashcard by Index\n";
cout << "7. Edit Flashcard\n";
cout << "8. Exit\n";
cout << "Choose an option: ";
}
void addFlashCard() {
string q, a;
cout << "Enter Question: ";
getline(cin, q);
cout << "Enter Answer: ";
getline(cin, a);
deck.addCard(FlashCard(q, a));
cout << "Flashcard added!" << endl;
}
void deleteFlashCard() {
deck.listCards();
if (deck.getCards().empty()) return;
int index;
cout << "Enter card number to delete: ";
cin >> index;
cin.ignore();
if (index < 1 || index > deck.getCards().size()) {
cout << "Invalid index!" << endl;
return;
}
deck.getCards().erase(deck.getCards().begin() + index - 1);
cout << "Card deleted." << endl;
}
void editFlashCard() {
deck.listCards();
if (deck.getCards().empty()) return;
int index;
cout << "Enter card number to edit: ";
cin >> index;
cin.ignore();
if (index < 1 || index > deck.getCards().size()) {
cout << "Invalid index!" << endl;
return;
}
FlashCard& card = deck.getCards()[index - 1];
cout << "Current Question: " << card.question << "\nEnter new question (leave empty to keep): ";
string q;
getline(cin, q);
// Trim leading/trailing whitespace
q.erase(q.begin(), find_if(q.begin(), q.end(), [](unsigned char ch) { return !isspace(ch); }));
q.erase(find_if(q.rbegin(), q.rend(), [](unsigned char ch) { return !isspace(ch); }).base(), q.end());
if (!q.empty()) card.question = q;
cout << "Current Answer: " << card.answer << "\nEnter new answer (leave empty to keep): ";
string a;
getline(cin, a);
a.erase(a.begin(), find_if(a.begin(), a.end(), [](unsigned char ch) { return !isspace(ch); }));
a.erase(find_if(a.rbegin(), a.rend(), [](unsigned char ch) { return !isspace(ch); }).base(), a.end());
if (!a.empty()) card.answer = a;
cout << "Card updated." << endl;
}
};
// Entry point
int main() {
FlashCardApp app;
app.run();
return 0;
}