1.13.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef enum { false, true } boolean;
  4. #define MAGIC_ASCII 0x177
  5. boolean isBlank(char c);
  6. /* prient input, one word per line */
  7. int main(int argc, char** argv)
  8. {
  9. int scale =0;
  10. if (argc == 2)
  11. {
  12. scale = atoi(argv[1]);
  13. // printf("scale: %d \n",scale);
  14. }
  15. int letter[MAGIC_ASCII+1];
  16. int letterCount[MAGIC_ASCII+1];
  17. char c = (char)1;
  18. for (int i = 0; i <= MAGIC_ASCII ; i++)
  19. letter[i]=i;
  20. for (int i = 0; i <= MAGIC_ASCII ; i++)
  21. letterCount[i]=0;
  22. while ((c = getchar()) != EOF)
  23. {
  24. for(int i=0; i<=MAGIC_ASCII;i++)
  25. // bug : doesn't keep track of endlines
  26. if ((letter[i]==c) && (letter[i]!='\n'))
  27. letterCount[i]++;
  28. }
  29. // for (int i=0; i<=MAGIC_ASCII; i++)
  30. //{
  31. // if (letterCount[i] >0)
  32. // printf("letter count: letter %c count %d \n",letter[i],letterCount[i]);
  33. // }
  34. // get count of columns
  35. int columns =0;
  36. for (int i=0; i<=MAGIC_ASCII; i++)
  37. {
  38. if (letterCount[i]>0)
  39. {
  40. columns++;
  41. }
  42. }
  43. printf("columns: %d \n",columns);
  44. char smallerArray[columns];
  45. int smallerArrayCount[columns];
  46. for (int i=0; i < columns; i++)
  47. {
  48. smallerArray[i]=0;
  49. smallerArrayCount[i]=0;
  50. }
  51. int current_col=0;
  52. for (int i=0; i<=MAGIC_ASCII; i++)
  53. {
  54. printf ("letter: %c \n", letter[i]);
  55. if (letterCount[i]>0)
  56. {
  57. printf("counted! \n");
  58. printf("i: %d lettercount: %d current_col %d \n", i, letterCount[i],current_col);
  59. smallerArrayCount[current_col]=letterCount[i];
  60. smallerArray[current_col]=letter[i];
  61. current_col++;
  62. }
  63. }
  64. for (int i=0; i<columns; i++)
  65. {
  66. printf("small letter count: letter %c count %d \n",smallerArray[i],smallerArrayCount[i]);
  67. }
  68. // get max height
  69. int height =0;
  70. for (int i=0; i<=MAGIC_ASCII; i++)
  71. if (letterCount[i]>height) height=letterCount[i];
  72. printf("height: %d",height);
  73. for (int current_height=height; current_height>0; )
  74. {
  75. for (int i=0; i< columns; i++)
  76. {
  77. if (smallerArrayCount[i]>=current_height) putchar('#');
  78. else putchar(' ');
  79. }
  80. putchar('\n');
  81. if(scale<=0)
  82. current_height--;
  83. else
  84. current_height-=scale;
  85. }
  86. for (int i =0; i < columns;i++)
  87. {
  88. if (smallerArray[i]==EOF)
  89. { putchar(' '); }
  90. if (isBlank(smallerArray[i]))
  91. { putchar(' '); }
  92. else putchar(smallerArray[i]);
  93. }
  94. putchar('\n'); //trailing endline
  95. return 0;
  96. }
  97. boolean isBlank(char c)
  98. {
  99. if (c==' ')
  100. return true;
  101. if (c=='\n')
  102. return true;
  103. if (c=='\t')
  104. return true;
  105. return false;
  106. }