-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab2.cpp
371 lines (361 loc) · 8.35 KB
/
lab2.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
#include<iostream>
#include<string.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
#include<malloc.h>
using namespace std;
class student
{
public:
char *name;
int age;
int number;
int score;
};
class teacher
{
public:
char *name;
int number;
int age;
};
class node
{
private:
student *info;
node *next;
public:
node* create(student* g);
student* create(node* head);
void Insert(node*& head, student* g);
void ShowFind(node* head);
void Show(node* head);
node* Find(node* phead,int x);
void Delete(node*& head);
};
class node2
{
private:
teacher *info;
node2 *next;
public:
node2* create2(teacher* g);
teacher* create2(node2* head2);
void Insert2(node2*& head2, teacher* g);
void ShowFind2(node2* head2);
void Show2(node2* head2);
node2* Find2(node2* phead2,int x);
void Delete2(node2*& head2);
};
int getInt(int* a)
{
int n;
do {
n=scanf("%d",a);
if (n==0)
{
printf ("Error");
scanf ("%*[^\n]");
n=0;
}
}while (n==0);
return 1;
}
char *getstring()
{
int n, l;
char buf[80];
char *str = NULL, *p;
str = (char *)malloc(sizeof(char));
if (str == NULL) return NULL;
strcpy(str,"");
do{
n = scanf("%79[^\n]",buf);
if (n == -1)
{
free(str);
return NULL;
}
if (n == 0) scanf("%*c");
else
{
l = strlen(buf)+strlen(str)+1;
p = (char *)realloc(str,l);
if (p == NULL)
{
free(str); return NULL;
}
str = p;
strcat(str, buf);
}
}while(n!=0);
return str;
}
node* node::create(student* g)
{
node* p = new node;
if (p == NULL)
return 0;
p->info = g;
p->next = NULL;
return p;
}
node2* node2::create2(teacher* g)
{
node2* p = new node2;
if (p == NULL)
return 0;
p->info = g;
p->next = NULL;
return p;
}
student* node::create(node *head)
{
system("cls");
student* st = new student;
fflush(stdin);
printf("Enter name of student: ");
st->name=getstring();
printf("Enter age: ");
getInt(&st->age);
printf("Enter score of exam: ");
getInt(&st->score);
printf ("Enter number of student's record-book:");
while(1)
{
getInt(&st->number);
if (Find(head,st->number)) printf ("Number was exist! Enter new number: ");
else break;
}
return st;
}
teacher* node2::create2(node2 *head2)
{
system("cls");
teacher* st = new teacher;
fflush(stdin);
printf("Enter name of teacher: ");
st->name=getstring();
printf("Enter age: ");
getInt(&st->age);
printf ("Enter number of teacher:");
while(1)
{
getInt(&st->number);
if (Find2(head2,st->number)) printf ("Number was exist! Enter new number: ");
else break;
}
return st;
}
void node:: Insert(node*& head, student* g)
{
node* p = create(g);
if (head == NULL)
head = p;
else {
node* temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = p;
}
printf ("Added!");
getch();
}
void node2:: Insert2(node2*& head2, teacher* g)
{
node2* p = create2(g);
if (head2 == NULL)
head2 = p;
else {
node2* temp = head2;
while (temp->next != NULL)
temp = temp->next;
temp->next = p;
}
printf ("Added!");
getch();
}
void node:: Show(node* head)
{
system("cls");
printf ("%s\t\t%s\t%s\t\t%s\n","Name","Age","Number of student's record-book","Score of exam");
for (node* p = head; p != NULL; p = p->next)
{
printf("%s\t\t%d\t%d\t\t\t\t\t%d\n", p->info->name,p->info->age,p->info->number,p->info->score);
}
getch();
}
void node2:: Show2(node2* head2)
{
system("cls");
printf ("%s\t%s\t%s\n","Name","Age","Number of teacher");
for (node2* p = head2; p != NULL; p = p->next)
{
printf("%s\t%d\t%d\n", p->info->name,p->info->age,p->info->number);
}
getch();
}
node* node::Find(node* phead, int x)
{
for (node* p = phead; p != NULL; p = p->next)
if (p->info->number==x)
{
return p;
}
return NULL;
getch();
}
node2* node2::Find2(node2* phead2, int x)
{
for (node2* p = phead2; p != NULL; p = p->next)
if (p->info->number==x)
{
return p;
}
return NULL;
getch();
}
void node::Delete(node* &head)
{
system("cls");
node *p1=head, *p2;
int x;
printf("Enter number of student: ");
scanf("%d",&x);
while((p1!= NULL) && (p1->info->number!=x))
{
p2 = p1;
p1 = p1->next;
}
//p1=Find(head,x);
if(p1 == NULL)
printf("This number of student don't exist!\n");
else
{
if(p1 == head)
head = p1->next;
else
p2->next = p1->next;
free(p1);
printf("Deleted!\n");
}
getch();
}
void node2::Delete2(node2* &head2)
{
system("cls");
node2 *p1=head2, *p2;
int x;
printf("Enter number of teacher: ");
scanf("%d",&x);
while((p1!= NULL) && (p1->info->number!=x))
{
p2 = p1;
p1 = p1->next;
}
//p1=Find2(head2,x);
if(p1 == NULL)
printf("This number of teacher don't exist!\n");
else
{
if(p1 == head2)
head2 = p1->next;
else
p2->next = p1->next;
free(p1);
printf("Deleted!\n");
}
getch();
}
void node::ShowFind(node* phead)
{
system("cls");
int x;
printf("Enter number of student: ");
scanf("%d",&x);
node *p=Find(phead,x);
if (p==NULL) printf ("This number of student don't exist!");
else
{
printf ("%s\t\t%s\t%s\t\t%s\n","Name","Age","Number of student's record-book","Score of exam");
printf("%s\t\t%d\t%d\t\t\t\t\t%d\n", p->info->name,p->info->age,p->info->number,p->info->score);
}
getch();
}
void node2::ShowFind2(node2* phead2)
{
system("cls");
int x;
printf("Enter number of teacher: ");
scanf("%d",&x);
node2 *p=Find2(phead2,x);
if (p==NULL) printf ("This number of teacher don't exist!");
else
{
printf ("%s\t%s\t%s\n","Name","Age","Number of teacher");
printf("%s\t\t%d", p->info->name,p->info->age,p->info->number);
}
getch();
}
int main()
{
int a,c;
student* g;
teacher*t;
node T,*head=NULL;
node2 T2,*head2=NULL;
do{
system("cls");
printf ("Type of Elements \n");
printf ("1. Student\n");
printf ("2. Teacher\n");
getInt(&c);
}while (c!=2&&c!=1);
do {
system("cls");
printf("\n------------------MENU---------------------------\n");
printf("0. Quit\n");
printf("1. Add \n");
printf("2. Show\n");
printf("3. Find\n");
printf("4. Delete\n");
printf ("Your chose:");
getInt(&a);
if (c==1)
{
switch (a)
{
case 1:
{
g = T.create(head);
T.Insert(head,g);
break;
}
case 2: T.Show(head);break;
case 3: T.ShowFind(head);break;
case 4: T.Delete(head);break;
case 0:break;
}
}
if (c==2)
{
switch (a)
{
case 1:
{
t = T2.create2(head2);
T2.Insert2(head2,t);
break;
}
case 2: T2.Show2(head2);break;
case 3: T2.ShowFind2(head2);break;
case 4: T2.Delete2(head2);break;
case 0:break;
}
}
} while (a!=0);
printf ("----------------------------------------That's all--------------------------------------");
getch();
return 0;
}