Guest User

Untitled

a guest
May 1st, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define ASCII_TO_ALPHA(x) x - 65
  6. #define ALPHA_TO_ASCII(x) x + 65
  7.  
  8. int main(int argc, char *argv[]){
  9.  
  10.     FILE *txtFile = fopen(argv[1], "r"); // open file that was dragged into exe
  11.  
  12.     int alphabetCount[26] = { 0 };
  13.     char buffer;
  14.  
  15.     if (txtFile){
  16.         while ( (buffer = toupper(fgetc(txtFile)) ) != EOF){
  17.             if (buffer >= 'A' && buffer <= 'Z'){
  18.                 alphabetCount[ASCII_TO_ALPHA(buffer)]++;
  19.             }
  20.         }
  21.     }
  22.  
  23.     gnu_writeAsHistogram(&alphabetCount);
  24.  
  25.     fclose(txtFile);
  26.  
  27.     return 0;
  28. }
  29.  
  30. void gnu_writeAsHistogram(int *alphabetCount){
  31.     int som = 0;
  32.     float alphabetPerc[26] = { 0 };
  33.  
  34.     for (int i = 0; i < 26; i++)
  35.         som += alphabetCount[i];
  36.  
  37.     for (int i = 0; i < 26; i++)
  38.         alphabetPerc[i] = (float)alphabetCount[i]/som * 100;
  39.  
  40.     FILE *gnuplotPipe = popen("C:\\Users\\Zain\\Documents\\Github\\GNUCPlotter\\gnuplot\\bin\\gnuplot.exe -persistent", "w");
  41.     fprintf(gnuplotPipe,
  42.         "set multiplot\n"
  43.         "set xrange [1:26]\n"
  44.         "set autoscale\n"
  45.         "show autoscale\n"
  46.         "set grid\n"
  47.         "set style data histogram\n"
  48.         "set style histogram cluster gap 1\n"
  49.         "set style fill solid border -1\n"
  50.         "set boxwidth 0.9\n"
  51.         "set xtic('A' 1, 'B' 2, 'C' 3, 'D' 4, 'E' 5, 'F' 6, 'G' 7, 'H' 8, 'I' 9, 'J' 10, 'K' 11, 'L' 12, 'M' 13, 'N' 14, 'O' 15, 'P' 16, 'Q' 17, 'R' 18, 'S' 19, 'T' 20, 'U' 21, 'V' 22, 'W' 23, 'X' 24, 'Y' 25, 'Z' 26)\n"
  52.         "set xtic rotate by -45 scale 0\n");
  53.  
  54.     fprintf(gnuplotPipe, "$myData << EOD\n");
  55.  
  56.     for (int i = 0; i < 26; i++){
  57.         fprintf(gnuplotPipe, "%d %.2f\n", i+1, alphabetPerc[i]);
  58.     }
  59.  
  60.     fprintf(gnuplotPipe, "EOD\n");
  61.     fprintf(gnuplotPipe, "stats $myData u 1:2\n");
  62.     fprintf(gnuplotPipe, "plot $myData u 1:2 with boxes notitle, $myData u 1:2:2 with labels offset char 0,1\n");
  63.  
  64.     fclose(gnuplotPipe);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment