Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.98 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <assert.h>
  6. #include <progbase.h>
  7. #include <progbase/console.h>
  8.  
  9. struct Sentence {
  10. int startIndex;
  11. char sentenceBuffer[128];
  12. };
  13.  
  14. struct SentenceArray {
  15. struct Sentence *sentences;
  16. int sizeofarray;
  17. };
  18.  
  19. //tasks
  20. int task_1_1(char txt[]);
  21. int task_1_2(char txt[], char buffer[], int sizeofbuffer);
  22. int task_2_1(struct Sentence sentence, struct Sentence sentence2);
  23. int task_2_2(struct SentenceArray sentenceArr, struct SentenceArray sentenceArr2);
  24. struct Sentence task_2_3(char txt[]);
  25. struct SentenceArray *getAllSentencesArrayNew(char * txt);
  26. void arrayFree(struct SentenceArray *array);
  27. int task4(char text[]);
  28.  
  29. void TestTask(int number);
  30.  
  31. //
  32. int ispunctend(char symb);
  33. int iscons(char symb);
  34.  
  35. //helpers
  36. int FindSentence(char txt[], int sentenceToFind, int arraytofill[]);
  37. int GetSentence(int sentencetofind, char txt[], char buffer[], int sizeofbuffer);
  38. struct Sentence CreateSentenceStruct(char txt[], int numsentence);
  39.  
  40. //
  41. int fileExists(const char *fileName);
  42. long getFileSize(const char *fileName);
  43. int readFileToBuffer(const char *fileName, char *buffer, int bufferLength);
  44.  
  45. int main(int argc, char * argv[]) {
  46. if(argc <= 1) return 1;
  47. else if(argc == 2){
  48. if(strcmp("task1", argv[1]) == 0) TestTask(1);
  49. else if(strcmp("task2", argv[1]) == 0) TestTask(2);
  50. else if(strcmp("task3", argv[1]) == 0) TestTask(3);
  51. else return 1;
  52. }
  53. else if(argc == 3){
  54. if(strcmp("task4", argv[1]) == 0) {
  55. if(fileExists(argv[2])){
  56. long size = getFileSize(argv[2]);
  57. char buffer[size];
  58. readFileToBuffer(argv[2], buffer, size);
  59. return task4(buffer);
  60. }
  61. else return 1;
  62. }
  63. else return 1;
  64. }
  65. else return 1;
  66.  
  67. return 0;
  68. }
  69.  
  70. void TestTask(int number) {
  71. char txt[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum volutpat elit vitae mollis pellentesque. Phasellus dignissim interdum consequat. Proin cursus feugiat augue, vitae laoreet quam interdum in. Duis vehicula urna ac dui rhoncus, ac blandit sem finibus. Maecenas id justo aliquet, tristique risus et, dictum enim. Etiam tempus malesuada augue eget dapibus. Maecenas neque quam, lacinia eget placerat et, malesuada ac leo.";
  72.  
  73. switch(number){
  74. case 1:
  75. assert(task_1_1(txt) == 57);
  76.  
  77. char buffer[128];
  78. task_1_2(txt, buffer, 128);
  79. char expecting[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
  80. assert(strcmp(buffer, expecting) == 0);
  81. break;
  82. case 2:{
  83. struct Sentence sent1 = {148, "Proin cursus feugiat augue, vitae laoreet quam interdum in."};
  84. struct Sentence sent2 = {148, "Proin cursus feugiat augue, vitae laoreet quam interdum in."};
  85. struct Sentence sent3 = {26, "Proin cursus feugiat augue, vitae laoreet quam interdum in."};
  86. struct Sentence sent4 = {327, "Etiam tempus malesuada augue eget dapibus."};
  87. assert(task_2_1(sent1, sent2) == 1);
  88. assert(task_2_1(sent1, sent3) == 0);
  89. assert(task_2_1(sent1, sent4) == 0);
  90.  
  91. //22
  92. /*struct SentenceArray sentArr1 = {
  93. struct Sentence sentences[8] = {
  94. {0, ""},{0, ""},{0, ""},{0, ""}
  95. },
  96. 8
  97. };*/
  98.  
  99. struct Sentence firstSentence = task_2_3(txt);
  100.  
  101. char expecting[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
  102. assert(firstSentence.startIndex == 0);
  103. assert(strcmp(firstSentence.sentenceBuffer, expecting) == 0);
  104. break;
  105. }
  106. case 3:{
  107. struct SentenceArray *result = getAllSentencesArrayNew(txt);
  108.  
  109. assert(result->sentences[5].startIndex == 267);
  110. assert(result->sizeofarray == 8);
  111. assert(result->sentences[8].startIndex == 0);
  112.  
  113. arrayFree(result);
  114. break;
  115. }
  116. }
  117. }
  118.  
  119. int task_1_2(char txt[], char buffer[], int sizeofbuffer) {
  120. return GetSentence(1, txt, buffer, sizeofbuffer);
  121. }
  122.  
  123. int task_2_1(struct Sentence sentence, struct Sentence sentence2) {
  124. if(sentence.startIndex != sentence2.startIndex) return 0;
  125. else if(strcmp(sentence.sentenceBuffer,sentence2.sentenceBuffer) != 0) return 0;
  126. return 1;
  127. }
  128.  
  129. int task_2_2(struct SentenceArray sentenceArr, struct SentenceArray sentenceArr2) {
  130. if(sentenceArr.sizeofarray != sentenceArr2.sizeofarray) return 0;
  131. else {
  132. for(int i = 0; i > sentenceArr.sizeofarray; i++){
  133. if(sentenceArr.sentences[i].startIndex != sentenceArr2.sentences[i].startIndex) return 0;
  134. else if(strcmp(sentenceArr.sentences[i].sentenceBuffer, sentenceArr2.sentences[i].sentenceBuffer) != 0) return 0;
  135. }
  136. }
  137. return 1;
  138. }
  139.  
  140. struct Sentence task_2_3(char txt[]){
  141. return CreateSentenceStruct(txt, 1);
  142. }
  143.  
  144. struct SentenceArray *getAllSentencesArrayNew(char *txt) {
  145. struct SentenceArray *sentencearray = malloc(sizeof(struct SentenceArray)); // init
  146. sentencearray->sentences = malloc(sizeof(struct Sentence) * 1); // init
  147.  
  148. int i = 1,
  149. indexes[] = {0,0},
  150. state = FindSentence(txt, i, indexes);
  151.  
  152. while(state){
  153. sentencearray->sentences = realloc(sentencearray->sentences, sizeof(struct Sentence) * i); // reallocate for new sentence
  154.  
  155. struct Sentence currentSentence = CreateSentenceStruct(txt, i);
  156. sentencearray->sentences[i - 1] = currentSentence;
  157.  
  158. i++;
  159. state = FindSentence(txt, i, indexes);
  160. }
  161.  
  162. sentencearray->sizeofarray = i - 1;
  163.  
  164. return sentencearray;
  165. }
  166.  
  167. void arrayFree(struct SentenceArray *array) {
  168. free(array->sentences);
  169. free(array);
  170. }
  171.  
  172. int task4(char text[]) {
  173. Console_clear();
  174.  
  175. printf("_______________________Завдання_______________________\n");
  176. printf("Шкала розфарбовування речень: менше 10 символів - червоний, від 10 до 25 - жовтий, більше 25 - зелений\n\
  177. Окремо вивести список тих речень, у яких більше 10 символів, а перше слово починається на приголосну літеру.\n");
  178. printf("______________________________________________________\n");
  179.  
  180. char buffer[128];
  181. int i = 1,
  182. len = 0,
  183. state = GetSentence(i, text, buffer, 128);
  184.  
  185. int * specSentences = malloc(sizeof(int));
  186. if(NULL == specSentences) {
  187. printf("Can't allocate memory");
  188. return 1;
  189. }
  190. int used = 0;
  191.  
  192. while(state){
  193. Console_setCursorAttribute(FG_DEFAULT);
  194. len = strlen(buffer);
  195.  
  196. if(len < 10) Console_setCursorAttribute(FG_RED);
  197. else if(len < 25) Console_setCursorAttribute(FG_YELLOW);
  198. else Console_setCursorAttribute(FG_GREEN);
  199.  
  200. if(len > 10){
  201. //BCDFGHJKLMNPQRSTVWXZ
  202. if(iscons(buffer[0])){
  203. specSentences = realloc(specSentences, sizeof(int) * (used + 1));
  204. if(NULL == specSentences) {
  205. printf("Can't reallocate memory");
  206. return 1;
  207. }
  208. specSentences[used] = i;
  209. used++;
  210. }
  211. }
  212.  
  213. printf("%s", buffer);
  214.  
  215. i++;
  216. state = GetSentence(i, text, buffer, 128);
  217. }
  218. Console_setCursorAttribute(FG_DEFAULT);
  219.  
  220. if(used) {
  221. printf("\n\nРечення у яких більше 10 символів, а перше слово починається на приголосну літеру:\n");
  222. for(int i = 0; i < used; i++){
  223. GetSentence(specSentences[i], text, buffer, 128);
  224. printf("[%d] - %s", specSentences[i], buffer);
  225. }
  226. }
  227. printf("\n");
  228.  
  229. free(specSentences);
  230. return 0;
  231. }
  232.  
  233. int ispunctend(char symb){
  234. if(symb == '.' || symb == '!' || symb == '?') return 1;
  235. return 0;
  236. }
  237. int iscons(char symb){
  238. char cons[] = "BCDFGHJKLMNPQRSTVWXZ";
  239. for(int i = 0; i < strlen(cons); i++){
  240. if(symb == cons[i]) return 1;
  241. }
  242. return 0;
  243. }
  244.  
  245. int FindSentence(char txt[], int sentenceToFind, int arraytofill[]){
  246. int sentencesPassed = 0,
  247. foundComplete = 0,
  248. j,
  249. indexHistory = 0;
  250.  
  251. for(int i = 0; i < strlen(txt) && !foundComplete; i++){
  252. if(ispunctend(txt[i])) {
  253. for(j = i + 1; j < strlen(txt); j++){
  254. if(!ispunctend(txt[j])) {
  255. j--;
  256. break;
  257. }
  258. }
  259. sentencesPassed++;
  260. foundComplete = (sentencesPassed == sentenceToFind) ? 1 : 0;
  261.  
  262. if(!foundComplete) indexHistory = j;
  263.  
  264. i = j;
  265. }
  266. }
  267.  
  268. if(foundComplete) {
  269. arraytofill[0] = (indexHistory != 0) ? (indexHistory + 1) : indexHistory;
  270. arraytofill[1] = j;
  271. }
  272.  
  273. return foundComplete;
  274. }
  275.  
  276. int GetSentence(int sentencetofind, char txt[], char buffer[], int sizeofbuffer) {
  277. int indexes[] = {0, 0};
  278. int state = FindSentence(txt, sentencetofind, indexes);
  279.  
  280. if(state) {
  281. for(int i = indexes[0], j = i - indexes[0]; i <= indexes[1] && j <= sizeofbuffer; i++, j = i - indexes[0]){
  282. buffer[j] = txt[i];
  283. if(j == sizeofbuffer) buffer[j] = '\0';
  284. else if(i == indexes[1]) buffer[j + 1] = '\0';
  285. }
  286. }
  287.  
  288. return state;
  289. }
  290.  
  291. struct Sentence CreateSentenceStruct(char txt[], int numsentence){
  292. int indexes[] = {0, 0};
  293. int state = FindSentence(txt, numsentence, indexes);
  294.  
  295. struct Sentence sentence = {-1, ""};
  296.  
  297. if(state) {
  298. char buffer[128];
  299. GetSentence(numsentence, txt, buffer, 70);
  300.  
  301. strcpy(sentence.sentenceBuffer, buffer);
  302. sentence.startIndex = indexes[0];
  303. }
  304.  
  305. return sentence;
  306. }
  307.  
  308. int task_1_1(char txt[]){
  309. int indexes[] = {0, 0};
  310. int state = FindSentence(txt, 2, indexes);
  311.  
  312. if(state){
  313. if(txt[indexes[0]] == ' ') return indexes[0] + 1;
  314. return indexes[0];
  315. }
  316. return -1;
  317. }
  318.  
  319. int fileExists(const char *fileName) {
  320. FILE *f = fopen(fileName, "rb");
  321. if (!f) return 0; // false: not exists
  322. fclose(f);
  323. return 1; // true: exists
  324. }
  325.  
  326. long getFileSize(const char *fileName) {
  327. FILE *f = fopen(fileName, "rb");
  328. if (!f) return -1; // error opening file
  329. fseek(f, 0, SEEK_END); // rewind cursor to the end of file
  330. long fsize = ftell(f); // get file size in bytes
  331. fclose(f);
  332. return fsize;
  333. }
  334.  
  335. int readFileToBuffer(const char *fileName, char *buffer, int bufferLength) {
  336. FILE *f = fopen(fileName, "rb");
  337. if (!f) return 0; // read 0 bytes from file
  338. long readBytes = fread(buffer, 1, bufferLength, f);
  339. fclose(f);
  340. return readBytes; // number of bytes read
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement