Created
June 16, 2020 02:47
-
-
Save Taresin/67b9e139dc5913e8b720735bf99634d2 to your computer and use it in GitHub Desktop.
COSC2138 - Week 3 Lab exercises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Question 1 | |
----------- | |
Test Case 1 | |
Input: Freddy | |
Non-numerical input occurred | |
Answer: -1 | |
Test Case 2 | |
Input: 40Freddy | |
Non-numerical input occurred | |
Answer: -1 | |
Test Case 3 | |
Input: 0 | |
Answer: 0 | |
Test Case 4 | |
Input: 999999999999999999 | |
Input greater than integer occurred | |
Answer: -1 | |
Test Case 5 | |
Input: 97 | |
Answer: 97 | |
Test Case 6 | |
Input: 12345678 | |
Answer: 12345678 | |
Test Case 7 | |
Input: 1234567890 | |
Answer: 1234567890 | |
Test Case 8 | |
Input: 2147483647 | |
Answer: 2147483647 | |
Question 2 | |
----------- | |
Test Case 1 | |
Input: Freddy | |
Not valid CSV data | |
Answer: -1 | |
Test Case 2 | |
Input: 45;98;182; | |
Not valid CSV data | |
Answer: -1 | |
Test Case 3 | |
Input: 45,98,182 | |
Answer: 325 | |
Test Case 4 | |
Input: 45,989999999999,182 | |
Input greater than integer occurred | |
Number too large for Integer | |
Answer: -1 | |
Test Case 5 | |
Input: 2147483647,0 | |
Answer: 2147483647 | |
Test Case 6 | |
Input: 2147483647,1 | |
Total sum too large for Integer | |
Answer: -1 | |
Test Case 7 | |
Input: 2147483646,1,0 | |
Answer: 2147483647 | |
Test Case 8 | |
Input: 2147483646,1,1 | |
Total sum too large for Integer | |
Answer: -1 | |
Question 3 | |
----------- | |
Test Case 1 | |
Input: Freddy | |
Freddy | |
Not valid CSV data | |
Answer: -1 | |
Test Case 2 | |
Input: 45;98;182; | |
45;98;182; | |
Not valid CSV data | |
Answer: -1 | |
Test Case 3 | |
Input: 45,98,182 | |
45,98,182 | |
Answer: 325 | |
Test Case 4 | |
Input: 45,989999999999,182 | |
45,989999999999,182 | |
Input greater than integer occurred | |
Number too large for Integer | |
Answer: -1 | |
Test Case 5 | |
Input: 2147483647,0 | |
2147483647,0 | |
Answer: 2147483647 | |
Test Case 6 | |
Input: 2147483647,1 | |
2147483647,1 | |
Total sum too large for Integer | |
Answer: -1 | |
Test Case 7 | |
Input: 2147483646,1,0 | |
2147483646,1,0 | |
Answer: 2147483647 | |
Test Case 8 | |
Input: 2147483646,1,1 | |
2147483646,1,1 | |
Total sum too large for Integer | |
Answer: -1 | |
Question 4 | |
----------- | |
Test Case 1 | |
Input: abcde | |
Answer: | |
Upper: 0 | |
Lower: 5 | |
Numbers: 0 | |
Spaces: 0 | |
Test Case 2 | |
Input: ABCDE | |
Answer: | |
Upper: 5 | |
Lower: 0 | |
Numbers: 0 | |
Spaces: 0 | |
Test Case 3 | |
Input: 12345 | |
Answer: | |
Upper: 0 | |
Lower: 0 | |
Numbers: 5 | |
Spaces: 0 | |
Test Case 4 | |
Input: | |
Answer: | |
Upper: 0 | |
Lower: 0 | |
Numbers: 0 | |
Spaces: 5 | |
Test Case 5 | |
Input: aB3 | |
Answer: | |
Upper: 1 | |
Lower: 1 | |
Numbers: 1 | |
Spaces: 1 | |
Process finished with exit code 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "q1.h" | |
#include "stdio.h" | |
#include "stdlib.h" | |
#include "string.h" | |
#include "limits.h" | |
#include "ctype.h" | |
int main() { | |
// Question 1 Tests | |
printf("Question 1\n-----------\n"); | |
test_q1("Freddy"); | |
test_q1("40Freddy"); | |
test_q1("0"); | |
test_q1("999999999999999999"); | |
test_q1("97"); | |
test_q1("12345678"); | |
test_q1("1234567890"); | |
char max[11]; | |
sprintf(max, "%d", INT_MAX); | |
test_q1(max); | |
// Question 2 Tests | |
printf("\n"); | |
printf("Question 2\n-----------\n"); | |
test_q2("Freddy"); | |
test_q2("45;98;182;"); | |
test_q2("45,98,182"); | |
test_q2("45,989999999999,182"); | |
test_q2("2147483647,0"); | |
test_q2("2147483647,1"); | |
test_q2("2147483646,1,0"); | |
test_q2("2147483646,1,1"); | |
// Question 3 Tests | |
printf("\n"); | |
printf("Question 3\n-----------\n"); | |
test_q3("Freddy"); | |
test_q3("45;98;182;"); | |
test_q3("45,98,182"); | |
test_q3("45,989999999999,182"); | |
test_q3("2147483647,0"); | |
test_q3("2147483647,1"); | |
test_q3("2147483646,1,0"); | |
test_q3("2147483646,1,1"); | |
// Question 4 Tests | |
printf("\n"); | |
printf("Question 4\n-----------\n"); | |
test_q4("abcde"); | |
test_q4("ABCDE"); | |
test_q4("12345"); | |
test_q4(" "); | |
test_q4("aB3 "); | |
return EXIT_SUCCESS; | |
} | |
/* | |
* Question 1 | |
* String to integer | |
* Only allowed atoi() and strtol() | |
*/ | |
int q1Cases = 0; | |
void test_q1(char *input) { | |
printf("Test Case %d\n", ++q1Cases); | |
printf("Input: %s\n", input); | |
printf("Answer: %d\n\n", str_to_int(input)); | |
} | |
int str_to_int(char *input) { | |
// Validate input is all numerical | |
char *cursor = input; | |
int inputSize = 0; | |
while (*cursor != '\0') { | |
char c = *cursor; | |
switch (c) { | |
case '0': | |
case '1': | |
case '2': | |
case '3': | |
case '4': | |
case '5': | |
case '6': | |
case '7': | |
case '8': | |
case '9': | |
break; | |
default: | |
printf("Non-numerical input occurred\n"); | |
return -1; | |
} | |
cursor++; | |
inputSize++; | |
} | |
// Check Zero | |
if (inputSize == 1 && *input == '0') { | |
return 0; | |
} | |
// Since longs can handle more chars than int, we can return that instead. | |
long longCheck = strtol(input, &input, 10); | |
if (longCheck > (long) INT_MAX || longCheck == 0) { | |
printf("Input greater than integer occurred\n"); | |
return -1; | |
} else { | |
return (int) longCheck; | |
} | |
} | |
/* | |
* Question 2 | |
* Comma Separated String addition | |
*/ | |
int q2Cases = 0; | |
void test_q2(char *input) { | |
printf("Test Case %d\n", ++q2Cases); | |
printf("Input: %s\n", input); | |
printf("Answer: %d\n\n", csv_add(input)); | |
} | |
int csv_add(char *input) { | |
// Validate input is all numerical or comma | |
char *cursor = input; | |
int inputSize = 0; | |
while (*cursor != '\0') { | |
char c = *cursor; | |
switch (c) { | |
case '0': | |
case '1': | |
case '2': | |
case '3': | |
case '4': | |
case '5': | |
case '6': | |
case '7': | |
case '8': | |
case '9': | |
case ',': | |
break; | |
default: | |
printf("Not valid CSV data\n"); | |
return -1; | |
} | |
cursor++; | |
inputSize++; | |
} | |
// Create char array for us to use in strtok | |
char *something = malloc(inputSize); | |
strcpy(something, input); | |
// Break up string into tokens | |
char *token; | |
token = strtok(something, ","); | |
int sum = 0; | |
while (token != NULL) { | |
int nextValue = str_to_int(token); | |
if (nextValue == -1) { | |
printf("Number too large for Integer\n"); | |
return -1; | |
} | |
if (INT_MAX - sum < nextValue) { | |
printf("Total sum too large for Integer\n"); | |
return -1; | |
} | |
sum += nextValue; | |
token = strtok(NULL, ","); | |
} | |
return sum; | |
} | |
/* | |
* Question 3 | |
* User Input Comma Separated String addition | |
*/ | |
int q3Cases = 0; | |
void test_q3(char *input) { | |
printf("Test Case %d\n", ++q3Cases); | |
printf("Input: %s\n", input); | |
printf("Answer: %d\n\n", csv_keyboard_input()); | |
} | |
int csv_keyboard_input() { | |
int bufferSize = 100; | |
char *buffer = malloc(bufferSize); | |
char *input = fgets(buffer, bufferSize, stdin); | |
// Remove potential new line character | |
int len = strlen(input); | |
if (input[len - 1] == '\n') { | |
input[len - 1] = '\0'; | |
} | |
return csv_add(input); | |
} | |
/* | |
* Question 4 | |
* User Input Comma Separated String addition | |
*/ | |
int q4Cases = 0; | |
void test_q4(char *input) { | |
printf("Test Case %d\n", ++q4Cases); | |
printf("Input: %s\n", input); | |
struct Histogram result = get_char_breakdown(input); | |
printf("Answer:\nUpper: %d\nLower: %d\nNumbers: %d\nSpaces: %d\n\n", | |
result.upper, | |
result.lower, | |
result.num, | |
result.space | |
); | |
} | |
struct Histogram get_char_breakdown(char *input) { | |
struct Histogram output; | |
int upper = 0; | |
int lower = 0; | |
int num = 0; | |
int space = 0; | |
char *cursor = input; | |
while (*cursor != '\0') { | |
char c = *cursor; | |
if (isupper(c)) { | |
upper++; | |
} else if (islower(c)) { | |
lower++; | |
} else if (isnumber(c)) { | |
num++; | |
} else if (isspace(c)) { | |
space++; | |
} | |
cursor++; | |
} | |
output.upper = upper; | |
output.lower = lower; | |
output.num = num; | |
output.space = space; | |
return output; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void test_q1(char *input); | |
void test_q2(char *input); | |
void test_q3(char *input); | |
void test_q4(char *input); | |
int str_to_int(char *input); | |
int csv_add(char *input); | |
int csv_keyboard_input(); | |
struct Histogram{ | |
int upper; | |
int lower; | |
int num; | |
int space; | |
}; | |
struct Histogram get_char_breakdown(char *input); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment