Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- hollow rectangle pattern v1.c
- Rectangle is defined with two attributes (parameters):
- number of columns (wide) and number of rows (height)
- The beginning of the coordinate system is in the upper left UL corner
- of the window and there is a point (x, y) = (0.0).
- x is column, y is row.
- Characteristic points of window and rectangle are:
- TL top left (x, y) = ( 0, 0)
- TR top right (x, y) = (79, 0) = ( MAX_COLUMNS, 0 )
- BL bottom left (x, y) = (0, 25) = ( 0 , MAX_ROWS )
- BR bottom right (x, y) = (79,25) = ( MAX_COLUMNS, MAX_ROWS )
- */
- #include <stdio.h>
- #include <time.h> // for clock()
- #define SECOND 1000 // time for delay() in milliseconds
- #define MAX_COLUMNS 79
- #define MAX_ROWS 25
- // wait (do nothing) for time in milliseconds
- void delay(int milliseconds)
- {
- clock_t start_time = clock(); // get start time
- // looping (do nothing) till required time is not acheived
- while ( clock() < start_time + milliseconds )
- ; // do nothing
- }
- // Function to print hollow rectangle with char '*'
- // from current cursor position with printf()
- // Rectangle is defined by number of columns (wide) and number of rows (height)
- void print_hollow_rectangle_printf( int columns, int rows )
- {
- int i, j;
- for (i = 1; i <= rows; i++)
- {
- for (j = 1; j <= columns; j++)
- {
- if (i==1 || i==rows || j==1 || j==columns)
- printf("*");
- else
- printf(" ");
- }
- printf("\n");
- }
- }
- // Function to print hollow rectangle with char '*'
- // from current cursor position with putchar()
- // Rectangle is defined by number of columns (wide) and number of rows (height)
- void print_hollow_rectangle_putchar( int columns, int rows )
- {
- int i, j;
- for (i = 1; i <= rows; i++)
- {
- for (j = 1; j <= columns; j++)
- {
- if (i==1 || i==rows || j==1 || j==columns)
- putchar('*');
- else
- putchar(' ');
- }
- putchar('\n');
- }
- }
- // move cursor down howMuchRows from current position
- void cursorDown(int howMuchRows)
- {
- int i;
- if( howMuchRows < 0 )
- howMuchRows = 0;
- if( howMuchRows > MAX_ROWS )
- howMuchRows = MAX_ROWS;
- for (i = 0; i < howMuchRows; i++)
- putchar('\n');
- }
- // move cursor right howMuchColumns from current position
- // overwrite existing characters on it's way
- void cursorRight(int howMuchColumns)
- {
- int i;
- if( howMuchColumns < 0 )
- howMuchColumns = 0;
- if( howMuchColumns > MAX_COLUMNS )
- howMuchColumns = MAX_COLUMNS;
- for (i = 0; i < howMuchColumns; i++)
- putchar(' ');
- }
- // move cursor down to position (x, y) = (column, howMuchRows) from current position
- // columns are counting from 0
- // rows are counting from current row
- // overwrite existing characters on it's way
- void cursorGoToFromCurrentColumnRow(int column, int howMuchRows)
- {
- int i;
- if( howMuchRows < 0 )
- howMuchRows = 0;
- if( howMuchRows > MAX_ROWS )
- howMuchRows = MAX_ROWS;
- if( column < 0 )
- column = 0;
- if( column > MAX_COLUMNS )
- column = MAX_COLUMNS;
- for (i = 0; i < howMuchRows; i++) // cursor down
- putchar('\n');
- for (i = 0; i < column; i++) // cursor right (with overwrithing existing characters)
- putchar(' ');
- }
- int main()
- {
- int i, columns = MAX_COLUMNS, rows = MAX_ROWS;
- // int i, columns = 10, rows = 7; // square
- // print ruler
- puts(" 1 2 3 4 5 6 7 ");
- puts("0123456789012345678901234567890123456789012345678901234567890123456789012345678");
- for (i = 0; i < 1; i++)
- {
- //system("CLS");
- // from current cursor position
- print_hollow_rectangle_printf(15, 8); // wide 15, height 8
- cursorDown(2);
- print_hollow_rectangle_putchar(40, 5); // wide 20, height 5
- //delay(100); // wait 100 milliseconds
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement