-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV3.cpp
More file actions
222 lines (190 loc) · 5.94 KB
/
Copy pathV3.cpp
File metadata and controls
222 lines (190 loc) · 5.94 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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <limits>
using namespace std;
// FlashCard class - holds individual Q&A and stats
class FlashCard {
public:
string question;
string answer;
int difficultyScore; // Higher = 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;
}
// Sort by difficultyScore descending
sort(cards.begin(), cards.end(), [](FlashCard& a, FlashCard& b) {
return a.difficultyScore > b.difficultyScore;
});
for (auto& card : cards) {
card.show();
cout << "Press Enter to see the answer...";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();
card.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') {
card.difficultyScore++;
} else {
if (card.difficultyScore > 0) card.difficultyScore--;
}
}
}
};
// 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:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid option! Try again.\n";
}
} while (choice != 6);
}
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. 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;
}
};
// Entry point
int main() {
FlashCardApp app;
app.run();
return 1;
}