/
options.c
497 lines (466 loc) · 15.3 KB
/
options.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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/******************************************************************************
* COSC1283/1284 - Programming Techniques
* Semester 1 2012 Assignment #1
* Full Name : Lauren Grimes
* Student Number : s3228871
* Yallara Username : s3228871
* Course Code : COSC1284
* Program Code : BP162
******************************************************************************/
#include "assignment1.h"
#include <string.h>
#include <ctype.h>
/****************************************************************************
* This source file contains important functions to be developed and
* used by various menu options, as indicated. Note that every
* function has as its first parameter the optionsStats array, which
* will be appropriately updated for later reporting of menu option 7.
* You may ignore this parameter and its relevance to each function
* until you develop the sessionSummary() function.
****************************************************************************/
/****************************************************************************
* This function prompts the user for a positive integer. It reports
* whether or not this number is a perfect square. Additionally, it
* prints the perfect squares immediately before and after this
* number. See requirement #2 in the assignment specification
****************************************************************************/
void perfectSquares(int *optionStats)
{
int userNum;
char *p;
char buffer[7 + EXTRA_SPACES];
int divNum;
int closestSquare;
char* prompt = "Please enter a positive integer ( 1 - 1000000): ";
printf("Perfect Squares \n ----------------- \n\n");
getString(buffer, MAX_INT_LENGTH, prompt);
if ( buffer[0] == '\0' )
{
printf("Exiting to Main Menu... \n \n");
return;
}
userNum = strtol(buffer, &p, 10);
if ((buffer[0] != '\n') && (*p == '\n' || *p == '\0'))
{
/* VALIDATION: Range validation*/
if ( (userNum < 1000000) && (userNum > 0) )
{
for (divNum=1; divNum <= userNum; divNum++)
{
if (divNum == (userNum / divNum))
{
if ((divNum*divNum) == userNum)
{
printf("'%d' is a perfect square: %d * %d \n \n", userNum, divNum, divNum);
return;
}
}
else if ((userNum-1) == divNum)
{
printf("'%d' is not a perfect Square.\n", userNum);
/*retreieve closest square roots */
/* issues occuring when using 27 as a square root. FIX!!!!!!! */
divNum = 1;
closestSquare = divNum * divNum;
while (closestSquare <( (userNum - divNum)-1))
{
if ((divNum * divNum) > userNum)
break;
divNum++;
closestSquare = divNum * divNum;
}
printf("'%d' is the perfect square before. %d * %d \n", closestSquare, divNum, divNum);
divNum++;
printf("'%d' is the perfect square root after. %d * %d \n \n",divNum * divNum, divNum, divNum);
(optionStats[0])++;
return;
}
}
}
else
{
printf("Invalid range \n");
}
}
else
{
printf("Invalid Number \n");
return;
/*need to change to reprompt for input*/
}
}
/****************************************************************************
ASCII TO BINARY TRANSLATOR
****************************************************************************/
void asciiToBinary(int *optionStats)
{
/*TRANSLATE THE ASCII CHARACTERS TO DECIMAL NOTATION THEN TO BINARY AND DISPLAY EACH ONE INDIVIDUALLY SEPERATED BY A SPACE.
READ IN EACH CHARACTER FORM THE STRING AND THEN DO THE TRANSLATION THEN SAVE TO NEW BUFFER AND PRINT OUT.
*/
char buffer[5 + EXTRA_SPACES];
char *prompt = "Please Enter upto 5 ASCII characters: ";
int i;
printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \n ASCII TO BINARY CONVERSION\n\n");
getString( buffer, 5 + EXTRA_SPACES, prompt);
if (buffer[0] == '\0')
{
printf("Exiting to Main Menu....");
return;
}
for (i = 0; buffer[i] != '\0'; i++)
{
int ascii = buffer[i];
char binaryNotation [8];
int j;
do
{
/* j = number of bits for binary number*/
/* % 2 to find if bit is 1 or 0
Divide ascii num by 2*/
for (j = 0; j < 8; j++)
{
binaryNotation[j] = ascii % 2;
ascii = ascii/2;
}
} while ((ascii %2) != 0);
/*print back from array */
for (j = 7; j>=0; j--)
{
printf("%d", binaryNotation[j]);
}
printf(" ");
}
printf("\n\n");
(optionStats[1])++;
return;
}
/****************************************************************************
* See requirement #4 "Matching Brackets"
****************************************************************************/
void matchingBrackets(int *optionStats)
{
char *prompt = "Please enter a string (1 - 20 Characters): ";
char buffer [20 + EXTRA_SPACES];
int i=0;
char brackets[20 + EXTRA_SPACES];
int j = 0;
int MATCHING = TRUE;
printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\nMATCHING BRACKETS\n\n");
getString(buffer, 20 + EXTRA_SPACES, prompt);
if (buffer[0] == '\0')
{
printf("Exiting to Main Menu....\n");
return;
}
do
{
if (((buffer[i] == '(') || (buffer[i] == '[') || (buffer[i] == '{')) || ((buffer[i] == ')') || (buffer[i] == ']') || (buffer[i] == '}')))
{
brackets[j] = buffer[i];
j++;
}
i++;
} while (buffer[i] != '\0');
/*reinitalise counters*/
i=0;
j=j-1;
/*For Nested brackets only */
for(i=0; (MATCHING== TRUE) && (i< j); i++, j--)
{
if ((brackets[i] == '(') && (brackets[j] != ')'))
{
printf("Brackets do not match\n");
MATCHING = FALSE;
(optionStats[2])++;
return;
}
else if ((brackets[i] == '{') && (brackets[j] != '}'))
{
printf("Brackets do not match\n");
MATCHING = FALSE;
(optionStats[2])++;
return;
}
else if ((brackets[i] == '[') && (brackets[j] != ']'))
{
printf("Brackets do not match\n");
MATCHING = FALSE;
(optionStats[2])++;
return;
}
else
{
i++;
j--;
}
}
if (MATCHING == TRUE)
{
printf("Brackets Match \n");
(optionStats[2])++;
}
}
/****************************************************************************
* See requirement #5 "Tic Tac Toe Game"
****************************************************************************/
void drawBoard(char boardTurns[][NUM_COLS])
{
printf("+---+---+---+\n");
printf("| %c | %c | %c |\n", boardTurns[0][0], boardTurns[0][1], boardTurns[0][2]);
printf("+---+---+---+\n");
printf("| %c | %c | %c |\n", boardTurns[1][0], boardTurns[1][1], boardTurns[1][2]);
printf("+---+---+---+\n");
printf("| %c | %c | %c |\n", boardTurns[2][0], boardTurns[2][1], boardTurns[2][2]);
printf("+---+---+---+\n");
}
/* char validateSelection
******************************
* Will check if the selection inputted by the user is a numeri number or if the turn has already been taken
******************************/
char checkWin(char matrixGrid[][NUM_COLS])
{
int i=0;
/*check the rows*/
for ( i=0; i< NUM_ROWS; i++)
{
if ( (matrixGrid[i][0]== matrixGrid[i][1]) && (matrixGrid[i][1]==matrixGrid[i][2]) && (matrixGrid[i][2]==matrixGrid[i][0]))
{
return matrixGrid[i][0];
}
}
/*Check the columns*/
for ( i=0; i< NUM_COLS; i++)
{
if ( (matrixGrid[0][i]==matrixGrid[1][i]) && (matrixGrid[1][i]==matrixGrid[2][i]) && (matrixGrid[2][i]==matrixGrid[0][i]))
{
return matrixGrid[0][i];
}
}
/*check diagonals */
if ((matrixGrid[0][0]==matrixGrid[1][1]) && (matrixGrid[1][1]==matrixGrid[2][2]) && (matrixGrid[2][2]==matrixGrid[0][0]))
{
return matrixGrid[0][0];
}
if ((matrixGrid[0][2]==matrixGrid[1][1]) && (matrixGrid[1][1]==matrixGrid[2][0]) && (matrixGrid[2][0]==matrixGrid[0][2]))
{
return matrixGrid[0][2];
}
return NULL;
}
void ticTacToeGame( int * optionStats )
{
char matrix[NUM_ROWS][NUM_COLS]= {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
char buffer[1 + EXTRA_SPACES];
int playerNum = 1;
int turnCount=0;
int selection=0;
/*drawGrid(*boardTurns)*/
for (turnCount=0; turnCount <= MAX_TURNS; turnCount++)
{
if ( playerNum == 1 )
{
int x =0;
int y =0;
char check=NULL;
char* prompt = "Player #1 select a square: ";
char *p;
/*implement for player two aswell */
drawBoard(matrix);
getString(buffer, 1 + EXTRA_SPACES, prompt);
selection= strtol(buffer, &p, 10);
/*validateNumericInput(selection, buffer, 1 + EXTRA_SPACES, prompt);*/
/*check if input is non-numeric*/
if ((selection < 0) || (selection > 9))
{
printf("invalid selection. please try again");
}
if (buffer[0] == '\0')
{
printf("Exiting to Main Menu.... \n \n");
return;
}
if ((buffer[0] != '\n') && (*p == '\n' || *p == '\0'))
{
if (selection <= 9 && selection > 0 )
{
for (x=0; x < NUM_ROWS; x++)
{
for (y=0; y < NUM_COLS; y++)
{
/*better way of finding char value of user input - although vlidation already performed so input should be ok to straight convert*/
if ( matrix[x][y] == (char)buffer[0] )
{
matrix[x][y]='X';
}
}
}
}
else
/*need to loop and reprompt for inpt */
printf("Invalid option.");
}
/*assign selection to board[][]*/
check=checkWin(matrix);
if (check != NULL)
{
printf("\n Player %c WON!\n", check);
drawBoard(matrix);
printf("Returning to menu");
(optionStats[3])++;
return;
}
playerNum=2;
}
if ( playerNum == 2 )
{
int x=0;
int y=0;
char check=NULL;
char *prompt = "Player #2 select a square: ";
char *p;
drawBoard(matrix);
getString(buffer, 1 + EXTRA_SPACES, prompt);
selection=strtol(buffer, &p, 10);
if ((selection < 0) || (selection >9))
{
printf("invalid selection. please try again \n");
}
if (buffer[0] == '\0')
{
printf("Exiting to Main Menu.... \n \n");
return;
}
else if ((buffer[0] != '\n') && ( *p == '\n' || *p == '\0'))
{
/*check for non numeric characters - also semi done in selection check */
if (selection <= 9 && selection > 0)
{
for (x=0; x < NUM_ROWS; x++)
{
for (y=0; y < NUM_COLS; y++)
{
if ( matrix[x][y] == (char)buffer[0])
{
matrix[x][y]='O';
}
}
}
}
else
printf("invalid Range");
}
check=checkWin(matrix);
if (check != NULL)
{
printf("\nPlayer %c WON!\n", check);
drawBoard(matrix);
printf("Returning to Menu\n");
(optionStats[3])++;
return;
}
playerNum=1;
}
}
}
/****************************************************************************
* This function prompts the user for a integer N and several lines of
* text (sentences between 150-200 characters. The text will be printed
* N characters per line, by adding extra spaces as evenly as possible
* so there are no broken words. See requirement #6 in the assignment
* specification.
****************************************************************************/
void formattingText(int *optionStats)
{
char *prompt1= "Enter a max number of characters per line: ";
char *prompt2= "Enter a sentence (between 150-200) characters: ";
char buffer [200 + EXTRA_SPACES];
char linefeeder [200 + EXTRA_SPACES];
char number [3+ EXTRA_SPACES];
int MAX_CHARS_LINE;
char *p;
int i;
char SPACE = ' ';
int pointer;
int spacecounter;
int insertcounter;
getString(number, 3 + EXTRA_SPACES, prompt1);
MAX_CHARS_LINE = strtol(number,&p,10);
if(number[0] == '\0')
{
printf("Exiting to Main Menu.... \n");
return;
}
getString(buffer, 200 + EXTRA_SPACES, prompt2);
if(buffer[0] == '\0')
{
printf("Exiting to Main Menu.... \n");
return;
}
printf("%d - max number \n", MAX_CHARS_LINE);
printf("%s \n", buffer);
/*initialise the pointer for the string*/
pointer = (MAX_CHARS_LINE-1);
/*all in a while loop */
while (buffer[pointer] != '\0')
{
/*is char is a space*/
/*clear out linefeeder before using again*/
if (buffer[pointer] == SPACE)
{
printf("space detected");
spacecounter=0;
/*copy upto MAX_CHARS into linefeeder and print out*/
for(i=0;i <= (MAX_CHARS_LINE-1); i++)
{
if (buffer[i] == SPACE)
{
spacecounter++;
}
linefeeder[i] = buffer[i];
}
linefeeder[MAX_CHARS_LINE] = '\n';
pointer=pointer+ MAX_CHARS_LINE;
printf("%s ...spaces: %d\n",linefeeder, spacecounter);
}
/*if char is not a space*/
if (buffer[pointer] != SPACE)
{
printf("Character detected");
/*initiliase the counters of characters iterated and spaces already in array*/
spacecounter=0;
insertcounter=0;
while (buffer[pointer] != SPACE)
{
pointer--;
insertcounter++;
}
for (i=pointer; i <=( (MAX_CHARS_LINE-1) + pointer); i++)
{
if (buffer[i] == SPACE)
{
spacecounter++;
}
}
printf(" %d spaces should be inserted. Currently %d spaces.", insertcounter, spacecounter);
if (buffer[pointer] == SPACE)
{
/*write to line feeder adding in extra spaces evenly*/
}
}
}
}
/*************************************************************************
* This function will report a Session Summary of the running of your
* program. This summary needs to output the number of times each option
* has been processed (not including the Exit option). See requirement #7
* in the assignment specification.
*************************************************************************/
void sessionSummary(int *optionStats)
{
printf("--------------------------------\n\nSESSION SUMMARY\n\n");
printf("Perfect Sqaures: %d \n", optionStats[0]);
printf("ASCII to Binary: %d \n", optionStats[1]);
printf("Matching Brackets: %d \n", optionStats[2]);
printf("Tic Tac Toe Game: %d \n", optionStats[3]);
printf("Formatting Text: %d \n", optionStats[4]);
}