Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.46 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <progbase/console.h>
  4. #include <stdbool.h>
  5. #include <time.h>
  6. #include <progbase.h>
  7. #include <ctype.h>
  8. #include <string.h>
  9. #include <assert.h>
  10.  
  11. int indexofSecSentence(char*str);
  12. void copyOfFirstSentense(char*str,char*buffer, int len);
  13.  
  14. struct Sentense{
  15. int index;
  16. char Sentense[500];
  17. };
  18. struct Sentense texts[10000];
  19. struct SentenseArray{
  20. struct Sentense * text;
  21. int lenght;
  22. };
  23.  
  24.  
  25. bool isEqual(struct Sentense first,struct Sentense second);
  26. bool isEqualArrays(struct SentenseArray*first,struct SentenseArray*second);
  27.  
  28. struct Sentense firstSentense(char*str);
  29.  
  30. struct SentenseArray * getAllSentencesArrayNew(const char * str);
  31.  
  32. void freeArray(struct SentenseArray * array);
  33.  
  34. void getAllSentencesArray(const char * str);
  35.  
  36. int main(void){
  37. int len = 400;
  38. char buffer[len];
  39. buffer[0] = '\0';
  40. char text[]={"At 00:00, Cinderella ran away from her prince, leaving a glass slipper on the marbled steps of the grand staircase. At 00:00, I ran away from him. But unlike Cinderella, I didn't leave a single thing behind."};
  41.  
  42.  
  43.  
  44.  
  45.  
  46. // TESTS FOR 1 TASK;
  47.  
  48.  
  49. assert(indexofSecSentence("aasfwrh, gsfgsgd s hdhf.h") == 24);
  50. assert(indexofSecSentence("a.asfwrh, gsfgsgd s hdhfh") == 2);
  51. assert(indexofSecSentence(".aasfwrh, gsfgsgd s hdhfh") == 1);
  52. assert(indexofSecSentence("!.?aasfwrh, gsfgsgd s hdhfh") == 3);
  53. assert(indexofSecSentence("aasfwrh, gsfgsgd s hdhfh") == -1);
  54. assert(indexofSecSentence(NULL) == -1);
  55.  
  56.  
  57. copyOfFirstSentense("aasfwrh, gsfgsgd s hdhf.h", buffer, 50);
  58. assert(strcmp(buffer,"aasfwrh, gsfgsgd s hdhf")==0);
  59. buffer[0] = '\0';
  60.  
  61. copyOfFirstSentense("aasfwrh, gsfgsgd s hdh.fh",buffer, 50);
  62. assert(strcmp(buffer,"aasfwrh, gsfgsgd s hdh")==0);
  63. buffer[0] = '\0';
  64.  
  65. copyOfFirstSentense(".aasfwrh, gsfgsgd s hdhfh",buffer, 50);
  66. assert(strcmp(buffer,"")==0);
  67. buffer[0] = '\0';
  68.  
  69. copyOfFirstSentense(NULL,buffer, len);
  70. assert(strcmp(buffer,"")==0);
  71. buffer[0] = '\0';
  72.  
  73. copyOfFirstSentense("aasfwrh, gsfgsgd s hdhfh",buffer, 5);
  74. assert(strcmp(buffer,"aasfw")==0);
  75. buffer[0] = '\0';
  76.  
  77. // TESTS FOR TASK 2;
  78. // Comparing Sentenses
  79.  
  80. struct Sentense one = {0,"you are here"};
  81. struct Sentense two = {0,"you are here"};
  82. struct Sentense three = {1,"you are here"};
  83. struct Sentense four = {0,"you are here "};
  84. assert(isEqual(one,one)==1);
  85. assert(isEqual(one,two)==1);
  86. assert(isEqual(one,three)==0);
  87. assert(isEqual(one,four)==0);
  88. assert(isEqual(three,four)==0);
  89.  
  90. //Comparing SentensesArrays
  91.  
  92. struct Sentense a[3] = {one,two,three};
  93. struct Sentense b[3] = {one,two,three};
  94. struct Sentense c[3] = {one,two,four};
  95. struct Sentense d[4] = {one,two,three,four};
  96. struct Sentense e[3] = {two,one,three};
  97.  
  98. struct SentenseArray a1 = { &a[0], 3};
  99. struct SentenseArray a2 = { &b[0], 3};
  100. struct SentenseArray a3 = { &c[0], 4};
  101. struct SentenseArray a4 = { &d[0], 4};
  102. struct SentenseArray a5 = { &e[0], 3};
  103.  
  104. assert ( isEqualArrays ( &a1 , &a2 ) == 1);
  105. assert ( isEqualArrays ( &a1 , &a1 ) == 1);
  106. assert ( isEqualArrays ( &a2 , &a2 ) == 1);
  107. assert ( isEqualArrays ( &a1 , &a3 ) == 0);
  108. assert ( isEqualArrays ( &a1 , &a4 ) == 0);
  109. assert ( isEqualArrays ( &a3 , &a4 ) == 0);
  110. assert ( isEqualArrays ( &a1 , &a5 ) == 1);
  111.  
  112. //testing function firstSentense
  113.  
  114. struct Sentense test1 = {0,"At 00:00, Cinderella ran away from her prince, leaving a glass slipper on the marbled steps of the grand staircase"};
  115. struct Sentense test2 = {1,"At 00:00, Cinderella ran away from her prince, leaving a glass slipper on the marbled steps of the grand staircase"};
  116. struct Sentense test3 = {0,"At 00:00, Cinderella ran away from her prince, leaving a glass slipper on the marbled steps of the grand staircase."};
  117.  
  118. assert (isEqual(firstSentense( text ), test1 ) == 1);
  119. assert (isEqual(firstSentense( text ), test2 ) == 0);
  120. assert (isEqual(firstSentense( text ), test3 ) == 0);
  121.  
  122. //task3 tests
  123. char*p2str = text;
  124. int n = 0;
  125. int lenght = 0;
  126. while(p2str!='\0' && n != -1){
  127. lenght = lenght + 1;
  128. n = indexofSecSentence(p2str);
  129. p2str = p2str + n;
  130. }
  131. const char tex[]={"At 00:00, Cinderella ran away from her prince, leaving a glass slipper on the marbled steps of the grand staircase. At 00:00, I ran away from him. But unlike Cinderella, I didn't leave a single thing behind."};
  132. const char tex2[]={"At 00:00, I ran away from him. But unlike Cinderella, I didn't leave a single thing behind."};
  133.  
  134. struct SentenseArray all = {texts,lenght};
  135. struct SentenseArray*p = &all;
  136. p = getAllSentencesArrayNew(tex);
  137.  
  138. struct SentenseArray vll = {texts,lenght};
  139. struct SentenseArray*z = &vll;
  140. z = getAllSentencesArrayNew(tex);
  141.  
  142. struct SentenseArray pll = {texts,lenght};
  143. struct SentenseArray*m = &pll;
  144. m = getAllSentencesArrayNew(tex2);
  145.  
  146. struct SentenseArray ill = {texts,lenght};
  147. struct SentenseArray*i = &ill;
  148. i = getAllSentencesArrayNew(tex2);
  149.  
  150. assert( isEqualArrays ( p , p ) == 1);
  151. assert( isEqualArrays ( p , z ) == 1);
  152. assert( isEqualArrays ( p , m ) == 0);
  153. assert( isEqualArrays ( p , i ) == 0);
  154.  
  155. freeArray(z);
  156. freeArray(p);
  157. freeArray(m);
  158. freeArray(i);
  159.  
  160. return 0;
  161. }
  162.  
  163. int indexofSecSentence(char*str){
  164. char cha[]={".!?"};
  165. if(str!=NULL){
  166. int m = strlen(str);
  167. for(int i = 0; i < m; i++){
  168. if (strchr(cha,str[i])){
  169. while(strchr(cha,str[i+1])&&i+1<m){
  170. i++;
  171. }
  172. if (strchr(cha,str[i+1])==0){
  173. return i+1;
  174. }
  175. else {
  176. return -1;
  177. }
  178. }
  179. }
  180. }
  181. return -1;
  182. }
  183.  
  184.  
  185. void copyOfFirstSentense(char*str,char*buffer, int len){
  186. char cha[]={".!?"};
  187. int n = 0;
  188. if(str!=NULL){
  189. int m = strlen(str);
  190. for(int i = 0; i < m; i++){
  191. if (strchr(cha,str[i])==0&&n<len){
  192. buffer[n] = str[i];
  193. n++;
  194. }
  195. else{
  196. break;
  197. }
  198. }
  199. }
  200. buffer[n] = '\0';
  201. }
  202.  
  203. bool isEqual(struct Sentense first,struct Sentense second){
  204. if(first.index == second.index && strcmp(first.Sentense,second.Sentense)==0){
  205. return true;
  206. }
  207. return false;
  208. }
  209.  
  210. bool isEqualArrays(struct SentenseArray*first,struct SentenseArray*second){
  211. if(first->lenght == second->lenght){
  212. for(int i = 0;i<(first->lenght);i++){
  213. if(!isEqual(first->text[i], second->text[i])){
  214. return false;
  215. }
  216. }
  217. return true;
  218. }
  219. return false;
  220. }
  221.  
  222. struct Sentense firstSentense(char*str){
  223. struct Sentense first;
  224. first.index = 0;
  225. copyOfFirstSentense(str,first.Sentense,500);
  226. return first;
  227. }
  228.  
  229.  
  230. void getAllSentencesArray(const char * str){
  231. //char b[] = "asdadsadqeqw .asfasfasffsasf.";
  232. char b[strlen(str)];
  233. strcpy(b,str);
  234. b[strlen(b)-1] = '\0';
  235. char * pstr = b;
  236. char*p2str = pstr;
  237. int lenght = 0;
  238.  
  239. int n = 0;
  240. while(p2str!='\0' && n != -1){
  241. lenght = lenght + 1;
  242. n = indexofSecSentence(p2str);
  243. p2str = p2str + n;
  244. }
  245.  
  246.  
  247. struct SentenseArray heap = {texts,4};
  248. heap.lenght = lenght;
  249. int index = 0;
  250. heap.text[0].index = index;
  251. for (int i = 0; i<lenght;i++){
  252. copyOfFirstSentense(pstr,heap.text[i].Sentense,500);
  253. puts(heap.text[i].Sentense);
  254. heap.text[i].index = index;
  255. index = index + n;
  256. n = indexofSecSentence(pstr);
  257. pstr = pstr + n;
  258. }
  259. printf("%i",lenght);
  260. puts(" ");
  261. }
  262.  
  263. struct SentenseArray * getAllSentencesArrayNew(const char * str){
  264. //char b[] = "asdadsadqeqw .asfasfasffsasf.";
  265. char b[strlen(str)];
  266. strcpy(b,str);
  267. b[strlen(b)-1] = '\0';
  268. char * pstr = b;
  269. char*p2str = pstr;
  270. int n = 0;
  271. int lenght = 0;
  272. while(p2str!='\0' && n != -1){
  273. lenght = lenght + 1;
  274. n = indexofSecSentence(p2str);
  275. p2str = p2str + n;
  276. }
  277.  
  278. struct SentenseArray*heap = malloc(sizeof(struct SentenseArray));
  279. heap->text = malloc(lenght * sizeof(struct Sentense));
  280. heap->lenght = lenght;
  281. int index = 0;
  282. heap->text[0].index = index;
  283. for (int i = 0; i<heap->lenght;i++){
  284. copyOfFirstSentense(pstr,heap->text[i].Sentense,500);
  285. // puts(heap->text[i].Sentense);
  286. heap->text[i].index = index;
  287. index = index + n;
  288. n = indexofSecSentence(pstr);
  289. pstr = pstr + n;
  290.  
  291. }
  292. // printf("%i",heap->lenght);
  293. //puts(" ");
  294. //freeArray(heap);
  295. return heap;
  296. }
  297.  
  298. void freeArray(struct SentenseArray * array) {
  299. free(array->text); //
  300. free(array); //
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement