Advertisement
tsounakis

2.2 stable 2

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