fork download
  1. // cs_161_007.c
  2. // http://t...content-available-to-author-only...h.net/test/read.cgi/tech/1354070278/7
  3.  
  4. #include<stdio.h>
  5. #include<stdlib.h>
  6.  
  7. #define INPUT_FILE "cs_161_007.c"
  8. #define RANGE (66) // length of scale
  9.  
  10. #if RANGE < 1
  11. #error RANGE < 1
  12. #endif
  13.  
  14. int main()
  15. {
  16. FILE *fp;
  17. int i, j;
  18. int counter[26];
  19. int ch;
  20. int sum;
  21. int max_ch_count;
  22. int max_ch_keta;
  23. int hist;
  24.  
  25. // init
  26. for (i = 0; i < 26; i++) {
  27. counter[i] = 0;
  28. }
  29. sum = 0;
  30.  
  31. // read & count
  32. if (NULL == (fp = fopen(INPUT_FILE, "r"))) {
  33. fprintf(stderr, "File \"" INPUT_FILE "\" open error.\n");
  34. exit(1);
  35. }
  36. while (1) {
  37. if (EOF == (ch = fgetc(fp))) {
  38. break;
  39. }
  40. if ('a' <= ch && ch <= 'z') {
  41. counter[ch - 'a']++;
  42. sum++;
  43. }
  44. }
  45. fclose(fp);
  46.  
  47. // make max_ch_count & max_ch_keta
  48. for (i = max_ch_count = 0; i < 26; i++) {
  49. if (max_ch_count < counter[i]) {
  50. max_ch_count = counter[i];
  51. }
  52. }
  53. max_ch_keta = 1;
  54. i = max_ch_count;
  55. while (i >= 10) {
  56. max_ch_keta++;
  57. i /= 10;
  58. }
  59.  
  60. // disp scale
  61. printf("File : " INPUT_FILE "\n");
  62. printf(" max count = %*d\n", max_ch_keta, max_ch_count);
  63. printf(" +");
  64. for (i = 0; i < RANGE; i++) {
  65. printf("-");
  66. }
  67. printf("+\n");
  68.  
  69. // show result
  70. for (i = 0; i < 26; i++) {
  71. printf("%c:", (char) (i + 'a'));
  72. hist = (RANGE * counter[i] + max_ch_count - 1) / max_ch_count;
  73. for (j = 0; j < hist; j++) {
  74. printf("*");
  75. }
  76. for (; j < RANGE; j++) {
  77. printf(" ");
  78. }
  79. printf(":%5.1f%%=%*d\n", (double) counter[i] / sum * 100.0, max_ch_keta, counter[i]);
  80. }
  81.  
  82. // end
  83. return 0;
  84. }
  85.  
  86. // End of cs_161_007.c
Runtime error #stdin #stdout 0.01s 1804KB
stdin
Standard input is empty
stdout
Standard output is empty