Advertisement
tsounakis

Untitled

May 14th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.07 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, "\n%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. for (i=0;i<length;i++)
  225. {
  226. loaddict(dic, dicofwords, &length_of_dic);
  227. iterator = *dicofwords;
  228. if (length_of_dic != 0) while (iterator!=NULL)
  229. {
  230. if (strcmp(iterator->word, a[i]) == 0)
  231. {
  232. printf("the word %s is in the dictionary.\n", a[i]);
  233. iterator = iterator -> next;
  234.  
  235. break;
  236. }
  237. else if (iterator->next == NULL)
  238. {
  239. printf("The word %s is not in the dictionary. [Replace the word (press a), add to dic (press b), continue (press c), exit the correction mode (press e)]\n", a[i]);
  240. scanf(" %c", &decision);
  241. switch (decision)
  242. {
  243. case 'a':
  244. replace(a[i], local_old[i]);
  245. add_to_dic(dic, a[i]);
  246. break;
  247. case 'b':
  248. add_to_dic(dic, a[i]);
  249. break;
  250. case 'c':
  251. break;
  252. case 'e':
  253. printf("(CORRECTION MODE [OFF]):~ ...\n");
  254. return;
  255. default:
  256. break;
  257. }
  258. }
  259. iterator = iterator -> next;
  260. }
  261. else
  262. {
  263. 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]);
  264. }
  265. }
  266. /*
  267. 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]);
  268. scanf(" %c", &decision);
  269. switch (decision)
  270. {
  271. case 'a':
  272. replace(a[i], local_old[i]);
  273. add_to_dic(dic, a[i]);
  274. break;
  275. case 'b':
  276. add_to_dic(dic, a[i]);
  277. break;
  278. case 'c':
  279. break;
  280. case 'e':
  281. printf("(CORRECTION MODE [OFF]):~ ...\n");
  282. return;
  283. default:
  284. break;
  285. }
  286. }
  287. }
  288. }*/
  289. input = fopen("input.txt", "w");
  290. for (i=0; i<length; i++)
  291. {
  292. if (i==length-1)
  293. {
  294. fprintf(input, "%s", local_old[i]);
  295. }
  296. else
  297. {
  298. fprintf(input, "%s ", local_old[i]);
  299. }
  300. }
  301. printf("(CORRECTION MODE [OFF]):~ ...\n");
  302. fclose(input);
  303. return;
  304. }
  305.  
  306. void replace(char a[N], char old_a[N])
  307. {
  308. char rep_word[N], c;
  309. printf("Type the word you want to replace: ");
  310. scanf("%s", rep_word);
  311. strcpy(a, strlwr(rep_word));
  312. if (old_a[0] >= 'A' && old_a[0]<= 'Z')
  313. {
  314. rep_word[0] = toupper(rep_word[0]);
  315. }
  316. if (old_a[strlen(old_a) - 1] == '.' || (old_a[strlen(old_a) - 1] == '?') || (old_a[strlen(old_a) - 1] == '!'))
  317. {
  318. c = old_a[strlen(old_a) - 1];
  319. rep_word[strlen(rep_word)] = c;
  320. rep_word[strlen(rep_word) + 1] = '\0';
  321. }
  322. strcpy(old_a, rep_word);
  323. return;
  324. }
  325.  
  326. void cleanString(char str[N])
  327. {
  328. int i, j;
  329. for(i=0; str[i]!='\0'; ++i)
  330. {
  331. while (!((str[i]>='a'&&str[i]<='z') || ((str[i]>='A' && str[i]<='Z') || str[i]=='\0') || (str[i]>='0' && str[i]<='9') || (str[i])=='\''))
  332. {
  333. for(j=i;str[j]!='\0';++j)
  334. {
  335. str[j]=str[j+1];
  336. }
  337. str[j]='\0';
  338. }
  339. }
  340. return;
  341. }
  342.  
  343. void histogram(char a[M][N], int lengthofwords[], int length)
  344. {
  345. int i, j, lengthWord;
  346. for (i=0;i<N;i++)
  347. {
  348. j = 0;
  349. while (j!=length)
  350. {
  351. lengthWord = strlen(a[j]);
  352. if (lengthWord == i)
  353. {
  354. lengthofwords[i]++;
  355. }
  356. j++;
  357. }
  358. }
  359. return;
  360. }
  361.  
  362. void stats(char a[M][N], int length, int spaces)
  363. {
  364. char choice, old[M][N];
  365. /*char** old = (char**)malloc(length*N*sizeof(char));*/
  366. int lengthWords, lengthChars = 0, i, j, stop = 0, diff = 0, lengthofwords[N] = {0};
  367. for (i=0; i<length; i++)
  368. {
  369. cleanString(a[i]);
  370. }
  371. for (i=0; i<length; i++)
  372. {
  373. strcpy(old[i], a[i]);
  374. strupr(a[i]);
  375. }
  376. printf("(STATS MODE[ON]):~ *INITIATING STATS MODE*\n");
  377. if (strlen(a[0]) == 0)
  378. {
  379. printf("NON READABLE TEXT.\n");
  380. return;
  381. }
  382. for (i=0; i < length; i++)
  383. {
  384. for (j=0; a[i][j] != '\0'; j++)
  385. {
  386. lengthChars++;
  387. }
  388. }
  389. lengthWords = i;
  390. histogram(a, lengthofwords, length);
  391. for (i=0; i < length; i++)
  392. {
  393. for (j=i; j<length;j++)
  394. {
  395. if (strcmp(a[i], "*0*0*0*") != 0)
  396. {
  397. if (strcmp(a[i], a[j]) == 0 && i != j)
  398. {
  399. strcpy(a[j], "*0*0*0*");
  400. }
  401. if (j == length - 1)
  402. {
  403. diff++;
  404. }
  405. }
  406. }
  407. }
  408. while (stop == 0)
  409. {
  410. 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]):~ ");
  411. scanf(" %c", &choice);
  412. putchar('\n');
  413. switch(choice){
  414. case 'a':
  415. printf("(STATS MODE[ON]):~ The text is composed of %d words and %d characters (%d including the spaces).\n", lengthWords, lengthChars, spaces + lengthChars);
  416. break;
  417. case 'b':
  418. printf("(STATS MODE[ON]):~ The text is composed of %d different words.\n", diff);
  419. break;
  420. case 'c':/* USEFUL GARBAGE
  421. printf("Word:\tOccurence:\t\n");
  422. for (i=0; i<length; i++)
  423. {
  424. for (j=0; j<length; j++)
  425. {
  426. if (i==occA[j] && occA[j] != 0)
  427. {
  428. printf("%s\t:\t", da[j]);
  429. for (k=0; k<occA[j];k++)
  430. {
  431. printf("*");
  432. }
  433. putchar('\n');
  434. }
  435. }
  436. }*/
  437. printf("(STATS MODE[ON]):~ ");
  438. printf("\n# of chars\t# of words:");
  439. printf("\nin a word:\n\n");
  440. for (i=1;i<N;i++)
  441. {
  442. printf("%d (occ.: %d)\t\t", i, lengthofwords[i]);
  443. for (j=0; j<lengthofwords[i];j++)
  444. {
  445. putchar('*');
  446. }
  447. putchar('\n');
  448. }
  449. break;
  450. case 'f':
  451. save_statistics(diff, lengthWords, lengthChars, spaces, lengthofwords);
  452. break;
  453. case 'e':
  454. stop = 1;
  455. default:
  456. break;
  457. }
  458. }
  459. printf("(STATS MODE[OFF]):~ ...\n");
  460. return;
  461. }
  462.  
  463. void save_statistics(int diff, int lengthWords, int lengthChars, int spaces, int lengthofwords[])
  464. {
  465. FILE* savefile;
  466. int i, j;
  467. char save_file_name[N];
  468. printf("(STATS MODE[ON])/SAVE OPTION:~ SAVE STATISTICS FILE AS: ");
  469. scanf("%s", save_file_name);
  470. for (i=0;save_file_name[i] != '\0';i++);
  471. strcat(save_file_name, ".txt");
  472. printf("(STATS MODE[ON])/SAVE OPTION:~ \"%s\" CREATED SUCCESSFULLY\n", save_file_name);
  473.  
  474. savefile = fopen(save_file_name, "w+");
  475.  
  476. fprintf(savefile, "The text is composed of %d words and %d characters (%d including the spaces).\n\n", lengthWords, lengthChars, spaces + lengthChars);
  477. fprintf(savefile, "The text is composed of %d different words.\n\n", diff);
  478. fprintf(savefile, "\n# of chars\t# of words:");
  479. fprintf(savefile, "\nin a word:\n\n");
  480. for (i=1;i<N;i++)
  481. {
  482. fprintf(savefile, "%d\t\t", i);
  483. for (j=0; j<lengthofwords[i];j++)
  484. {
  485. fprintf(savefile, "*");
  486. }
  487. fprintf(savefile, "\n");
  488. }
  489.  
  490. printf("(STATS MODE[ON])/SAVE OPTION:~ \"%s\" SAVED SUCCESSFULLY\n", save_file_name);
  491. printf("(STATS MODE[ON])/SAVE OPTION:~ EXITING SAVE OPTION\n");
  492. return;
  493. }
  494.  
  495. void legend()
  496. {
  497. 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\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|");
  498. return;
  499. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement