tsounakis

Untitled

May 14th, 2020
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #define N 17
  6. #define M 30000
  7.  
  8. char DEVELOPMENT[] = "OFF";
  9.  
  10. struct List
  11. {
  12. char* word;
  13. struct List* next;
  14. };
  15.  
  16. typedef struct List list;
  17. typedef list* list_ptr;
  18.  
  19. list * createnode(char* ch);
  20. void replace(char a[N], char old_a[N]);
  21. void add_to_dic(FILE* dic, char word[N]);
  22. void correct(int length, int length_of_dic, char a[M][N], list_ptr*, FILE*, FILE*);
  23. void save_statistics(int diff, int legnthWords, int lengthChars, int spaces, int lengthofwords[]);
  24. void histogram(char a[M][N], int lengthofwords[], int length);
  25. char getchoice();
  26. void cleanString(char []);
  27. void read(char a[M][N], int*, int*, FILE*, char raw_a[M][N]);
  28. void loaddict(FILE*, list_ptr*, int*);
  29. void stats(char a[M][N], int, int);
  30. void legend();
  31.  
  32. int main() {
  33. FILE* input;
  34. list_ptr dicofwords;
  35. FILE* dic;
  36. char choice, text[M][N], raw_text[M][N];
  37. int length = 0, spaces, length_of_dic;
  38. int i;
  39. int* ptr_length;
  40. int* ptr_spaces;
  41. int* ptr_lod;
  42.  
  43. input = NULL;
  44. dic = NULL;
  45. dicofwords = NULL;
  46.  
  47. ptr_lod = &length_of_dic;
  48. ptr_spaces = &spaces;
  49. ptr_length = &length;
  50.  
  51. printf("The infamous 'I lost three weeks for a 7' word and text editor v2_5b\nPlease follow the on-screen instructions.\n\n");
  52. legend();
  53.  
  54. while(1)
  55. {
  56. putchar('\n');
  57. printf("CHOOSE AN ACTION: ");
  58. choice = getchoice();
  59. printf("---------------------------------------------------\n");
  60. switch (choice)
  61. {
  62. case 'r':
  63. read(text, ptr_length, ptr_spaces, input, raw_text);
  64. break;
  65. case 'd':
  66. printf("(LOAD DIC MODE[ON]):~ INITIATING LOAD DICTIONARY MODE\n");
  67. loaddict(dic, &dicofwords, ptr_lod);
  68. printf("(LOAD DIC MODE[ON]):~ ALL WORDS SUCCESSFULLY LOADED\n");
  69. printf("(LOAD DIC MODE[OFF]):~ ...\n");
  70. break;
  71. case 'c':
  72. correct(length, length_of_dic, raw_text, &dicofwords, dic, input);
  73. break;
  74. case 's':
  75. stats(text, length, spaces);
  76. break;
  77. case 'l':
  78. legend();
  79. break;
  80. case 'e':
  81. return 0;
  82. break;
  83. case 'z':
  84. for (i=0;i<length && strcpy(DEVELOPMENT,"on") == 0;i++)
  85. {
  86. printf("%d = %s\n",i ,text[i]);
  87. }
  88. for (i=0;i<*ptr_lod && strcpy(DEVELOPMENT, "on") == 0;i++)
  89. {
  90. /*printf("%s => added\n", dictionary[i]);
  91. */}
  92. default:
  93. printf("\n*Wrong input*\n");
  94. break;
  95. }
  96. printf("---------------------------------------------------\n");
  97. }
  98. }
  99. char getchoice()
  100. {
  101. char choice;
  102. scanf(" %c", &choice);
  103. return choice;
  104. }
  105.  
  106. void read(char a[M][N], int* ptr_length, int* ptr_spaces, FILE* input, char raw_a[M][N])
  107. {
  108. char ch;
  109. int i, length, spaces = 0;
  110. *ptr_spaces = 0;
  111. input = fopen("input.txt", "r");
  112. printf("(READ TEXT MODE[ON]):~ INITIATING READ TEXT MODE\n");
  113. /*Search for spaces*/
  114. while ((ch = fgetc(input)) != EOF)
  115. {
  116. if (ch == ' ')
  117. {
  118. spaces++;
  119. }
  120. }
  121. *ptr_spaces = spaces;
  122. fseek(input, 0, SEEK_SET);
  123. /**/
  124. for (i=0; feof(input) == 0; i++)
  125. {
  126. fscanf(input, "%s", a[i]);
  127. if (ferror(input) != 0)
  128. {
  129. printf("ERROR. EXITING READ MODE");
  130. return;
  131. }
  132. }
  133. length = i;
  134. if (strcpy(DEVELOPMENT, "on")==0)
  135. printf("the length is %d", length);
  136. else
  137. printf("(READ TEXT MODE[ON]):~ LOADING TEXT...\n");
  138. if (strcmp(DEVELOPMENT, "ON") == 0)
  139. {
  140. for (i=0; i<length; i++)
  141. {
  142. printf("%s\n", a[i]);
  143. }
  144. }
  145. printf("(READ TEXT MODE[OFF]...):~ SUCCESSFULLY READ THE TEXT\n");
  146. *ptr_length = length;
  147. for (i=0; i<length; i++)
  148. {
  149. strcpy(raw_a[i], a[i]);
  150. }
  151. fclose(input);
  152. return;
  153. }
  154.  
  155. list * createnode(char* ch) {
  156. list_ptr newnode_ptr ;
  157. newnode_ptr = malloc(sizeof (list));
  158. newnode_ptr -> word = malloc(sizeof(strlen(ch)+1)*sizeof(char));
  159. strcpy(newnode_ptr->word, ch);
  160. newnode_ptr -> next = NULL;
  161. return newnode_ptr;
  162. }
  163.  
  164. void loaddict(FILE* dic, list_ptr* words, int* ptr)
  165. {
  166. int length = 0;
  167. int num = 0;
  168. char current_word[M+1] = "";
  169. list_ptr iterator;
  170. list_ptr newnode;
  171.  
  172. fseek(dic, 0, SEEK_SET);
  173. iterator = *words;
  174. dic = fopen("dic.txt", "r");
  175.  
  176. if (ferror(dic) != 0)
  177. {
  178. printf("(LOAD DIC MODE[ON]):~ ERROR! NO DICTIONARY FOUND. [ABSENCE OF .txt DIC FILE.]\n(LOAD DIC MODE[OFF]):~ ...\n");
  179. }
  180. fscanf(dic, "%s", current_word);
  181. newnode = createnode(current_word);
  182. iterator = newnode;
  183. *words = newnode;
  184.  
  185. num++;
  186.  
  187. while ((fscanf(dic, "%s", current_word)) != EOF) {
  188. num++;
  189. length += strlen(current_word);
  190.  
  191. newnode = createnode(current_word);
  192. iterator->next = newnode;
  193. iterator = iterator->next;
  194. }
  195. *ptr = num;
  196. fclose(dic);
  197. return;
  198. }
  199.  
  200. void add_to_dic(FILE* dic, char word[N])
  201. {
  202. dic = fopen("dic.txt", "r+");
  203. fseek(dic, 0, SEEK_END);
  204. fprintf(dic, "%s\n", word);
  205. fclose(dic);
  206. return;
  207. }
  208.  
  209. void correct(int length, int length_of_dic, char a[M][N], list_ptr *dicofwords, FILE* dic, FILE* input)
  210. {
  211. int i;
  212. list_ptr iterator = *dicofwords;
  213. char local_old[M][N];
  214. char decision;
  215.  
  216. for (i=0; i<length;i++)
  217. {
  218. strcpy(local_old[i], a[i]);
  219. cleanString(a[i]);
  220. strlwr(a[i]);
  221.  
  222. }
  223. printf("(CORRECTION MODE [ON]):~ INITIATING CORRECTION MODE\n");
  224. printf("length of dic is %d\nlength of text is %d and first thing in dic is %s\n", length_of_dic, length, iterator->word);
  225. for (i=0;i<length;i++)
  226. {
  227. loaddict(dic, dicofwords, &length_of_dic);
  228. iterator = *dicofwords;
  229. if (length_of_dic != 0) while (iterator!=NULL)
  230. {
  231. if (strcmp(iterator->word, a[i]) == 0)
  232. {
  233. printf("the word %s is in the dictionary.\n", a[i]);
  234. iterator = iterator -> next;
  235.  
  236. break;
  237. }
  238. else if (iterator->next == NULL)
  239. {
  240. printf("The word %s is not in the dictionary.\n", a[i]);
  241. scanf(" %c", &decision);
  242. switch (decision)
  243. {
  244. case 'a':
  245. replace(a[i], local_old[i]);
  246. add_to_dic(dic, a[i]);
  247. break;
  248. case 'b':
  249. add_to_dic(dic, a[i]);
  250. break;
  251. case 'c':
  252. break;
  253. case 'e':
  254. printf("(CORRECTION MODE [OFF]):~ ...\n");
  255. return;
  256. default:
  257. break;
  258. }
  259. }
  260. iterator = iterator -> next;
  261. }
  262. else
  263. {
  264. printf("The word %s is not in the dictionary. Please decide what to do \n[Replace the word (press a), add to dic (press b), continue (press c), exit the correction mode (press e)]: \n\n", a[i]);
  265. }
  266. }
  267. /*
  268. printf("\t\tWord \"%s\" is not included in the dictionary.\nPlease decide what to do to [Replace the word (press a), add to dic (press b), continue (press c), exit the correction mode (press e)]\n", a[i]);
  269. scanf(" %c", &decision);
  270. switch (decision)
  271. {
  272. case 'a':
  273. replace(a[i], local_old[i]);
  274. add_to_dic(dic, a[i]);
  275. break;
  276. case 'b':
  277. add_to_dic(dic, a[i]);
  278. break;
  279. case 'c':
  280. break;
  281. case 'e':
  282. printf("(CORRECTION MODE [OFF]):~ ...\n");
  283. return;
  284. default:
  285. break;
  286. }
  287. }
  288. }
  289. }*/
  290. input = fopen("input.txt", "w");
  291. for (i=0; i<length; i++)
  292. {
  293. if (i==length-1)
  294. {
  295. fprintf(input, "%s", local_old[i]);
  296. }
  297. else
  298. {
  299. fprintf(input, "%s ", local_old[i]);
  300. }
  301. }
  302. printf("(CORRECTION MODE [OFF]):~ ...\n");
  303. fclose(input);
  304. return;
  305. }
  306.  
  307. void replace(char a[N], char old_a[N])
  308. {
  309. char rep_word[N], c;
  310. printf("Type the word you want to replace: ");
  311. scanf("%s", rep_word);
  312. strcpy(a, strlwr(rep_word));
  313. if (old_a[0] >= 'A' && old_a[0]<= 'Z')
  314. {
  315. rep_word[0] = toupper(rep_word[0]);
  316. }
  317. if (old_a[strlen(old_a) - 1] == '.' || (old_a[strlen(old_a) - 1] == '?') || (old_a[strlen(old_a) - 1] == '!'))
  318. {
  319. c = old_a[strlen(old_a) - 1];
  320. rep_word[strlen(rep_word)] = c;
  321. rep_word[strlen(rep_word) + 1] = '\0';
  322. }
  323. strcpy(old_a, rep_word);
  324. return;
  325. }
  326.  
  327. void cleanString(char str[N])
  328. {
  329. int i, j;
  330. for(i=0; str[i]!='\0'; ++i)
  331. {
  332. while (!((str[i]>='a'&&str[i]<='z') || ((str[i]>='A' && str[i]<='Z') || str[i]=='\0') || (str[i]>='0' && str[i]<='9') || (str[i])=='\''))
  333. {
  334. for(j=i;str[j]!='\0';++j)
  335. {
  336. str[j]=str[j+1];
  337. }
  338. str[j]='\0';
  339. }
  340. }
  341. return;
  342. }
  343.  
  344. void histogram(char a[M][N], int lengthofwords[], int length)
  345. {
  346. int i, j, lengthWord;
  347. for (i=0;i<N;i++)
  348. {
  349. j = 0;
  350. while (j!=length)
  351. {
  352. lengthWord = strlen(a[j]);
  353. if (lengthWord == i)
  354. {
  355. lengthofwords[i]++;
  356. }
  357. j++;
  358. }
  359. }
  360. return;
  361. }
  362.  
  363. void stats(char a[M][N], int length, int spaces)
  364. {
  365. char choice, old[M][N];
  366. /*char** old = (char**)malloc(length*N*sizeof(char));*/
  367. int lengthWords, lengthChars = 0, i, j, stop = 0, diff = 0, lengthofwords[N] = {0};
  368. for (i=0; i<length; i++)
  369. {
  370. cleanString(a[i]);
  371. }
  372. for (i=0; i<length; i++)
  373. {
  374. strcpy(old[i], a[i]);
  375. strupr(a[i]);
  376. }
  377. printf("(STATS MODE[ON]):~ *INITIATING STATS MODE*\n");
  378. if (strlen(a[0]) == 0)
  379. {
  380. printf("NON READABLE TEXT.\n");
  381. return;
  382. }
  383. for (i=0; i < length; i++)
  384. {
  385. for (j=0; a[i][j] != '\0'; j++)
  386. {
  387. lengthChars++;
  388. }
  389. }
  390. lengthWords = i;
  391. histogram(a, lengthofwords, length);
  392. for (i=0; i < length; i++)
  393. {
  394. for (j=i; j<length;j++)
  395. {
  396. if (strcmp(a[i], "*0*0*0*") != 0)
  397. {
  398. if (strcmp(a[i], a[j]) == 0 && i != j)
  399. {
  400. strcpy(a[j], "*0*0*0*");
  401. }
  402. if (j == length - 1)
  403. {
  404. diff++;
  405. }
  406. }
  407. }
  408. }
  409. while (stop == 0)
  410. {
  411. printf("(STATS MODE[ON]):~ CHOOSE:\n\t\tWORD COUNTER [PRESS a]\n\t\tDIFFERENT WORDS COUNTER [PRESS b]\n\t\tCHARACTERS x WORDS HISTOGRAM [PRESS c]\n\t\tSAVE STATS AS A .txt FILE [PRESS f]\n(STATS MODE[ON]):~ ");
  412. scanf(" %c", &choice);
  413. putchar('\n');
  414. switch(choice){
  415. case 'a':
  416. printf("(STATS MODE[ON]):~ The text is composed of %d words and %d characters (%d including the spaces).\n", lengthWords, lengthChars, spaces + lengthChars);
  417. break;
  418. case 'b':
  419. printf("(STATS MODE[ON]):~ The text is composed of %d different words.\n", diff);
  420. break;
  421. case 'c':/* USEFUL GARBAGE
  422. printf("Word:\tOccurence:\t\n");
  423. for (i=0; i<length; i++)
  424. {
  425. for (j=0; j<length; j++)
  426. {
  427. if (i==occA[j] && occA[j] != 0)
  428. {
  429. printf("%s\t:\t", da[j]);
  430. for (k=0; k<occA[j];k++)
  431. {
  432. printf("*");
  433. }
  434. putchar('\n');
  435. }
  436. }
  437. }*/
  438. printf("(STATS MODE[ON]):~ ");
  439. printf("\n# of chars\t# of words:");
  440. printf("\nin a word:\n\n");
  441. for (i=1;i<N;i++)
  442. {
  443. printf("%d (occ.: %d)\t\t", i, lengthofwords[i]);
  444. for (j=0; j<lengthofwords[i];j++)
  445. {
  446. putchar('*');
  447. }
  448. putchar('\n');
  449. }
  450. break;
  451. case 'f':
  452. save_statistics(diff, lengthWords, lengthChars, spaces, lengthofwords);
  453. break;
  454. case 'e':
  455. stop = 1;
  456. default:
  457. break;
  458. }
  459. }
  460. printf("(STATS MODE[OFF]):~ ...\n");
  461. return;
  462. }
  463.  
  464. void save_statistics(int diff, int lengthWords, int lengthChars, int spaces, int lengthofwords[])
  465. {
  466. FILE* savefile;
  467. int i, j;
  468. char save_file_name[N];
  469. printf("(STATS MODE[ON])/SAVE OPTION:~ SAVE STATISTICS FILE AS: ");
  470. scanf("%s", save_file_name);
  471. for (i=0;save_file_name[i] != '\0';i++);
  472. strcat(save_file_name, ".txt");
  473. printf("(STATS MODE[ON])/SAVE OPTION:~ \"%s\" CREATED SUCCESSFULLY\n", save_file_name);
  474.  
  475. savefile = fopen(save_file_name, "w+");
  476.  
  477. fprintf(savefile, "The text is composed of %d words and %d characters (%d including the spaces).\n\n", lengthWords, lengthChars, spaces + lengthChars);
  478. fprintf(savefile, "The text is composed of %d different words.\n\n", diff);
  479. fprintf(savefile, "\n# of chars\t# of words:");
  480. fprintf(savefile, "\nin a word:\n\n");
  481. for (i=1;i<N;i++)
  482. {
  483. fprintf(savefile, "%d\t\t", i);
  484. for (j=0; j<lengthofwords[i];j++)
  485. {
  486. fprintf(savefile, "*");
  487. }
  488. fprintf(savefile, "\n");
  489. }
  490.  
  491. printf("(STATS MODE[ON])/SAVE OPTION:~ \"%s\" SAVED SUCCESSFULLY\n", save_file_name);
  492. printf("(STATS MODE[ON])/SAVE OPTION:~ EXITING SAVE OPTION\n");
  493. return;
  494. }
  495.  
  496. void legend()
  497. {
  498. printf("/////////////////////////////////|\nLEGEND:\t\t\t\t||\n[r:\tInsert text]\t\t||\n[d:\tLoad dictionary]\t||\n[c:\tCorrect the loaded text]||\n[s:\tStatistics]\t\t||\n[l:\tShow legend]\t\t||\n[e:\tEscape]\t\t\t||\n\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|");
  499. return;
  500. }
Advertisement
Add Comment
Please, Sign In to add comment