-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple-Log-In-System.c
More file actions
220 lines (194 loc) · 5.7 KB
/
Simple-Log-In-System.c
File metadata and controls
220 lines (194 loc) · 5.7 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
/*************************************************************************
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 <ctype.h>
#define HMax 10
#define VMax 100
#define EndOfList -1
typedef struct{
char RecKey[8];
char password[6];
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(char Key[]);
boolean FullHashList(HashListType HList);
void SearchSynonymList(HashListType HList,char KeyArg[],int *Loc,int *Pred);
void SearchHashList(HashListType HList,char KeyArg[],int *Loc,int *Pred);
void AddRec(HashListType *HList,ListElm InRec);
int findAverage(char s[]);
void BuildHashList(HashListType *Hlist);
main(){
char choice;
HashListType Hlist;
ListElm Item;
int Loc, Pred;
BuildHashList(&Hlist);
do{
printf("New entry Y/N (Y=Yes, N=No)\n");
scanf(" %c", &choice);
}
while(choice!='N' && choice!='Y');
if(choice == 'Y'){
do{
printf("USERNAME: ");
scanf("%s", Item.RecKey);
getchar();
printf("\n");
printf("PASSWORD: ");
scanf("%s", Item.password);
getchar();
printf("\n");
SearchHashList(Hlist, Item.RecKey, &Loc, &Pred);
if(Loc != -1){
if(strcmp(Hlist.List[Loc].password, Item.password)==0)
printf("You have logged in to the system\n");
else
printf("Access is forbidden: Wrong password\n");
}
else
printf("Access is forbidden: Wrong user ID\n");
do{
printf("New entry Y/N (Y=Yes, N=No)\n");
scanf(" %c", &choice);
}
while(choice!='N' && choice!='Y');
}while(choice != 'N');
}
system("PAUSE");
}
//A method to get the HashKey
int HashKey(char Key[]){
int average;
average = findAverage(Key);
return average % 10; //The list will have 10 positions
}
int findAverage(char s[]){
int length;
length = strlen(s);
return ((s[0] + s[length - 1])/2); //return = (ASCII code of 1st char + ASCII code of last char) / 2
}
//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;
strcpy(HList->List[index].password, "0");
strcpy(HList->List[index].RecKey, "0");
index=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, char KeyArg[8], int *Loc, int *Pred){
int Next;
Next=HList.SubListPtr;
*Loc=-1;
*Pred=-1;
while(Next!=EndOfList){
if(strcmp(HList.List[Next].RecKey,KeyArg) == 0){
*Loc=Next;
Next=EndOfList;
}
else{
*Pred=Next;
Next=HList.List[Next].Link;
}
}
}
//A method to search inside the hash list
void SearchHashList(HashListType HList,char 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("There is already someone with the same key \n");
}
else
printf("Full list...");
}
//A method to create the hash list, read the file and insert the records in the list
void BuildHashList(HashListType *Hlist){
ListElm Item;
FILE *fp;
int nscan;
CreateHashList(&(*Hlist)); //Create the Hash list
fp = fopen("I5f6.txt", "r"); //Read the file
if(fp == NULL)
printf("Problem opening file");
else{
while(TRUE){
nscan = fscanf(fp, "%s %s", Item.RecKey, Item.password);
if (nscan == EOF)
break;
if (nscan != 2){
printf("Improper file format\n");
break;
}
else
AddRec(Hlist, Item); //Add each recording in the list
}
}
}