forked from stephentambussi/SCU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab4.c
234 lines (234 loc) · 4.88 KB
/
lab4.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int counter=0;
union patientinfo//union declaration
{
float fever;
char paintype[20];
int daysick;
};
struct urgentcare//struct declaration
{
int department;
char name[20];
union patientinfo person;
};
struct urgentcare apptmnt[10];//array of structs-creates ten structs
int insert();//do not need pointers
int show();
int Remove();
int show_dept();
int check_duplicate();
int show_info();//new function that displays info for given name
int main(void)
{
int state=1;
while(state!=0 && counter<10)
{
printf("\nPress 1 to add name & department. Departments: 1,2,3,4 \n");
printf("Press 2 to show the current list of names.\n");
printf("Press 3 to remove the oldest item on list with the given department.\n");
printf("Press 4 to display the items on list with given department.\n");
printf("Press 5 to display the patient's medical info with the given name.\n");
printf("Press 0 to quit.\n");
scanf("%d",&state);
if(state==1)//insert
{
printf("Enter name:");
char temp[10];
scanf("%s",temp);
printf("Enter department:");
int temp2 = 0;
scanf("%d",&temp2);
if(temp2>0 && temp2<5)
{
insert(temp,temp2);
}
else
{
printf("Wrong department number. Try again.\n");
}
}
else if(state==2)//show list
{
show();
}
else if(state==3)//remove
{
Remove();
}
else if(state==4)//show list with department
{
show_dept();
}
else if(state==5)
{
printf("Enter name of patient:");
char temp[10];
scanf("%s",temp);
show_info(temp);
}
}
if(counter==10)
{
printf("List full.\n");
}
return 0;
}
int insert(char x[],int y)//inserts name and department to list
{
int check = check_duplicate(x);
if(check==0)//checks for existing name
{
printf("Enter info of illness.\n");
if(y==1)//adds info of illness
{
printf("D1=Fever temperature: ");
float temp;
scanf("%f",&temp);
apptmnt[counter].person.fever=temp;
}
else if(y==2)
{
printf("D2=Type of pain: ");
char type[10];
scanf("%s",type);
strcpy(apptmnt[counter].person.paintype,type);
}
else if(y==3||y==4)
{
printf("D3&D4=Days sick: ");
int temp;
scanf("%i",&temp);
apptmnt[counter].person.daysick=temp;
}
apptmnt[counter].department = y;
strcpy(apptmnt[counter].name,x);
counter++;
printf("Added entry!\n");
}
else//throws error if name exists
{
printf("Error. Name already exists.\n");
}
}
int show()//lists the names and departments
{
struct urgentcare *p;//the pointer for the struct
p = apptmnt;
for(int i=0;i<10;i++,p++)
{
printf("Name & Department: %s || %d\n",p->name,p->department);//this works-use as example
}
}
int Remove()//removes oldest item on list with department
{
struct urgentcare *p1,*p2;//pointers for the struct
p1 = apptmnt;
p2 = apptmnt;
printf("Remove entry on list with given department: ");
int removenum;
scanf("%d",&removenum);
if(removenum>0 && removenum<5)
{
int i;
for(i=0;i<counter;i++,p1++)
{
if(p1->department==removenum)
{
int tempcnt2 = i+1;
p2++;//increments pointer position in memory
while(tempcnt2<=counter)
{
strcpy(p1->name,p2->name);
strcpy(p2->name,"");
p1->department=p2->department;
p2->department=0;
p1++;//increments pointer position in memory
p2++;//increments pointer position in memory
tempcnt2++;
}
counter--;
break;
}
}
}
else
{
printf("Department does not exist. Try again.\n");
}
}
int show_dept()//shows all the items with given department
{
struct urgentcare *p;//the pointer for the struct
p = apptmnt;
printf("Enter department number you wish to see entries for: ");
int tempd;
scanf("%d",&tempd);
if(tempd>1 && tempd<5)
{
for(int i=0;i<counter;i++,p++)
{
if(p->department==tempd)
{
printf("Name & Department: %s || %d\n",p->name,p->department);
}
}
}
else
{
printf("Department does not exist. Try again.\n");
}
}
int check_duplicate(char b[])//checks for duplicate items
{
struct urgentcare *p;//the pointer for the struct
p = apptmnt;
int flag = 0;
for(int i=0;i<10;i++,p++)
{
if(strcmp(p->name,b)==0)
{
flag=1;
}
}
return(flag);
}
int show_info(char pname[])//displays patient medical info
{
struct urgentcare *p;
p=apptmnt;
int flag=0;
for(int i=0;i<10;i++,p++)
{
if(strcmp(p->name,pname)==0)
{
flag=0;
if(p->department==1)
{
printf("Patient: %s |Department: %i |Fever Temperature: %f degrees.\n",p->name,p->department,p->person.fever);
break;
}
else if(p->department==2)
{
printf("Patient: %s |Department: %i |Pain Type: %s.\n",p->name,p->department,p->person.paintype);
break;
}
else if(p->department==3||p->department==4)
{
printf("Patient: %s |Department: %i |Days Sick: %i days.\n",p->name,p->department,p->person.daysick);
break;
}
}
else
{
flag=1;
}
}
if(flag==1)
{
printf("Name not found.\n");
}
return 0;
}