Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef TKK_AS_C_GENERIC_PRINTER_H
- #define TKK_AS_C_GENERIC_PRINTER_H
- #include <stdio.h>
- #include "specialType.h"
- /* Enumeration is a set of named integer constants. It gives you an
- * opportunity to use a more descriptive notation for constant values.
- * If not defined otherwise, the first enumeration is considered to
- * be zero, second enumeration one, etc. In this exercise the function
- * you must implement takes an enumerated constant as a parameter to
- * define the type of the memory address pointed by the void pointer.
- * You can use enumerations for example in switch-case- or if-statements.
- * Example:
- * enum TYPE t;
- * t = STRING;
- * // some code here
- * switch(t)
- * {
- * case INT:
- * // do something
- * break;
- * case STRING:
- * // do something
- * break;
- * // and so on...
- * }
- */
- enum TYPE
- {
- INT,
- DOUBLE,
- CHAR,
- STRING,
- BOOL,
- SPECIAL,
- };
- /* SHORT DESCRIPTION:
- * --------------------
- * Prints the contents of the memory address pointed by void* value
- * with the assumption that content is a variable of TYPE t.
- *
- * ASSUMPTIONS / SPECIAL CASES:
- * --------------------
- * INT, DOUBLE, CHAR and STRING are printed as they were normal pointers
- * of given type. Newline should be printed after the data.
- * TYPE BOOL should print "true\n" if pointer points to some memory address
- * and "false\n" if pointer is NULL.
- * If TYPE t is SPECIAL the function doesn't print anything by itself but
- * calls function void printSpecialType(void*) with given value. This
- * handles the printing of special types.
- *
- * RETURN VALUES:
- * None.
- */
- void genericPrinter(enum TYPE t, void* value) {
- switch(t) {
- case INT:
- printf("%d\n", *((int*)value));
- break;
- case DOUBLE:
- printf("%f\n", *((double*)value));
- break;
- case CHAR:
- printf("%c\n", *((char*)value));
- break;
- case STRING:
- printf("%s\n", ((char*)value));
- break;
- case BOOL:
- if(value == NULL)
- printf("false\n");
- else
- printf("true\n");
- break;
- case SPECIAL:
- printSpecialType(value);
- break;
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement