Created
January 13, 2011 07:35
-
-
Save mrdaemon/777535 to your computer and use it in GitHub Desktop.
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
/* - | |
* TP No. 1 | |
* "Doing Melissa's programming homework out of boredom" | |
* (c) 2010-2011 | |
* Alexandre Gauthier <[email protected]> | |
* | |
* Specifications: | |
* | |
* Read 10 numbers from the user and store them in an array | |
* of 10 elements. | |
* | |
* Output the following lists: | |
* - Numbers that are less than 15 | |
* - Odd Numbers | |
* - Reversed array (LIFO - Last in, first out) | |
* | |
* Emphasis on the following material: | |
* - Program functionality | |
* - Functions and Arrays | |
* - Overall presentation (comments, indent, output...) | |
* | |
*/ | |
#include <stdio.h> | |
/* Poor Man's Boolean using preprocessor macros | |
* WARNING: Never test directly against TRUE! Non-zero values | |
* other than 1 would cause that test to fail! | |
*/ | |
#define TRUE 1 | |
#define FALSE 0 | |
/* Magic Constants */ | |
#define NUMCOUNT 10 /* Amount of numbers */ | |
#define LESSTHANVAL 15 /* Value to test < against */ | |
/* Type for pointer to test function */ | |
typedef unsigned char (*p_testf)(int); | |
/* Function Prototypes */ | |
void get_numbers(int dst[], int dstsize); | |
void print_array(int nums[], int size); | |
void print_reversed_array(int nums[], int size); | |
void run_test(int nums[], int size, p_testf tfunc); | |
unsigned char prompt_yn(const char *question); | |
unsigned char test_is_lt_spec(int num); | |
unsigned char test_is_odd(int num); | |
/* Program entry point */ | |
int main (int argc, char const* argv[]) | |
{ | |
int numbers[NUMCOUNT]; /* User entered numbers */ | |
int num_size = sizeof(numbers) / sizeof(int); /* numbers array size */ | |
/* Populate an array with pointers to the test functions */ | |
p_testf tests[] = { &test_is_lt_spec, &test_is_odd }; | |
int test_count = sizeof(tests) / sizeof(p_testf); /* Amount of tests */ | |
/* Print headers */ | |
printf("TP N.1, CPL - Melissa Fiorentino\n"); | |
printf("(c) Alexandre Gauthier 2010-2011\n"); | |
printf("================================\n"); | |
do { | |
printf("\n"); | |
/* Populate numbers array with user imput */ | |
printf("Please enter a series of %i integers:\n", NUMCOUNT); | |
get_numbers(numbers, num_size); | |
printf("Thank you.\n\n"); | |
printf("Entered integers: "); | |
print_array(numbers, num_size); | |
printf("\n\n"); | |
/* Run tests defined in our tests array */ | |
printf("Running tests...\n\n"); | |
int i; | |
for (i=0; i < test_count; i++){ | |
printf("Test case %i: ", i); | |
run_test(numbers, num_size, tests[i]); | |
printf("\nDone.\n"); | |
} | |
printf("Integers, in reverse order of entry:\n"); | |
print_reversed_array(numbers, num_size); | |
printf("\n"); | |
} while (prompt_yn("Run again?") == TRUE); | |
printf("\n---- Done.\n"); | |
return 0; | |
} | |
/* get numbers from user and store them in | |
* specified integer array. Array size | |
* dictates the amount of numbers prompted for. | |
* It will validate if the input is a decimal | |
* integer before adding it to the destination array. | |
* | |
* dst[] - array to store the elements into. | |
* dstsize - lenght of the integer array (in elements) | |
*/ | |
void get_numbers(int dst[], int dstsize){ | |
int i; | |
int input; | |
char buffer[5]; | |
for(i=0; i < dstsize; i++){ | |
while(1){ | |
printf("[%i]> ", i + 1); | |
fgets(buffer, sizeof(input), stdin); | |
printf("\n"); | |
if(sscanf(buffer, "%i", &input)){ | |
dst[i] = input; | |
break; | |
} else { | |
printf("Input must be a decimal integer!\n"); | |
} | |
} | |
} | |
} | |
/** | |
* Print the contents of an array of integers | |
* separated by spaces, on a single line. | |
* | |
* nums[] -- array to print | |
* size -- size of array | |
*/ | |
void print_array(int nums[], int size){ | |
int i; | |
for(i=0; i < size; i++){ | |
printf("%i ", nums[i]); | |
} | |
} | |
/** | |
* Print the contents of an array of integers | |
* in reverse order (last in, first out), | |
* separated by spaces on a single line. | |
* | |
* nums[] -- array to print in reverse | |
* size -- size of array | |
*/ | |
void print_reversed_array(int nums[], int size){ | |
int i; | |
for(i=size; i > 0; --i){ | |
printf("%i ", nums[i]); | |
} | |
} | |
/** | |
* Run a test on an array of integers, printing | |
* only the elements that succeed the test on a | |
* single line. | |
* | |
* nums[] -- array of integers | |
* size -- size of array | |
* tfunc -- pointer to function matching the signature | |
* unsigned char func(int num) | |
*/ | |
void run_test(int nums[], int size, p_testf tfunc){ | |
int i; | |
printf("--------------------------------------\n"); | |
for (i=0; i < size; i++){ | |
if (tfunc(i) == TRUE) | |
printf("%i ", nums[i]); | |
} | |
printf("\nTest Unit Completed.\n"); | |
} | |
/** | |
* Prompt the user with an arbitrary binary | |
* question. Automatically follows the question | |
* specified with [Y/N], returning 1 (TRUE) on | |
* affirmative, and 0 (FALSE) on negative. | |
* | |
* question - Question to ask, ex. "Continue?" | |
*/ | |
unsigned char prompt_yn(const char *question){ | |
int c; /* getchar() must be stored in a type | |
larger than char, to allow for EOF */ | |
while(TRUE) { | |
printf("%s [y/n] ", question); | |
/* Damn you, buffered io */ | |
while((c = getchar()) != EOF){ | |
if(c != '\n') | |
putchar(c); | |
switch(c){ | |
case 'y': | |
case 'Y': | |
return TRUE; | |
case 'n': | |
case 'N': | |
return FALSE; | |
default: | |
printf("Please answer [Y]es or [N]o\n"); | |
break; | |
} | |
} | |
} | |
/** | |
* Test if number is lower than the value | |
* defined by the LESSTHANVAL magic number. | |
* Returns 1(TRUE) on success, 0(FALSE) on | |
* failure. | |
* | |
* Intended to be passed as a pointer to the | |
* run_test() function. | |
* | |
* num -- integer to test | |
*/ | |
unsigned char test_is_lt_spec(int num){ | |
/* Ternary operator, shorthand */ | |
return (num < LESSTHANVAL) ? TRUE : FALSE; | |
} | |
/** | |
* Test if integer is odd. | |
* Returns 1(TRUE) on success, 0(FALSE) on | |
* failure. | |
* | |
* Intended to be passed as a pointer to the | |
* run_test() function. | |
* | |
* num -- integer to test | |
*/ | |
unsigned char test_is_odd(int num){ | |
/* Ternary operator, shorthand */ | |
return (!(num % 2)) ? TRUE : FALSE; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment