-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab_14.cpp
247 lines (214 loc) · 13.9 KB
/
Lab_14.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
// Week17.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
struct address {
char house[30];
char city[20];
char county[30];
char state[5];
char zip[10];
};
struct data {
char firstName[30];
char secondName[50];
char companyName[70];
struct address address;
char phone1[15];
char phone2[15];
char email[70];
char web[100];
};
struct data people[501];
struct data SortData(char *dataLine);
void SearchByName(char *name, struct data *ptr);
void SearchByCompany(char *company, struct data *ptr);
void SearchByCity(char *city, struct data *ptr);
void SearchByCounty(char *county, struct data *ptr);
void SearchByState(char *state, struct data *ptr);
void SearchByZip(char *zip, struct data *ptr);
void StringToUpper(char *string);
int main()
{
char userOption;
char userInput[50];
bool isRunning = true;
FILE *fp;
fp = fopen("people.txt", "r+");
if (fp == NULL)
{
perror("File coudn't open");
}
else
{
for (int i = 0; i < 501; i++)
{
char temp[200];
fgets(temp, 200, fp);
if (i > 0)
{
people[i] = SortData(temp);
}
}
}
while (isRunning == true)
{
fflush(stdin);
printf("\n Choose Option:\n [1]Search by name\n[2]Search by company\n[3]Search by city\n[4]Search by county\n[5]Search by state\n[6]Search by zip\n[0]Exit\n");
scanf(" %c", &userOption);
switch (userOption)
{
case '1':
printf("Input all or part of the first name or surname: \n");
scanf(" %s", userInput);
SearchByName(userInput, people);
break;
case '2':
printf("Input all or part of the company name: \n");
fflush(stdin);
scanf(" %s", userInput);
SearchByCompany(userInput, people);
break;
case '3':
printf("Input all or part of the city name:\n");
fflush(stdin);
scanf(" %s", userInput);
SearchByCity(userInput, people);
break;
case '4':
printf("Input all or part of the county name: \n");
fflush(stdin);
scanf(" %s", userInput);
SearchByCounty(userInput, people);
break;
case '5':
printf("Input all or part of the state name: \n");
fflush(stdin);
scanf(" %s", userInput);
SearchByState(userInput, people);
break;
case '6':
printf("Input all or part of the zip code:\n");
fflush(stdin);
scanf(" %s", userInput);
SearchByZip(userInput, people);
}
}
return 0;
}
struct data SortData(char *dataLine)
{
struct data temp;
char *token;
token = strtok(dataLine, ",");
strcpy(temp.firstName, token);
StringToUpper(temp.firstName);
token = strtok(NULL, ",");
strcpy(temp.secondName, token);
StringToUpper(temp.secondName);
token = strtok(NULL, ",");
strcpy(temp.companyName, token);
token = strtok(NULL, ",");
strcpy(temp.address.house, token);
token = strtok(NULL, ",");
strcpy(temp.address.city, token);
token = strtok(NULL, ",");
strcpy(temp.address.county, token);
token = strtok(NULL, ",");
strcpy(temp.address.state, token);
token = strtok(NULL, ",");
strcpy(temp.address.zip, token);
token = strtok(NULL, ",");
strcpy(temp.phone1, token);
token = strtok(NULL, ",");
strcpy(temp.phone2, token);
token = strtok(NULL, ",");
strcpy(temp.email, token);
token = strtok(NULL, ",");
strcpy(temp.web, token);
return temp;
}
void SearchByName(char *name, struct data *ptr)
{
StringToUpper(name);
for (int i = 0; i < 501; i++)
{
if ((strstr(ptr[i].firstName,name)!= NULL)||(strstr(ptr[i].secondName,name)!=NULL))
{
printf("First Name: %s\nLastName:%s\nCompany Name: %s\nAddress: %s\nCity: %s\nCounty: %s\nState: %s\nZip: %s\nPhone #1:%s\nPhone #2: %s\n Email: %s\nWeb: %s\n",
ptr[i].firstName,ptr[i].secondName,ptr[i].companyName,ptr[i].address.house,ptr[i].address.city,ptr[i].address.county,ptr[i].address.state,ptr[i].address.zip,ptr[i].phone1,ptr[i].phone2,ptr[i].email,ptr[i].web);
}
}
}
void SearchByCompany(char *name, struct data *ptr)
{
StringToUpper(name);
for (int i = 0; i < 501; i++)
{
if (strstr(ptr[i].companyName, name) != NULL)
{
printf("First Name: %s\nLastName:%s\nCompany Name: %s\nAddress: %s\nCity: %s\nCounty: %s\nState: %s\nZip: %s\nPhone #1:%s\nPhone #2: %s\n Email: %s\nWeb: %s\n",
ptr[i].firstName, ptr[i].secondName, ptr[i].companyName, ptr[i].address.house, ptr[i].address.city, ptr[i].address.county, ptr[i].address.state, ptr[i].address.zip, ptr[i].phone1, ptr[i].phone2, ptr[i].email, ptr[i].web);
}
}
}
void SearchByCity(char *name, struct data *ptr)
{
StringToUpper(name);
for (int i = 0; i < 501; i++)
{
if (strstr(ptr[i].address.city, name) != NULL)
{
printf("First Name: %s\nLastName:%s\nCompany Name: %s\nAddress: %s\nCity: %s\nCounty: %s\nState: %s\nZip: %s\nPhone #1:%s\nPhone #2: %s\n Email: %s\nWeb: %s\n",
ptr[i].firstName, ptr[i].secondName, ptr[i].companyName, ptr[i].address.house, ptr[i].address.city, ptr[i].address.county, ptr[i].address.state, ptr[i].address.zip, ptr[i].phone1, ptr[i].phone2, ptr[i].email, ptr[i].web);
}
}
}
void SearchByZip(char *name, struct data *ptr)
{
StringToUpper(name);
for (int i = 0; i < 501; i++)
{
if (strstr(ptr[i].address.zip, name) != NULL)
{
printf("First Name: %s\nLastName:%s\nCompany Name: %s\nAddress: %s\nCity: %s\nCounty: %s\nState: %s\nZip: %s\nPhone #1:%s\nPhone #2: %s\n Email: %s\nWeb: %s\n",
ptr[i].firstName, ptr[i].secondName, ptr[i].companyName, ptr[i].address.house, ptr[i].address.city, ptr[i].address.county, ptr[i].address.state, ptr[i].address.zip, ptr[i].phone1, ptr[i].phone2, ptr[i].email, ptr[i].web);
}
}
}
void SearchByCounty(char * name, struct data *ptr)
{
StringToUpper(name);
for (int i = 0; i < 501; i++)
{
if (strstr(ptr[i].address.county, name) != NULL)
{
printf("First Name: %s\nLastName:%s\nCompany Name: %s\nAddress: %s\nCity: %s\nCounty: %s\nState: %s\nZip: %s\nPhone #1:%s\nPhone #2: %s\n Email: %s\nWeb: %s\n",
ptr[i].firstName, ptr[i].secondName, ptr[i].companyName, ptr[i].address.house, ptr[i].address.city, ptr[i].address.county, ptr[i].address.state, ptr[i].address.zip, ptr[i].phone1, ptr[i].phone2, ptr[i].email, ptr[i].web);
}
}
}
void SearchByState(char *name,struct data *ptr)
{
StringToUpper(name);
for (int i = 0; i < 501; i++)
{
if (strstr(ptr[i].address.state, name) != NULL)
{
printf("First Name: %s\nLastName:%s\nCompany Name: %s\nAddress: %s\nCity: %s\nCounty: %s\nState: %s\nZip: %s\nPhone #1:%s\nPhone #2: %s\n Email: %s\nWeb: %s\n",
ptr[i].firstName, ptr[i].secondName, ptr[i].companyName, ptr[i].address.house, ptr[i].address.city, ptr[i].address.county, ptr[i].address.state, ptr[i].address.zip, ptr[i].phone1, ptr[i].phone2, ptr[i].email, ptr[i].web);
}
}
}
void StringToUpper(char *string)
{
int i = 0;
while (*string) {
*string = toupper((unsigned char)*string);
string++;
}
}