Posted By

A4i on 12/22/11


Tagged


Versions (?)

TreeDrawing


 / Published in: C
 

This is a console program that draw a Tree...

  1. // -----------------------------------------------------------------------------
  2. // -- Tree Drawing -------------------------------------------------------------
  3. // -----------------------------------------------------------------------------
  4. // --------------------------------- 22/12/11 ----------------------------------
  5. // -----------------------------------------------------------------------------
  6. // ------------------------------------------------------------------- By A4i --
  7. // -----------------------------------------------------------------------------
  8.  
  9. #include <stdio.h>
  10.  
  11. void printTriangle(int width);
  12. void printLine(int size);
  13. void printBase(int size);
  14. void printTree(int size);
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18. int arg, inCheck, size;
  19.  
  20. if(argc == 2)
  21. {
  22. inCheck = sscanf(argv[1], "%d", &arg);
  23.  
  24. if(inCheck && (arg > 0) && (arg <= 40))
  25. {
  26. size = arg;
  27. }
  28. else
  29. {
  30. goto Begin;
  31. }
  32. }
  33. else
  34. {
  35. Begin:
  36.  
  37. puts("Welcome to the TreeDrawing program...\n");
  38.  
  39. do
  40. {
  41. printf("Enter the width [0 < width <= 40] : ");
  42. inCheck = scanf("%d", &size);
  43. fflush(stdin);
  44. }
  45. while(!inCheck || (size < 0) || (size > 40));
  46. }
  47.  
  48. printf("\n");
  49.  
  50. printTree(size);
  51.  
  52. printf("\n");
  53.  
  54. getchar();
  55. return 0;
  56. }
  57.  
  58. void printTriangle(int width)
  59. {
  60. int i, n, size, tmp;
  61.  
  62. for(n = 1; n <= width; n++)
  63. {
  64. tmp = (width - n);
  65.  
  66. for(i = 0; i < tmp; i++)
  67. {
  68. printf(" ");
  69. }
  70.  
  71. for(i = 0; i < n; i++)
  72. {
  73. printf("*");
  74.  
  75. if(i < n - 1)
  76. {
  77. printf(" ");
  78. }
  79. }
  80.  
  81. printf("\n");
  82. }
  83. }
  84.  
  85. void printLine(int size)
  86. {
  87. int i, j, tmp;
  88.  
  89. tmp = (2 * size - 2) / 2;
  90.  
  91. for(i = 0; i < (size / 2); i++)
  92. {
  93. for(j = 0; j < tmp; j++)
  94. {
  95. printf(" ");
  96. }
  97.  
  98. printf("I");
  99.  
  100. if(i != (size - 2) - 1)
  101. {
  102. printf("\n");
  103. }
  104. }
  105. }
  106.  
  107. void printBase(int size)
  108. {
  109. int i, tmp, width;
  110.  
  111. width = size / 2;
  112.  
  113. tmp = size - width;
  114.  
  115. for(i = 0; i < tmp; i++)
  116. {
  117. printf(" ");
  118. }
  119.  
  120. for(i = 0; i < width; i++)
  121. {
  122. printf("+");
  123.  
  124. if(i != width - 1)
  125. {
  126. printf(" ");
  127. }
  128. }
  129. }
  130.  
  131. void printTree(int size)
  132. {
  133. printTriangle(size);
  134. printLine(size);
  135. printBase(size);
  136. }

Report this snippet  

You need to login to post a comment.