Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #include "stdlib.h"
  2. #include "stdio.h"
  3. #include "list.h"
  4. #include "string.h"
  5. #include "stdint.h"
  6.  
  7.  
  8. void stringToNum(element_t * tp, element_t t){
  9. char* thingToBeParsed = (char*) t;
  10. intptr_t* storingThing = (intptr_t*) tp;
  11. char* endChar;
  12. *storingThing = strtol(thingToBeParsed, &endChar, 10);
  13. if (endChar == thingToBeParsed){
  14. *storingThing = -1;
  15. }
  16. }
  17.  
  18. void numToString(element_t * tp, element_t t, element_t num){
  19. char* thingToBeParsed = (char*) t;
  20. char** storingThing = (char**) tp;
  21. intptr_t number = (intptr_t) num;
  22. if (number<0){
  23. *storingThing = thingToBeParsed;
  24. } else{
  25. *storingThing = NULL;
  26. }
  27. }
  28.  
  29. int isPositive(element_t t){
  30. intptr_t tp = (intptr_t) t;
  31. if (tp<0){
  32. return 0;
  33. } else {
  34. return 1;
  35. }
  36. }
  37.  
  38. int isNotNull(element_t t){
  39. char* tp = (char*) t;
  40. if (tp == NULL){
  41. return 0;
  42. } else {
  43. return 1;
  44. }
  45. }
  46.  
  47. void truncStr(element_t* out, element_t str, element_t i){
  48. char** strOut = (char**) out;
  49. char* strIn = str;
  50. intptr_t intIn = (intptr_t) i;
  51. if (strlen(strIn) <= intIn){
  52. *strOut = strIn;
  53. return;
  54. }
  55. strIn[(int)intIn] = 0;
  56. *strOut = strIn;
  57. }
  58.  
  59. // void print(element_t str){
  60.  
  61. // char* strp = (char*) str;
  62. // if (strlen(strp)==0){
  63. // printf("");
  64. // return;
  65. // }
  66. // printf("%s ", strp);
  67. // }
  68.  
  69. void print1(element_t str){
  70. char* strp = (char*) str;
  71. printf("%s\n", strp);
  72. }
  73.  
  74. void findMax(element_t * outPtr, element_t outVal, element_t inVal){
  75. intptr_t inV = (intptr_t) inVal;
  76. intptr_t outV = (intptr_t) outVal;
  77. intptr_t* outP = (intptr_t*) outPtr;
  78. if (inV>outV){
  79. *outP = inV;
  80. } else {
  81. *outP = outV;
  82. }
  83. }
  84.  
  85. void attach(element_t * outPtr, element_t outVal, element_t inVal){
  86. char** outP = (char**) outPtr;
  87. char* outV = (char*) outVal;
  88. char* inV = (char*) inVal;
  89. if (strlen(*outP)==0){
  90. *outP = realloc(*outP, strlen(inV)+1);
  91. strcpy(*outP, inV);
  92. return;
  93. }
  94. char space[] = " ";
  95. const int newLen = strlen(*outP) + strlen(inV) +3;
  96. *outP = realloc(*outP, newLen);
  97. strcat(*outP, space);
  98. strcat(*outP, inV);
  99. }
  100.  
  101.  
  102. int main(int argc, char* argv[]){
  103. struct list* l0 = list_create();
  104. for (int i=1; i<argc; i++){
  105. list_append(l0, (element_t) argv[i]);
  106. }
  107.  
  108. struct list* l1 = list_create();
  109. list_map1(stringToNum, l1, l0);
  110.  
  111. struct list* l2 = list_create();
  112. list_map2(numToString, l2, l0, l1);
  113.  
  114. struct list* l3 = list_create();
  115. list_filter(isPositive, l3, l1);
  116.  
  117. struct list* l4 = list_create();
  118. list_filter(isNotNull, l4, l2);
  119.  
  120. struct list* l5 = list_create();
  121. list_map2(truncStr, l5, l4, l3);
  122.  
  123. list_foreach(print1, l5);
  124.  
  125. char* result = malloc(sizeof(*result));
  126. //result = (char*)'\0';
  127. list_foldl(attach, (element_t*)&result, l5);
  128.  
  129. printf("%s", result);
  130. printf("\n");
  131.  
  132. element_t max = 0;
  133. list_foldl(findMax, &max, l3);
  134.  
  135. printf("%ld\n", (long) max);
  136.  
  137. free(result);
  138.  
  139. list_destroy(l0);
  140. list_destroy(l1);
  141. list_destroy(l2);
  142. list_destroy(l3);
  143. list_destroy(l4);
  144. list_destroy(l5);
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement