-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.c
More file actions
394 lines (337 loc) · 9.27 KB
/
Copy pathcore.c
File metadata and controls
394 lines (337 loc) · 9.27 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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
*****************************************************************************
Assignment 1 - Milestone 3
Full Name : Hyunjin Shin
Student ID#: 168043214
Email : hshin41@myseneca.ca
Section : NAA
Authenticity Declaration:
I declare this submission is the result of my own work and has not been
shared with any other student or 3rd party content provider. This submitted
piece of work is entirely of my own creation.
*****************************************************************************
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "core.h"
//////////////////////////////////////
// USER INTERFACE FUNCTIONS
//////////////////////////////////////
// Clear the standard input buffer
void clearInputBuffer(void)
{
// Discard all remaining char's from the standard input buffer:
while (getchar() != '\n')
{
; // do nothing!
}
}
// Wait for user to input the "enter" key to continue
void suspend(void)
{
printf("<ENTER> to continue...");
clearInputBuffer();
putchar('\n');
}
//////////////////////////////////////
// USER INPUT FUNCTIONS
//////////////////////////////////////
//
// Copy your work done from Milestone #2 (core.c) into this file
// - Organize your functions in the same order as they are listed in the core.h file
//
// 1. inputInt
int inputInt(void)
{
int nread = -1;
int result = 0;
int done = 1;
do {
nread = scanf("%d", &result);
if (getchar() != '\n')
{
printf("Error! Input a whole number: "); // when user inputs a float number
clearInputBuffer();
}
else if (nread != 1)
{
printf("Error! Input a whole number: ");
clearInputBuffer();
}
else
{
done = 0;
}
} while (done);
return result;
}
// 2. inputIntPositive
int inputIntPositive(void)
{
int result = 0;
int done = 1;
do {
result = inputInt();
if (result < 0)
{
printf("ERROR! Value must be > 0: ");
}
else
{
done = 0;
}
} while (done);
return result;
}
// 3. inputIntRange
int inputIntRange(int min, int max)
{
int result = 0;
int done = 1;
do {
result = inputInt();
if (result < min || result > max)
{
printf("ERROR! Value must be between %d and %d inclusive: ", min, max);
}
else
{
done = 0;
}
} while (done);
return result;
}
// 4. inputCharOption
char inputCharOption(char* string)
{
char result;
int found = 1;
int i = 0;
int posn = 0; // position of null terminator
// finding the position of a null terminator
while (string[i] != '\0')
{
i++;
posn = i;
}
// getting input from user and check if it matches the give string
do {
scanf("%c", &result);
clearInputBuffer();
for (i = 0; i < posn + 1; i++)
{
if (result == string[i])
{
posn = i;
found = 0; // found the character maching that of the string
}
}
if (found != 0)
{
printf("ERROR: Character must be one of [%s]:", string);
}
} while (found);
// copy the matched character into a variable to return it
result = string[posn];
return result;
}
// 5. inputCString
void inputCString(char* string, int min, int max)
{
int done = 1;
char ch = 'X';
int i = 0;
int posn = 0; //position of '\0'
char temp[MAX_STRING] = "\0";
while (done)
{
// getting input string and store it into temp string
i = 0;
do {
ch = getchar();
temp[i] = ch;
posn = i;// to find the position of null terminator;
i++;
} while (ch != '\n');
temp[posn] = '\0'; // to insert null terminator at the end of the string.
//test to see if it is properly stored
/*printf("i = %d, posn = %d", i, posn);
for (i = 0; i <= posn; i++)
{
printf("%c", temp[i]);
}*/
// filtering invaild input when min == max
if (min == max)
{
if (posn > max || posn < min)
{
printf("ERROR: String length must be exactly %d chars: ", max);
}
else
{
for (i = 0; i < posn + 1; i++)
{
string[i] = temp[i];
}
done = 0;
}
}
// filtering invaild input when min != max
else
{
if (posn > max)
{
printf("ERROR: String length must be no more than %d chars: ", max);
}
else if (posn < min)
{
printf("ERROR: String length must be between %d and %d: ", min, max);
}
else
{
for (i = 0; i < posn + 1; i++)
{
string[i] = temp[i];
}
done = 0;
}
}
}
return;
}
// 6. displayFormattedPhone
void displayFormattedPhone(const char phoneNumber[])
{
int i = 0;
int bad = 0;
int posn = 0;
// figuring out the size of phone number
while (phoneNumber != NULL && phoneNumber[i] != '\0')
{
i++;
posn = i; // index of null terminator
}
// check if it has the valid size and is not NULL pointer
if (posn != 10 || phoneNumber == NULL)
{
printf("(___)___-____");
}
else
{
for (i = 0; i < 10; i++)
{
if (phoneNumber[i] > 57 || phoneNumber[i] < 48) // checking if they are 0~9 with ASKII CODE
{
printf("(___)___-____");
i = 100; // escape the for loop
bad = 1;
}
}
if (bad == 0)
{
printf("(%c%c%c)%c%c%c-%c%c%c%c", phoneNumber[0], phoneNumber[1], phoneNumber[2], phoneNumber[3],
phoneNumber[4], phoneNumber[5], phoneNumber[6], phoneNumber[7], phoneNumber[8], phoneNumber[9]);
}
}
}
//////////////////////////////////////
// UTILITY FUNCTIONS
//////////////////////////////////////
// 7. inputOneChar -- getting only one char.
char inputOneChar(char* string)
{
char result;
int ch;
int found = 1;
int i = 0;
int posn = 0; // position of null terminator
// finding the position of a null terminator
while (string[i] != '\0')
{
i++;
posn = i;
}
// getting input from user and check if it matches the give string
do {
scanf("%c", &result);
while((ch = getchar()) != '\n') // to check if the input is more than one character
{
printf("ERROR: Character must be one of [%s]: ", string);
clearInputBuffer();
scanf("%c", &result);
}
for (i = 0; i < posn + 1; i++)
{
if (result == string[i])
{
posn = i;
found = 0; // found the character maching that of the string
}
}
if (found != 0)
{
printf("ERROR: Character must be one of [%s]:", string);
}
} while (found);
// copy the matched character into a variable to return it
result = string[posn];
return result;
}
// 8. inputPhoneString
void inputPhoneString(char* string, int min, int max)
{
int done = 1;
char ch = 'X';
int i = 0;
int posn = 0; //position of '\0'
char temp[MAX_STRING] = "\0";
while (done)
{
// getting input string and store it into temp string
i = 0;
do {
ch = getchar();
temp[i] = ch;
posn = i;// to find the position of null terminator;
i++;
} while (ch != '\n');
temp[posn] = '\0'; // to insert null terminator at the end of the string.
// filtering invaild input when min == max
if (min == max)
{
if (posn > max || posn < min)
{
printf("Invalid %d-digit number! Number: ", max);
}
else
{
for (i = 0; i < posn + 1; i++)
{
string[i] = temp[i];
}
done = 0;
}
}
// filtering invaild input when min != max
else
{
if (posn > max)
{
printf("Invalid %d-digit number! Number: ", max);
}
else if (posn < min)
{
printf("Invalid %d-digit number! Number: ", max);
}
else
{
for (i = 0; i < posn + 1; i++)
{
string[i] = temp[i];
}
done = 0;
}
}
}
return;
}