-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStudentManager.cpp
More file actions
287 lines (287 loc) · 7.55 KB
/
StudentManager.cpp
File metadata and controls
287 lines (287 loc) · 7.55 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
287
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
int listsize,idcount;
struct student{
int id;
int no;
string name;
};
struct node{
student data;
node *next;
};
string mask(){
string password="";
char ch;
cout<<"Enter your password: "<<flush;
while(1){
ch=_getch();
if(ch==13||ch==10){
cout<<endl;
break;
}
else if(ch==8||ch==127){
if(password.length()>0){
password.pop_back();
cout<<"\b \b"<<flush;
}
}
else{
password+=ch;
cout<<'*'<<flush;
}
}
return password;
}
bool verify() {
string password=mask();
if(password=="lhm") {
return true;
} else {
cout << "Wrong password!" << endl;
return false;
}
}
int menu() {
system("cls");
cout << "========== Students Manager ==========\n";
cout << "1. Add\n";
cout << "2. Delete\n";
cout << "3. Search\n";
cout << "4. Display\n";
cout << "5. Modify\n";
cout<<"6. Count\n";
cout<<"7. Sort\n";
cout << "8. Save\n";
cout << "9. Load\n";
cout << "0. Exit\n";
cout << "==================================\n";
cout << "Enter your choice: "<<flush;
int choice=_getch()-'0';
cout<<endl;
return choice;
}
node *initialize(){
node *head=new node;
head->data.id=0;
head->data.no=-1;
head->data.name="head";
head->next=NULL;
return head;
}
void add(node *cur){
node *temp=new node;
temp->next=NULL;
cout<<"Enter number: ";
cin>>temp->data.no;
cout<<"Enter name: ";
cin>>temp->data.name;
temp->data.id=++idcount;
while(cur->next!=NULL){
cur=cur->next;
}
cur->next=temp;
++listsize;
cout<<"ID: "<<idcount<<" added successfully"<<endl;
}
void del(node *cur){
cout<<"ID to be deleted: ";
int id;
cin>>id;
if(id<=0||id>idcount){
cout<<"ID: "<<id<<" not found"<<endl;
return;
}
while(cur->next!=NULL){
if(cur->next->data.id==id){
node *temp=cur->next;
cur->next=cur->next->next;
delete temp;
--listsize;
cout<<"Deleted successfully"<<endl;
return;
}
else{
cur=cur->next;
}
}
cout<<"ID: "<<id<<" not found"<<endl;
}
void search(node *cur){
cout<<"ID to be searched: ";
int id;
cin>>id;
while(cur->next!=NULL){
if(cur->next->data.id==id){
cout<<"ID Number Name"<<endl;
cout<<cur->next->data.id<<" "<<cur->next->data.no<<" "<<cur->next->data.name<<endl;
return;
}
else{
cur=cur->next;
}
}
cout<<"ID: "<<id<<" not found"<<endl;
}
void display(node *cur){
cout<<"ID Number Name"<<endl;
cur=cur->next;
while(cur!=NULL){
cout<<cur->data.id<<" "<<cur->data.no<<" "<<cur->data.name<<endl;
cur=cur->next;
}
}
void modify(node *cur){
cout<<"ID to be modified: ";
int id;
cin>>id;
if(id<=0||id>idcount){
cout<<"ID: "<<id<<" not found"<<endl;
return;
}
while(cur->next!=NULL){
if(cur->next->data.id==id){
cout<<"Enter new number: ";
cin>>cur->next->data.no;
cout<<"Enter new name: ";
cin>>cur->next->data.name;
cout<<"ID: "<<id<<" modified successfully"<<endl;
return;
}
else{
cur=cur->next;
}
}
cout<<"ID: "<<id<<" not found"<<endl;
}
node *sort(node *old){
node *head=initialize(),*minp=old,*newcur=head,*oldcur=old;
if(listsize>0){
for(int i=0;i<listsize;++i){
int minno=old->next->data.no;
oldcur=old;
minp=old;
while(oldcur->next!=NULL){
if(oldcur->next->data.no<minno){
minp=oldcur;
minno=minp->next->data.no;
}
oldcur=oldcur->next;
}
while(newcur->next!=NULL){
newcur=newcur->next;
}
newcur->next=minp->next;
minp->next=minp->next->next;
newcur->next->next=NULL;
}
delete old;
cout<<"Sorted successfully"<<endl;
return head;
}
else{
cout<<"No data to sort"<<endl;
return old;
}
}
void save(node *cur){
ofstream outfile("students.txt");
outfile << listsize << "," << idcount << endl;
cur=cur->next;
while(cur!=NULL){
outfile << cur->data.id << ","<< cur->data.no << ","<< cur->data.name << endl;
cur=cur->next;
}
cout << "Saved successfully! Total students: " << listsize << ", Last ID: " << idcount << endl;
}
void reset(node *head) {
node *current = head;
while (current != NULL) {
node *next = current->next;
delete current;
current = next;
idcount=0;
listsize=0;
}
}
node *load(){
ifstream infile("students.txt");
if(!infile){
cout<<"Cannot open file for reading"<<endl;
cout<<"No saved data found. Creating new list."<<endl;
return initialize();
}
string firstLine;
getline(infile, firstLine);
size_t commaPos = firstLine.find(",");
if(commaPos == string::npos){
cout << "Error: Invalid file format!" << endl;
infile.close();
return initialize();
}
listsize = stoi(firstLine.substr(0, commaPos));
int savedIdCount = stoi(firstLine.substr(commaPos + 1));
node *head = initialize();
node *current = head;
string line;
int studentsLoaded = 0;
int actualMaxId = 0;
while(getline(infile, line)){
if(line.empty()){
continue;
}
size_t pos1 = line.find(",");
size_t pos2 = line.find(",", pos1 + 1);
if(pos1 != string::npos && pos2 != string::npos){
node *newNode = new node;
newNode->next = NULL;
newNode->data.id = stoi(line.substr(0, pos1));
newNode->data.no = stoi(line.substr(pos1 + 1, pos2 - pos1 - 1));
newNode->data.name = line.substr(pos2 + 1);
if(newNode->data.id > actualMaxId){
actualMaxId = newNode->data.id;
}
current->next = newNode;
current = newNode;
++studentsLoaded;
}
}
infile.close();
idcount = actualMaxId;
if(studentsLoaded != listsize){
cout << "Warning: Expected " << listsize << " students, but loaded " << studentsLoaded << endl;
listsize = studentsLoaded;
}
if(savedIdCount != actualMaxId){
cout << "Note: File's idcount was " << savedIdCount << ", but actual max ID is " << actualMaxId << endl;
}
cout << "Loaded successfully! Total students: " << listsize << ", Last ID: " << idcount << endl;
return head;
}
int main(){
ios::sync_with_stdio(false);
if(!verify()){
system("pause");
return 0;
}
node *head=initialize();
while(1){
int choice=menu();
switch(choice){
case 0:reset(head);return 0;
case 1:add(head);break;
case 2:del(head);break;
case 3:search(head);break;
case 4:display(head);break;
case 5:modify(head);break;
case 6:cout<<"Total number of students: "<<listsize<<endl;break;
case 7:head=sort(head);break;
case 8:save(head);break;
case 9:reset(head);head=load();break;
default:cout<<"Invalid choice"<<endl;break;
}
system("pause");
}
}