training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

forms.c (2083B)


      1 /* Exercise 5.21 */
      2 
      3 void triangle(int, int, int);
      4 void square(int, int, int);
      5 
      6 int main()
      7 {
      8    int n, c, choose = 0;
      9 
     10    while(choose > 4 || choose < 1) {
     11       printf(
     12 	 "\nFigures:\n\n"
     13 	 "   1: empty triangle\n"
     14 	 "   2: full triangle\n"
     15 	 "   3: empty square\n"
     16 	 "   4: full square\n"
     17 	 "\n"
     18       );
     19 
     20       printf("Choose a figure number: ");
     21       scanf("%d", &choose);
     22    }
     23 
     24    printf("\nGive me height: ");
     25    scanf("%d", &n);
     26 
     27    getchar(); /* take newline and, of course, ignore it */
     28 
     29    printf("Give me the character: ");
     30    c = getchar();
     31 
     32    printf("\n");
     33    switch(choose) {
     34       case 1:
     35          triangle(n, c, 0);
     36 	 break;
     37       case 2:
     38 	 triangle(n, c, 1);
     39 	 break;
     40       case 3:
     41 	 square(n, c, 0);
     42 	 break;
     43       case 4:
     44 	 square(n, c, 1);
     45 	 break;
     46    } /* end switch (choose) */
     47    printf("\n");
     48 
     49    return 0;
     50 } /* E0F main */
     51 
     52 /* Show a triangle of "character" of "height" size */
     53 void triangle(int height, int character, int full)
     54 {
     55    int i, j, k, x;
     56 
     57    for(i = height - 1, x = 1; i >= 1; x+=2, i--) {
     58       for(j = 1; j <= i; j++)
     59 	 printf(" ");
     60 
     61       printf("%c", character);
     62 
     63       if(i == height - 1) {
     64 	 printf("\n");
     65 	 continue;
     66       }
     67 
     68       for(k = x; k > 2; k--)
     69 	 printf("%c", full ? character : ' ');
     70 
     71       printf("%c\n", character);
     72    }
     73 
     74    /* print the base */
     75    if(full)
     76       height = height-- * 2;
     77 
     78    for(i = 1; i <= height ; i++)
     79       printf("%c%s", character, full ? "" : " ");
     80    printf("\n");
     81 } /* eof triangle() */
     82 
     83 /* Show a square of "character" of "height" size */
     84 void square(int height, int character, int full)
     85 {
     86    int i, j;
     87 
     88    int base() {
     89       for(i = full ? height : height / 2 + (!(height % 2) ? 0 : 1); i >= 1; i--)
     90          printf("%c%s", character, full ? "" : " ");
     91       printf("\n");
     92    }
     93 
     94    base(height);
     95    /* begin square */
     96    for(i = height - 2; i >= 1; i--) {
     97       printf("%c", character);
     98       for(j = 1; j <= height - (full ? 2 : !(height % 2) ? 3 : 2 ); j++)
     99 	 printf("%c", full ? character : ' ');
    100       printf("%c\n", character);
    101    }
    102    /* end square */
    103    base(height);
    104 
    105 } /* eof square() */
    106