-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathf06.cpp
More file actions
243 lines (223 loc) · 4.29 KB
/
f06.cpp
File metadata and controls
243 lines (223 loc) · 4.29 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
//LAB PROGRAM 6. Write a C++ program to implement index on secondary key, the name, for a file of student objects. Implement add ( ), search ( ), delete ( ) using the secondary index.
//use common search for search and delete.
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
class student
{
public:
string usn, name, sem;
void enter_data();
void pack();
}s1;
struct sec_index
{
string usn, name;
int addr;
}i2[100], found[20];
int cnt, find_cnt;
fstream fp;
int indexnums[20];
void create_index();
void sort_index();
void search();
void lin_srch(string);
void del();
void error(int);
int main()
{
int choice;
system("clear");
fp.open("record6.txt", ios::out|ios::app);
fp.close();
create_index();
while(1)
{
cout<<"1. to Add Record\n2. to Search for a record\n3. to delete a record\n4. Exit\n\nEnter choice : ";
cin>>choice;
switch(choice)
{
case 1:
s1.enter_data();
fp.open("record6.txt",ios::out|ios::app);
if(!fp)
error(1);
s1.pack();
fp.close();
break;
case 2:
search();
break;
case 3:
del();
break;
default:
exit(0);
}
}
}
void create_index()
{
int pos, i;
string seg, usnbuf, namebuf;
cnt = -1;
fp.open("record6.txt", ios::in);
if(!fp)
error(1);
while(fp)
{
usnbuf.erase();
namebuf.erase();
pos = fp.tellg();
getline(fp, usnbuf, '|');
getline(fp, namebuf, '|');
getline(fp,seg);
if(usnbuf[0] == '*'||usnbuf.length()==0||namebuf.length()==0)
continue;
cnt++;
i2[cnt].usn = usnbuf;
i2[cnt].name = namebuf;
i2[cnt].addr = pos;
}
fp.close();
sort_index();
}
void sort_index()
{
struct sec_index temp;
for(int i=0; i<=cnt; i++)
{
for(int j=i+1; j<=cnt; j++)
if(i2[i].name>i2[j].name)
{
temp.usn = i2[i].usn;
temp.name = i2[i].name;
temp.addr = i2[i].addr;
i2[i].usn = i2[j].usn;
i2[i].name = i2[j].name;
i2[i].addr = i2[j].addr;
i2[j].usn = temp.usn;
i2[j].name = temp.name;
i2[j].addr = temp.addr;
}
}
}
void student::enter_data()
{
cout<<"\nEnter usn: ";
cin>>usn;
cout<<"\nEnter name: ";
cin>>name;
cout<<"\nEnter sem: ";
cin>>sem;
}
void student::pack()
{
int pos = fp.tellp();
string buf = usn + "|" + name + "|" + sem + "|";
fp<<buf<<endl;
cnt++;
i2[cnt].addr =pos;
i2[cnt].usn = usn;
i2[cnt].name = name;
sort_index();
}
void lin_srch(string name_srch)
{
find_cnt = 0;int j=0;
for(int i = 0; i<=cnt; i++)
{
if(i2[i].name == name_srch)
{
indexnums[j++]=i;
found[find_cnt].usn = i2[i].usn;
found[find_cnt].name = i2[i].name;
found[find_cnt].addr = i2[i].addr;
find_cnt++;
}
}
}
void search()
{
string name_srch, buf;
int ch;
cout<<"\nEnter the Name of The student to be searched : ";
cin>>name_srch;
lin_srch(name_srch);
if(find_cnt == 0)
{
cout<<"\nRecord Not Found!\n";
return;
}
if(find_cnt == 1)
{
cout<<"\n1 Record Found\n";
ch = 0;
}
if(find_cnt>1)
{
cout<<"\nMultiple Records Found!\n";
for(int i = 0; i< find_cnt; i++)
cout<<i<<". Usn = "<<found[i].usn<<endl;
cout<<"\nEnter choice: ";
cin>>ch;if(ch>find_cnt){cout<<"Invalid Range\n\n";return;}
}
cout<<"\nUsn|Name|Sem"<<endl;
fp.open("record6.txt",ios::in);
if(!fp)
error(1);
fp.seekg(found[ch].addr, ios::beg);
getline(fp,buf);
fp.close();
cout<<buf<<endl;
}
void del()
{
string name_srch;
int ch;
cout<<"\nEnter the Name of The student to be deleted : ";
cin>>name_srch;
lin_srch(name_srch);
if(find_cnt == 0)
{
cout<<"\nRecord Not Found!\n";
return;
}
if(find_cnt == 1)
{
cout<<"\n1 Record Found\n";
ch = 0;
}
if(find_cnt>1)
{
cout<<"\nMultiple Records Found!\n";
for(int i = 0; i< find_cnt; i++)
cout<<i<<". "<<found[i].usn<<endl;
cout<<"\nEnter choice: \n";
cin>>ch;if(ch>find_cnt){cout<<"Invalid Range\n\n";return;}
}
cout<<"\nRecord Deleted\n\n";
fp.open("record6.txt",ios::out|ios::in);
if(!fp)
error(1);
fp.seekp(found[ch].addr, ios::beg);
fp.put('*');
fp.close();
for(int i=indexnums[ch]; i<cnt;i++)
{
i2[i].usn = i2[i+1].usn;
i2[i].name = i2[i+1].name;
i2[i].addr = i2[i+1].addr;
}
cnt--;
}
void error(int error_type)
{
switch(error_type)
{
case 1: cout<<"\nFATAL ERROR!: Unable to open the record File\n";
exit(0);
}
}