-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dentist-HashList.c
275 lines (237 loc) · 7 KB
/
Dentist-HashList.c
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
/*************************************************************************
Copyright © 2021 Konstantinidis Konstantinos
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define HMax 11
#define VMax 100
#define EndOfList -1
typedef enum{
Whitening=1, Cleaning=2, Extraction=3
}service_type;
typedef struct{
double paid;
service_type service;
} visit_type;
typedef visit_type ListElementType;
typedef struct{
int RecKey;
ListElementType Data;
int Link;
} ListElm;
typedef struct{
int HashTable[HMax];
int Size;
int SubListPtr;
int StackPtr;
ListElm List[VMax];
} HashListType;
typedef enum{
FALSE, TRUE
} boolean;
void CreateHashList(HashListType *HList);
int HashKey(int Key);
boolean FullHashList(HashListType HList);
void SearchSynonymList(HashListType HList,int KeyArg,int *Loc,int *Pred);
void SearchHashList(HashListType HList,int KeyArg,int *Loc,int *Pred);
void AddRec(HashListType *HList,ListElm InRec);
void menu(int *choice);
int GetCharacterSum(char *Name);
void NewVisit(HashListType *DB);
void SearchVisit(HashListType DB);
int main(){
int choice;
HashListType DB; //An imitation of a database
CreateHashList(&DB);
do{
menu(&choice);
if(choice == 1)
NewVisit(&DB); //Make a new appointment
else if(choice == 2)
SearchVisit(DB); //Search for a client
}while(choice != 3);
printf("Exiting the program.. \n"); //If choice is 3 then terminate the program
return 0;
}
/*This is the menu that will appear in the user screen
The user will use one of the provided choices*/
void menu(int *choice){
printf("\t\t\tMENU\n");
printf("-------------------------------------------------\n");
printf("1. INSERT APPOINTMENT\n");
printf("2. PRINT CLIENT'S APPOINTMENTS\n");
printf("3. EXIT\n");
do{
printf("\nEnter the number of your choice: ");
scanf("%d", choice);
if(*choice<1 || *choice>3){ //Get a choice between 1-3
printf("This is not a given choice...\n\n");
}
}while(*choice<1 && *choice>3);
}
//A method to get the HashKey
int HashKey(int Key){
return Key%HMax;
}
//A method to create a Hash List
void CreateHashList(HashListType *HList){
int index;
HList->Size = 0;
HList->StackPtr = 0;
index = 0;
while(index<HMax){
HList->HashTable[index] = EndOfList;
index = index+1;
}
index = 0;
while(index<VMax-1){
HList->List[index].Link = index+1;
HList->List[index].Data.paid = 0;
HList->List[index].Data.service = 0;
index += 1;
}
HList->List[index].Link = EndOfList;
}
//A method to see if the list is full
boolean FullHashList(HashListType HList){
return(HList.Size==VMax);
}
//A method to search for synonyms in the hash list
void SearchSynonymList(HashListType HList,int KeyArg,int *Loc,int *Pred){
int Next;
Next = HList.SubListPtr;
*Loc = -1;
*Pred = -1;
while(Next != EndOfList){
if(HList.List[Next].RecKey == KeyArg){
*Loc = Next;
Next = EndOfList;
}
else{
*Pred = Next;
Next = HList.List[Next].Link;
}
}
}
//A method to search inside the hash list
void SearchHashList(HashListType HList,int KeyArg,int *Loc,int *Pred){
int HVal;
HVal = HashKey(KeyArg);
if(HList.HashTable[HVal] == EndOfList){
*Pred = -1;
*Loc = -1;
}
else{
HList.SubListPtr = HList.HashTable[HVal];
SearchSynonymList(HList,KeyArg,Loc,Pred);
}
}
//A method to add a new record in the list
void AddRec(HashListType *HList,ListElm InRec){
int Loc, Pred, New, HVal;
if(!(FullHashList(*HList))){
Loc = -1;
Pred = -1;
SearchHashList(*HList,InRec.RecKey,&Loc,&Pred);
if(Loc == -1){
HList->Size = HList->Size + 1;
New = HList->StackPtr;
HList->StackPtr = HList->List[New].Link;
HList->List[New] = InRec;
if(Pred==-1){
HVal = HashKey(InRec.RecKey);
HList->HashTable[HVal] = New;
HList->List[New].Link = EndOfList;
}
else{
HList->List[New].Link = HList->List[Pred].Link;
HList->List[Pred].Link = New;
}
}
else
printf("A client with the same name is already in the catalogue\n");
}
else
printf("The list is full can't add more...");
}
/*A method to get the summary of the characters from the client's name
because we want to "save" the name as an integer and not a string*/
int GetCharacterSum(char *Name){
int index;
int key;
char a = 'A' - 1;
index = 0;
key = 0;
while(TRUE){
if(index > strlen(Name))
break;
else
key += (index + 1) * (Name[index] - a);
index++;
}
return key;
}
//A method to create a new appointment
void NewVisit(HashListType *DB){
char ch;
ListElm AnItem;
ListElementType visit;
char client[20];
do{
printf("Enter the client's Name: ");
scanf("%s", client);
getchar();
printf("Enter the service number:\n1) Whitening\n2) Cleaning\n3) Extraction\n");
scanf("%d",&visit.service);
while(visit.service<1 || visit.service>3){
printf("Incorrect service.\n");
printf("Please enter a correct service number:\n1) Whitening\n2) Cleaning\n3) Extraction\n");
scanf("%d",&visit.service);
}
getchar();
printf("Enter the amount paid: ");
scanf(" %lf",&visit.paid);
getchar();
printf("\n");
AnItem.Data = visit;
AnItem.RecKey = GetCharacterSum(client);
AddRec(DB, AnItem);
printf("Continue Y/N: ");
scanf(" %c", &ch);
while(ch!= 'Y' && ch!= 'N'){
printf("Wrong input.\n");
printf("Continue Y/N: ");
scanf(" %c", &ch);
}
}while(ch != 'N');
}
//A method to search for a client
void SearchVisit(HashListType DB){
int Akey;
int Loc, Pred;
char client[20];
int service;
double paid;
printf("Enter the client's Name: ");
scanf(" %s", client);
getchar();
Akey = GetCharacterSum(client);
SearchHashList(DB, Akey, &Loc, &Pred);
if(Loc!=-1){
printf("Service: %d\n", DB.List[Loc].Data.service);
printf("Amount Paid: %.2f\n", DB.List[Loc].Data.paid);
}
else
printf("This customer does not exist..\n\n\n");
}