Advertisement
Guest User

Untitled

a guest
Nov 25th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. #include <assert.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define ARRAY_AMOUNT 500
  8. // structs type
  9. struct Paragraph {
  10. int index;
  11. char buff[ARRAY_AMOUNT];
  12. };
  13. struct ParagraphArray {
  14. struct Paragraph *buffpoint;
  15. int size;
  16. };
  17.  
  18. // This is my function
  19.  
  20. int itemIndex(char *point);
  21. int itemIndex2(const char *point, int curIndex);
  22.  
  23. struct Paragraph firstPar(char *point);
  24. struct ParagraphArray *getAllParagraphsArrayNew(const char *str);
  25.  
  26. void arrayFree(struct ParagraphArray *array);
  27. void bufChange(const char *point, char *buffer, int size);
  28.  
  29. bool isEqualPar(struct Paragraph temp1, struct Paragraph temp2);
  30. bool isEqualParArr(struct ParagraphArray temp1, struct ParagraphArray temp2);
  31.  
  32. int main(void) {
  33. // First task // main
  34. char str[] = "1 - First\n\n2 - Second\n\n3 - Third\n\n4 - Fourth\n\n5 - Fifth";
  35. //char str2[] = "1 - First\n\n2 - Second";
  36. char buffer[] = {"Bufer\n\nI am so cool\n\nI am a Buffer"};
  37. int size = strlen(buffer);
  38.  
  39. assert(itemIndex(str) == 11);
  40. assert((itemIndex2(&buffer[0], 10)) == 21);
  41. assert(itemIndex2(str, 0) == 11);
  42.  
  43. bufChange(&str[0], &buffer[0], size);
  44.  
  45. // Second task // main
  46. struct Paragraph x = {1, "jalhsdfj"};
  47. struct Paragraph y = {1, "jalhsdfj"};
  48. // This struct created only to cheak "isEqualPar"
  49. struct Paragraph comparison = {.index = 0, .buff = "1 - First"};
  50. assert(isEqualPar(x, y) == 1);
  51. assert(isEqualPar(firstPar(str), x) == 0);
  52. assert(isEqualPar(firstPar(str), comparison) == 1);
  53.  
  54. // This structs created to check isEqualParArr
  55. struct Paragraph buf[3] = {
  56. {2, "Buffer filling"}, {15, "Bla - Bla - Bool"}, {300, "(x^2)` = 2*x"}};
  57. struct ParagraphArray temp1 = {.size = 3, .buffpoint = &buf[0]};
  58. struct Paragraph buf2[3] = {
  59. {2, "Buffer filling"}, {15, "Bla - Bla - Bool"}, {300, "(x^2)` = 2*x"}};
  60. struct ParagraphArray temp2 = {.size = 3, .buffpoint = &buf2[0]};
  61. struct Paragraph buf3[3] = {{4, "Tra - Ta - Ta "},
  62. {15, "Bla - Bla - Bool swimming pool"},
  63. {12, "I can in C "}};
  64. struct ParagraphArray temp3 = {.size = 3, .buffpoint = &buf3[0]};
  65.  
  66. assert(isEqualParArr(temp1, temp2) == 1);
  67. assert(isEqualParArr(temp1, temp3) == 0);
  68. struct ParagraphArray *d = getAllParagraphsArrayNew(str);
  69. for (int i = 0; i < d->size; i++) {
  70. printf("%s\n", d->buffpoint[i].buff);
  71. }
  72. arrayFree(d);
  73. return 0;
  74. }
  75.  
  76. // FIRST TASK
  77. int itemIndex(char *point) {
  78. int curr = 0;
  79. for (char *i = point; *i != '\0'; i++, curr++) {
  80. if (*i == '\n' && *(i + 1) == '\n' && *(i + 2) != '\0') {
  81. return curr + 2;
  82. }
  83. }
  84. return -1;
  85. }
  86.  
  87. int itemIndex2(const char *point, int curIndex) {
  88. for (const char *i = point + curIndex; *i != '\0'; i++, curIndex++) {
  89. if (*i == '\n' && *(i + 1) == '\n' && *(i + 2) != '\0') {
  90. return curIndex + 2;
  91. }
  92. }
  93. return -1;
  94. }
  95.  
  96. void bufChange(const char *point, char *buffer, int size) {
  97. for (int i = 0; i < size - 1; i++) {
  98. if ( point[i + 1] == '\0' || (point[i] == '\n' && point[i + 1] == '\n') )
  99. break;
  100. *(buffer + i) = *(point + i);
  101. };
  102. }
  103.  
  104. // SECOND TASK
  105. bool isEqualPar(struct Paragraph temp1, struct Paragraph temp2) {
  106. if (temp1.index == temp2.index && !strcmp(temp1.buff, temp2.buff))
  107. return true;
  108. return false;
  109. }
  110.  
  111. bool isEqualParArr(struct ParagraphArray temp1, struct ParagraphArray temp2) {
  112. if (temp1.size == temp2.size) {
  113. for (int i = 0; i < temp1.size; i++) {
  114. if (isEqualPar(temp1.buffpoint[i], temp2.buffpoint[i]) == 0)
  115. return false;
  116. }
  117. return true;
  118. }
  119. return false;
  120. }
  121.  
  122. struct Paragraph firstPar(char *point) {
  123. struct Paragraph CurrPar = {.index = 0, .buff = "\0"};
  124. for (int i = 0; point != NULL && i < ARRAY_AMOUNT; i++) {
  125. if ((point[i] == '\n' && point[i + 1] == '\n') || (i == ARRAY_AMOUNT - 1)) {
  126. CurrPar.buff[i] = '\0';
  127. return CurrPar;
  128. }
  129. CurrPar.buff[i] = point[i];
  130. }
  131. return CurrPar;
  132. }
  133.  
  134. // THIRD TASK
  135.  
  136. struct ParagraphArray * getAllParagraphsArrayNew(const char *str) {
  137. int counter = 0;
  138. int index = 0;
  139.  
  140. for (int i = 0; i < strlen(str); i++) {
  141. if (str[i + 1] == '\0' || (str[i] == '\n' && str[i + 1] == '\n'))
  142. counter++;
  143. }
  144. struct ParagraphArray * masspoint = malloc(sizeof(struct ParagraphArray));
  145. masspoint->size = counter;
  146. masspoint->buffpoint = calloc(sizeof(struct Paragraph), counter);
  147.  
  148. for (int i = 0; i < counter; i++) {
  149. bufChange(str + index, masspoint->buffpoint[i].buff, ARRAY_AMOUNT);
  150. index = itemIndex2(str, index);
  151. }
  152. return masspoint;
  153. }
  154.  
  155. // remember *(name).data == name->data
  156. void arrayFree(struct ParagraphArray *array) {
  157. if (array->buffpoint != NULL)
  158. free(array->buffpoint);
  159. free(array);
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement