Guest User

Untitled

a guest
Jan 18th, 2020
75
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <iostream>
  3.  
  4. typedef struct node {
  5. char* value;
  6. node* next;
  7. } node;
  8.  
  9. void print_list(node* head) {
  10. node* current = head;
  11. while (current) {
  12. printf("%s\n", current->value);
  13. current = current->next;
  14. }
  15. printf("\n");
  16. return;
  17. }
  18.  
  19. void swap(char* m, char* n) {
  20. char t = *m;
  21. *m = *n;
  22. *n = t;
  23. return;
  24. }
  25.  
  26. node* sort(node* head) {
  27. struct node* a = NULL;
  28. struct node* b = NULL;
  29. struct node* c = NULL;
  30. struct node* e = NULL;
  31. struct node* tmp = NULL;
  32. while (e != head->next)
  33. {
  34. c = a = head;
  35. b = a->next;
  36. while (a != e)
  37. {
  38. if (a->value[0] > b->value[0])
  39. {
  40. if (a == head)
  41. {
  42. tmp = b->next;
  43. b->next = a;
  44. a->next = tmp;
  45. head = b;
  46. c = b;
  47. }
  48. else
  49. {
  50. tmp = b->next;
  51. b->next = a;
  52. a->next = tmp;
  53. c->next = b;
  54. c = b;
  55. }
  56. }
  57. else
  58. {
  59. c = a;
  60. a = a->next;
  61. }
  62. b = a->next;
  63. if (b == e) e = a;
  64. }
  65. }
  66. return head;
  67. }
  68.  
  69. /*node* add_in_head(node* head, char* value)
  70. {
  71. node* temp = (node*)malloc(sizeof(node));
  72. temp->value = value;
  73. temp->next = head;
  74. //temp = sort(temp);
  75. return temp;
  76. }*/
  77.  
  78. node* add_in_head(node** q, char* value)
  79. {
  80. node* tmp = NULL;
  81. tmp = *q;
  82. if (*q == NULL)
  83. {
  84. *q = (node*)malloc(sizeof(struct node));
  85. tmp = *q;
  86. }
  87. else
  88. {
  89. while (tmp->next != NULL)
  90. tmp = tmp->next;
  91. tmp->next = (node*)malloc(sizeof(struct node));
  92. tmp = tmp->next;
  93. }
  94. tmp->value = value;
  95. tmp->next = NULL;
  96. return tmp;
  97. }
  98.  
  99. int ch_str(char* str, char ch) {
  100. int i = 0;
  101. while (str[i]) {
  102. if (str[i] == ch) {
  103. return i;
  104. }
  105. i++;
  106. }
  107. return -1;
  108. }
  109. void longest(char* str, int* A) {
  110. char sep[] = "., ()!?\n\t";
  111. int i = 0, max_len = 0, curr = 0, in_word = 1, j = 0;
  112. int pos[50];
  113. while (str[i]) {
  114. if (ch_str(sep, str[i]) != -1) {
  115. if (in_word) {
  116. if (curr > max_len) {
  117. pos[j] = i;
  118. max_len = curr;
  119. }
  120. else
  121. {
  122. if (curr == max_len) {
  123. j++;
  124. pos[j] = i;
  125. }
  126. }
  127. curr = 0;
  128. in_word = 0;
  129. }
  130. }
  131. else {
  132. curr++;
  133. in_word = 1;
  134. }
  135. i++;
  136. }
  137. if (in_word) {
  138. if (curr > max_len) {
  139. max_len = curr;
  140. pos[j] = i;
  141. }
  142. else
  143. {
  144. if (curr == max_len) {
  145. j++;
  146. pos[j] = i;
  147. }
  148. }
  149. }
  150. A[0] = (int)max_len;
  151. for (i = 0; i <= j; i++) {
  152. A[i + 1] = pos[i];
  153. //printf("%d\n",pos[i]);
  154. }
  155. return;
  156. }
  157.  
  158.  
  159. char* reverse_sub(char* str, int start, int end) {
  160. while (start < end) {
  161. swap(str + start++, str + end--);
  162. }
  163. return str;
  164. }
  165.  
  166. int main()
  167. {
  168. char str[255];
  169. int k = 0, h = 0;
  170. FILE* fp = fopen("c:\\vvv\\test.txt", "r");
  171. if (fp == NULL) {
  172. printf("Error!\n");
  173. exit(1);
  174. }
  175. int start = 0;
  176. int end = 0;
  177. int A[50];
  178. node* head = NULL;
  179. while (fgets(str, 51, fp))
  180. {
  181. memset(A, -1, sizeof(A));
  182. longest(str, A);
  183. for (k = 1; k < 100; k++)
  184. {
  185. if (A[k] == -1) break;
  186. start = (int)A[k] - (int)A[0];
  187. end = (int)A[k] - 1;
  188. reverse_sub(str, start, end);
  189. }
  190. //printf(str);
  191. head = add_in_head(&head, str);
  192. print_list(head);
  193. head = sort(head);
  194. }
  195. //head = sort(head);
  196. //print_list(head);
  197. return 0;
  198. }
RAW Paste Data