fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. //drawTriangle: Called on to draw the triangle
  6. void drawTriangle(int size, char direction, char pattern);
  7.  
  8. //main: Gathers input and calls drawTriangle
  9.  
  10. //lowercase a c character by reference
  11. char tolower(char &c)
  12. {
  13. if(c > 0x40 && c < 0x5B)
  14. c += 0x20;
  15. return c;
  16. }
  17.  
  18. int strlen(char *str)
  19. {
  20. char *c = str;
  21. int i;
  22. for(i = 0; *c!=0; ++i, ++c);
  23.  
  24. return i;
  25. }
  26.  
  27. bool strstr(char *in, char c)
  28. {
  29. char *str = in;
  30. for(int i = 0; i < strlen(in); ++str, ++i)
  31. if(*str==c)
  32. return true;
  33.  
  34. return false;
  35. }
  36.  
  37. void ScanInputChar(const char *prompt, char &input, int len, char* expected)
  38. {
  39. do
  40. {
  41. char buf[16];
  42. printf(prompt);
  43. fgets(buf, 16, stdin);
  44. input = buf[0];
  45. tolower(input);
  46. }
  47. while(!strstr(expected, input));
  48. }
  49.  
  50. void ScanInputNum(const char *prompt, int &input, int expectedmin, int expectedmax)
  51. {
  52. do
  53. {
  54. char buf[16];
  55. printf(prompt);
  56. fgets(buf, 16, stdin);
  57. input = atoi(buf);
  58. }
  59. while(input < expectedmin || input > expectedmax);
  60. }
  61.  
  62. int main() {
  63. //Variables which will be filled by user
  64. int size;
  65. char direction, pattern;
  66.  
  67. //Getting input from user
  68. ScanInputNum("Please enter the size of your triangle (Must be between 1-15)\n", size, 1, 15);
  69. ScanInputChar("Please enter the direction of your triangle (Either F or B)\n", direction, 1, "FBfb");
  70. ScanInputChar("Please enter the pattern for your triangle (V for vertical, H for horizontal, or N for none)\n", pattern, 1, "VHNvhn");
  71.  
  72. printf("\n\nHere is your triangle:\n\n");
  73.  
  74. //Calling drawTirangle and sending the user input
  75. drawTriangle(size, direction, pattern);
  76.  
  77. system("pause");
  78.  
  79. return 0;
  80. }
  81.  
  82. //drawTriangle: Draws the triangle using the users parameters
  83. void drawTriangle(int size, char direction, char pattern)
  84. {
  85. int iStart = 0;
  86. int iPos = 0;
  87. int iSize = size;
  88. int iIncrement = 0;
  89. bool bDirection = (direction == 'b'); //true if backward
  90.  
  91. if(bDirection)
  92. iIncrement = -1;
  93. else
  94. iIncrement = 1;
  95.  
  96. for(int i = 0; i < size; ++i, iPos+=iIncrement)
  97. {
  98. if(bDirection)
  99. {
  100. for(int k = size-i;k>1; k--)
  101. printf(" ");
  102. }
  103.  
  104. for(int j = 0; j != iPos + iIncrement; j+=iIncrement)
  105. {
  106. if(bDirection)
  107. printf(" ");
  108.  
  109. if(pattern == 'v')
  110. {
  111. if((j + ((i%2)*bDirection))%2==0)
  112. printf("#");
  113. else
  114. printf("*");
  115. }
  116. else if(pattern == 'h')
  117. {
  118. if(i%2==0)
  119. printf("#");
  120. else
  121. printf("*");
  122. }
  123. else
  124. {
  125. printf("*");
  126. }
  127.  
  128. if(!bDirection)
  129. printf(" ");
  130.  
  131. }
  132. printf("\n");
  133. }
  134. }
Success #stdin #stdout #stderr 0s 3460KB
stdin
10
b
v
stdout
Please enter the size of your triangle (Must be between 1-15)
Please enter the direction of your triangle (Either F or B)
Please enter the pattern for your triangle (V for vertical, H for horizontal, or N for none)


Here is your triangle:

                   #
                 * #
               # * #
             * # * #
           # * # * #
         * # * # * #
       # * # * # * #
     * # * # * # * #
   # * # * # * # * #
 * # * # * # * # * #
stderr
sh: 1: pause: not found