Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #ifndef TKK_AS_C_GENERIC_PRINTER_H
  2. #define TKK_AS_C_GENERIC_PRINTER_H
  3. #include <stdio.h>
  4.  
  5. #include "specialType.h"
  6.  
  7.  
  8. /* Enumeration is a set of named integer constants. It gives you an
  9. * opportunity to use a more descriptive notation for constant values.
  10. * If not defined otherwise, the first enumeration is considered to
  11. * be zero, second enumeration one, etc. In this exercise the function
  12. * you must implement takes an enumerated constant as a parameter to
  13. * define the type of the memory address pointed by the void pointer.
  14. * You can use enumerations for example in switch-case- or if-statements.
  15. * Example:
  16. * enum TYPE t;
  17. * t = STRING;
  18. * // some code here
  19. * switch(t)
  20. * {
  21. * case INT:
  22. * // do something
  23. * break;
  24. * case STRING:
  25. * // do something
  26. * break;
  27. * // and so on...
  28. * }
  29. */
  30. enum TYPE
  31. {
  32. INT,
  33. DOUBLE,
  34. CHAR,
  35. STRING,
  36. BOOL,
  37. SPECIAL,
  38. };
  39.  
  40.  
  41. /* SHORT DESCRIPTION:
  42. * --------------------
  43. * Prints the contents of the memory address pointed by void* value
  44. * with the assumption that content is a variable of TYPE t.
  45. *
  46. * ASSUMPTIONS / SPECIAL CASES:
  47. * --------------------
  48. * INT, DOUBLE, CHAR and STRING are printed as they were normal pointers
  49. * of given type. Newline should be printed after the data.
  50. * TYPE BOOL should print "true\n" if pointer points to some memory address
  51. * and "false\n" if pointer is NULL.
  52. * If TYPE t is SPECIAL the function doesn't print anything by itself but
  53. * calls function void printSpecialType(void*) with given value. This
  54. * handles the printing of special types.
  55. *
  56. * RETURN VALUES:
  57. * None.
  58. */
  59. void genericPrinter(enum TYPE t, void* value) {
  60.  
  61.  
  62. switch(t) {
  63.  
  64. case INT:
  65. printf("%d\n", *((int*)value));
  66. break;
  67.  
  68. case DOUBLE:
  69. printf("%f\n", *((double*)value));
  70. break;
  71.  
  72. case CHAR:
  73. printf("%c\n", *((char*)value));
  74. break;
  75.  
  76. case STRING:
  77. printf("%s\n", ((char*)value));
  78. break;
  79.  
  80. case BOOL:
  81. if(value == NULL)
  82. printf("false\n");
  83. else
  84. printf("true\n");
  85.  
  86. break;
  87.  
  88. case SPECIAL:
  89. printSpecialType(value);
  90. break;
  91. }
  92.  
  93. }
  94.  
  95.  
  96.  
  97.  
  98.  
  99. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement