tsounakis

2.2 more stable3

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