tsounakis

2.2 stable

Apr 22nd, 2020
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define N 30
  5. #define M 100
  6.  
  7. char DEVELOPMENT[] = "OFF";
  8.  
  9. char getchoice();
  10. void cleanString(char []);
  11. void read(char a[M][N], int*, int*);
  12. void loaddict();
  13. void correct();
  14. void save();
  15. void stats(char a[M][N], int, int);
  16. void legend();
  17.  
  18. int main() {
  19.     char choice, text[M][N];
  20.     int length = 0, spaces;
  21.     int* ptr_length;
  22.     int* ptr_spaces;
  23.     ptr_spaces = &spaces;
  24.     ptr_length = &length;
  25.     if (strcmp(DEVELOPMENT, "ON") == 0)
  26.     {
  27.         printf("DEVELOPMENT MODE[ON]\n");
  28.     }
  29.     legend();
  30.     while(1)
  31.     {      
  32.         printf("CHOOSE AN ACTION: ");
  33.         choice = getchoice();
  34.         switch (choice)
  35.         {
  36.             case 'r':
  37.                 read(text, ptr_length, ptr_spaces);
  38.                 break;
  39.             case 'd':
  40.                 loaddict();
  41.                 break;
  42.             case 'c':
  43.                 correct();
  44.                 break;
  45.             case 's':
  46.                 save();
  47.                 break;
  48.             case 'S':
  49.                 stats(text, length, spaces);
  50.                 break;
  51.             case 'l':
  52.                 legend();
  53.                 break;
  54.             case 'e':
  55.                 return 0;
  56.                 break;
  57.             default:
  58.                 printf("\n*Wrong input*\n");
  59.                 break;
  60.         }
  61.     }
  62. }
  63. char getchoice()
  64. {
  65.     char choice;
  66.     scanf(" %c", &choice);
  67.     return choice;
  68. }
  69.  
  70. void read(char a[M][N], int* ptr_length, int* ptr_spaces)
  71. {
  72.     FILE *input;
  73.     char ch;
  74.     int i, length, spaces = 0;
  75.     *ptr_spaces = 0;
  76.     input = fopen("input.txt", "r+");
  77.     printf("(READ TEXT MODE[ON]):~ INITIATING READ TEXT MODE\n");
  78.     /*Search for spaces*/
  79.     while ((ch = fgetc(input)) != EOF)
  80.     {
  81.         if (ch == ' ')
  82.         {
  83.             spaces++;
  84.         }
  85.     }
  86.     *ptr_spaces = spaces;
  87.     fseek(input, 0, SEEK_SET);
  88.     /**/
  89.     for (i=0; feof(input) == 0; i++)
  90.     {
  91.         fscanf(input, "%s", a[i]);
  92.         if (ferror(input) != 0)
  93.         {
  94.             printf("ERROR. EXITING READ MODE");
  95.             return;
  96.         }
  97.     }
  98.     length = i;
  99.     if (strcmp(DEVELOPMENT, "ON") == 0)
  100.     {
  101.         for (i=0; i<length; i++)
  102.         {
  103.             printf("%s\n", a[i]);
  104.         }
  105.     }  
  106.     printf("(READ TEXT MODE[OFF]...):~ SUCCESSFULLY READ THE TEXT\n");
  107.     *ptr_length = length;
  108.     return;
  109. }
  110.  
  111. void loaddict()
  112. {
  113.     printf("\nDummy function.\n");
  114.     return;
  115. }
  116.  
  117. void correct()
  118. {
  119.     printf("\nText corrected.\n\n");
  120.     return;
  121. }
  122.  
  123. void save()
  124. {
  125.     printf("\nText saved.\n\n");
  126.     return;
  127. }
  128.  
  129. void cleanString(char str[N])
  130. {
  131.     int i, j;
  132.     for(i=0; str[i]!='\0'; ++i)
  133.     {
  134.         while (!((str[i]>='a'&&str[i]<='z') || ((str[i]>='A' && str[i]<='Z') || str[i]=='\0') || (str[i])=='\''))
  135.         {
  136.             for(j=i;str[j]!='\0';++j)
  137.             {
  138.                 str[j]=str[j+1];
  139.             }
  140.             str[j]='\0';
  141.         }
  142.     }
  143.     return;
  144. }
  145.  
  146. void stats(char a[M][N], int length, int spaces)
  147. {
  148.     char choice, da[M][N], old[M][N];
  149.     int lengthWords, lengthChars = 0, i, j, stop = 0, diff = 0, occ, occA[M] = {0}, k;
  150.     for (i=0; i<length; i++)
  151.     {
  152.         cleanString(a[i]);
  153.     }
  154.     for (i=0; i<length; i++)
  155.     {
  156.         strcpy(old[i], a[i]);
  157.         strupr(a[i]);
  158.     }
  159.     printf("(STATS MODE[ON]):~ *INITIATING STATS MODE*\n");
  160.     if (strlen(a[0]) == 0)
  161.     {
  162.         printf("NON READABLE TEXT.\n");
  163.         return;
  164.     }
  165.     for (i=0; i < length; i++)
  166.     {
  167.         for (j=0; old[i][j] != '\0'; j++)
  168.         {
  169.             lengthChars++;
  170.         }
  171.     }
  172.     lengthWords = i;
  173.     for (i=0; i < length; i++)
  174.     {
  175.         occ = 1;
  176.         for (j=i; j<length;j++)
  177.         {
  178.             if (strcmp(a[i], "*0*0*0*") != 0)
  179.             {
  180.                 if (strcmp(a[i], a[j]) == 0 && i != j)
  181.                 {
  182.                     occ++;
  183.                     strcpy(a[j], "*0*0*0*");
  184.                 }
  185.                 if (j == length - 1)
  186.                 {
  187.                     strcpy(da[diff], old[i]);
  188.                     occA[diff] = occ;
  189.                     diff++;
  190.                 }
  191.             }  
  192.         }
  193.     }
  194.     while (stop == 0)
  195.     {
  196.         printf("\nCHOOSE [a, b or c]:");
  197.         scanf(" %c", &choice);
  198.         switch(choice){
  199.             case 'a':
  200.                 printf("The text is composed of %d words and %d characters (%d including the spaces).\n", lengthWords, lengthChars, spaces + lengthChars);
  201.                 break;
  202.             case 'b':
  203.                 printf("The text is composed of %d different words.\n", diff);
  204.                 break;
  205.             case 'c':
  206.                 printf("Word:\tOccurence:\t\n");
  207.                 for (i=0; i<length; i++)
  208.                 {
  209.                     for (j=0; j<length; j++)
  210.                     {
  211.                         if (i==occA[j] && occA[j] != 0)
  212.                         {
  213.                             printf("%s\t:\t", da[j]);
  214.                             for (k=0; k<occA[j];k++)
  215.                             {
  216.                                 printf("*");
  217.                             }
  218.                             putchar('\n');
  219.                         }
  220.                     }
  221.                 }
  222.                 break;
  223.             case 'e':
  224.                 stop = 1;
  225.             default:
  226.                 break;
  227.         }
  228.     }  
  229.     printf("(STATS MODE[OFF]):~ ...\n");
  230.     return;
  231. }
  232.  
  233. void legend()
  234. {
  235.     printf("LEGEND:\n[r:\tInsert text]\n[d:\tLoad dictionary]\n[c:\tCorrect the loaded text]\n[S:\tStatistics]\n[s:\tSave text]\n[l:\tShow legend]\n[e:\tEscape]\n");
  236.     return;
  237. }
Advertisement
Add Comment
Please, Sign In to add comment