Advertisement
dmilicev

hollow rectangle pattern v1.c

Oct 23rd, 2019
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.09 KB | None | 0 0
  1. /*
  2.     hollow rectangle pattern v1.c
  3.  
  4.     Rectangle is defined with two attributes (parameters):
  5.  
  6.     number of columns (wide) and number of rows (height)
  7.  
  8.     The beginning of the coordinate system is in the upper left UL corner
  9.     of the window and there is a point (x, y) = (0.0).
  10.     x is column, y is row.
  11.  
  12.     Characteristic points of window and rectangle are:
  13.  
  14.     TL top left     (x, y) = ( 0, 0)
  15.     TR top right    (x, y) = (79, 0) = ( MAX_COLUMNS,           0 )
  16.     BL bottom left  (x, y) = (0, 25) = ( 0          ,    MAX_ROWS )
  17.     BR bottom right (x, y) = (79,25) = ( MAX_COLUMNS,    MAX_ROWS )
  18.  
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <time.h>                   // for clock()
  23.  
  24. #define SECOND 1000                 // time for delay() in milliseconds
  25. #define MAX_COLUMNS 79
  26. #define MAX_ROWS    25
  27.  
  28.  
  29. // wait (do nothing) for time in milliseconds
  30. void delay(int milliseconds)
  31. {
  32.     clock_t start_time = clock();   // get start time
  33.  
  34.     // looping (do nothing) till required time is not acheived
  35.     while ( clock() < start_time + milliseconds )
  36.         ;                           // do nothing
  37. }
  38.  
  39. // Function to print hollow rectangle with char '*'
  40. // from current cursor position with printf()
  41. // Rectangle is defined by number of columns (wide) and number of rows (height)
  42. void print_hollow_rectangle_printf( int columns, int rows )
  43. {
  44.     int i, j;
  45.  
  46.     for (i = 1; i <= rows; i++)
  47.     {
  48.         for (j = 1; j <= columns; j++)
  49.         {
  50.             if (i==1 || i==rows || j==1 || j==columns)
  51.                 printf("*");
  52.             else
  53.                 printf(" ");
  54.         }
  55.         printf("\n");
  56.     }
  57. }
  58.  
  59. // Function to print hollow rectangle with char '*'
  60. // from current cursor position with putchar()
  61. // Rectangle is defined by number of columns (wide) and number of rows (height)
  62. void print_hollow_rectangle_putchar( int columns, int rows )
  63. {
  64.     int i, j;
  65.  
  66.     for (i = 1; i <= rows; i++)
  67.     {
  68.         for (j = 1; j <= columns; j++)
  69.         {
  70.             if (i==1 || i==rows || j==1 || j==columns)
  71.                 putchar('*');
  72.             else
  73.                 putchar(' ');
  74.         }
  75.         putchar('\n');
  76.     }
  77. }
  78.  
  79. // move cursor down howMuchRows from current position
  80. void cursorDown(int howMuchRows)
  81. {
  82.     int i;
  83.  
  84.     if( howMuchRows < 0 )
  85.         howMuchRows = 0;
  86.  
  87.     if( howMuchRows > MAX_ROWS )
  88.         howMuchRows = MAX_ROWS;
  89.  
  90.     for (i = 0; i < howMuchRows; i++)
  91.         putchar('\n');
  92. }
  93.  
  94. // move cursor right howMuchColumns from current position
  95. // overwrite existing characters on it's way
  96. void cursorRight(int howMuchColumns)
  97. {
  98.     int i;
  99.  
  100.     if( howMuchColumns < 0 )
  101.         howMuchColumns = 0;
  102.  
  103.     if( howMuchColumns > MAX_COLUMNS )
  104.         howMuchColumns = MAX_COLUMNS;
  105.  
  106.     for (i = 0; i < howMuchColumns; i++)
  107.         putchar(' ');
  108. }
  109.  
  110. // move cursor down to position (x, y) = (column, howMuchRows) from current position
  111. // columns are counting from 0
  112. // rows are counting from current row
  113. // overwrite existing characters on it's way
  114. void cursorGoToFromCurrentColumnRow(int column, int howMuchRows)
  115. {
  116.     int i;
  117.  
  118.     if( howMuchRows < 0 )
  119.         howMuchRows = 0;
  120.  
  121.     if( howMuchRows > MAX_ROWS )
  122.         howMuchRows = MAX_ROWS;
  123.  
  124.     if( column < 0 )
  125.         column = 0;
  126.  
  127.     if( column > MAX_COLUMNS )
  128.         column = MAX_COLUMNS;
  129.  
  130.     for (i = 0; i < howMuchRows; i++)   // cursor down
  131.         putchar('\n');
  132.  
  133.     for (i = 0; i < column; i++)    // cursor right (with overwrithing existing characters)
  134.         putchar(' ');
  135. }
  136.  
  137.  
  138. int main()
  139. {
  140.     int i, columns = MAX_COLUMNS, rows = MAX_ROWS;
  141. //    int i, columns = 10, rows = 7;    // square
  142.  
  143.     // print ruler
  144.     puts("          1         2         3         4         5         6         7        ");
  145.     puts("0123456789012345678901234567890123456789012345678901234567890123456789012345678");
  146.  
  147.     for (i = 0; i < 1; i++)
  148.     {
  149.         //system("CLS");
  150.                                                 // from current cursor position
  151.         print_hollow_rectangle_printf(15, 8);   // wide 15, height 8
  152.  
  153.         cursorDown(2);
  154.  
  155.         print_hollow_rectangle_putchar(40, 5);  // wide 20, height 5
  156.  
  157.         //delay(100);   // wait 100 milliseconds
  158.     }
  159.  
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement