Advertisement
dmilicev

hollow rectangle print speed v2.c

Oct 23rd, 2019
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.78 KB | None | 0 0
  1. /*
  2.  
  3.     hollow rectangle pattern v2.c
  4.  
  5.     Demonstrates print speed on screen.
  6.  
  7.     https://www.geeksforgeeks.org/puts-vs-printf-for-printing-a-string/
  8.  
  9.     Note: puts() and putchar() are 10 times faster then printf()
  10.  
  11.     Also note that puts() moves the cursor to next line.
  12.     If you do not want the cursor to be moved to next line,
  13.     then you can use following variation of puts().
  14.  
  15.     fputs(str, stdout);
  16.  
  17.     In #include <windows.h> are defined cursor position functions
  18.     for tracking cursor all the time:
  19.     gotoxy(x,y) , wherex() , wherey()
  20.  
  21.     The beginning of the coordinate system is in the upper left UL corner
  22.     of the window and there is a point (x, y) = (0.0).
  23.     x is column, y is row.
  24.  
  25.     Characteristic points of window and rectangle are:
  26.     TL top left     (x, y) = ( 0, 0)
  27.     TR top right    (x, y) = (79, 0) = ( MAX_COLUMNS,           0 )
  28.     BL bottom left  (x, y) = (0, 25) = ( 0          ,    MAX_ROWS )
  29.     BR bottom right (x, y) = (79,25) = ( MAX_COLUMNS,    MAX_ROWS )
  30.  
  31.     Rectangle is defined with two points, TL and BR
  32.     (xTL , yTL, xBR, yBR)
  33.  
  34.     or by TL(xTL,yTL) , width and height
  35.     (xTL , yTL, xTL + width , yTL + height)
  36.  
  37. */
  38.  
  39. #include <stdio.h>
  40. #include <conio.h>                  // for getch()
  41. #include <string.h>                 // for puts()
  42. #include <time.h>                   // for clock()
  43. #include <windows.h>
  44. // tracking cursor all the time, for cursor position functions
  45. // gotoxy(x,y) , wherex() , wherey()
  46.  
  47. #define SECOND 1000                 // time for delay() in milliseconds
  48. #define MAX_COLUMNS 79              // in command window
  49. #define MAX_ROWS    25              // in command window
  50. #define T   0           // for delay(T); , wait, for better view, T = 1000 for 1 second
  51.  
  52.  
  53. /*
  54. // wait (do nothing) for time in milliseconds
  55. void delay(int milliseconds)
  56. {
  57.     clock_t start_time = clock();   // get start time
  58.  
  59.     // looping (do nothing) till required time is not acheived
  60.     while ( clock() < start_time + milliseconds )
  61.         ;                           // do nothing
  62. }
  63. */
  64.  
  65. // place cursor at position (x, y) = ( column, row)
  66. void gotoxy( int column, int row )
  67. {
  68.     COORD coord;
  69.  
  70.     coord.X = column;
  71.     coord.Y = row;
  72.  
  73.     SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), coord );
  74. }
  75.  
  76. // return x coordinate (column) of current cursor position
  77. // on failure return -1
  78. int wherex()
  79. {
  80.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  81.  
  82.     if (!GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &csbi ) )
  83.         return -1;
  84.  
  85.     return csbi.dwCursorPosition.X;
  86. }
  87.  
  88. // return y coordinate (row) of current cursor position
  89. // on failure return -1
  90. int wherey()
  91. {
  92.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  93.  
  94.     if (!GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi ) )
  95.         return -1;
  96.  
  97.     return csbi.dwCursorPosition.Y;
  98. }
  99.  
  100. // Function to print hollow rectangle with char ch, with puts(), 10 times faster
  101. // from current cursor position
  102. // rows     must be between 2 and MAX_ROWS-2 because of 2 horizontal edges
  103. // columns  must be between 2 and MAX_COLUMNS-2 because of 2 vertical edges
  104. void print_hollow_rectangle_puts(char ch, int howMuchColumns, int howMuchRows)
  105. {
  106.     int i;
  107.     char str1[MAX_COLUMNS+1];       // str1 is string for first and last line of rectangle
  108.     char str2[MAX_COLUMNS+1];       // str2 is string for middle lines of rectangle
  109.  
  110.     if( howMuchRows < 2 )                   // 2 for two horizontal edges of the rectangle
  111.         howMuchRows = 2;
  112.  
  113.     if( howMuchColumns < 2 )                // 2 for two vertical edges of the rectangle
  114.         howMuchColumns = 2;
  115.  
  116.     if( howMuchRows > MAX_ROWS - 2 )        // 2 for two horizontal edges of the rectangle
  117.         howMuchRows = MAX_ROWS - 2;
  118.  
  119.     if( howMuchColumns > MAX_COLUMNS - 2 )  // 2 for two vertical edges of the rectangle
  120.         howMuchColumns = MAX_COLUMNS -2;
  121.                                 // creating string for first and last line of rectangle
  122.     str1[0] = ch;                   // TL (top-left) character
  123.     for (i = 1; i < howMuchColumns-1; i++)  // middle characters of top line
  124.         str1[i] = ch;
  125.     str1[i] = ch;                   // TR (top-righth) character
  126.     str1[i+1] = '\0';               // end of first and last line string
  127.                                 // creating string for middle lines of rectangle
  128.     str2[0] = ch;                   // first left character of middle lines
  129.     for (i = 1; i < howMuchColumns-1; i++) // middle characters of middle lines are blanko (space, ' ')
  130.         str2[i] = ' ';
  131.     str2[i] = ch;                   // last right character of middle lines
  132.     str2[i+1] = '\0';               // end of middle lines string
  133.                                 // print rectangle
  134.     puts(str1);                     // print string for first line of rectangle
  135.  
  136.     for (i = 0; i < howMuchRows-2; i++) // print string for middle lines of rectangle
  137.         puts(str2);
  138.  
  139.     puts(str1);                     // print string for last line of rectangle
  140. }
  141.  
  142. // Function to print hollow rectangle with char ch, with puts, 10 times faster
  143. // from cursor position row, column
  144. // rows     must be between 2 and MAX_ROWS-2
  145. // columns  must be between 2 and MAX_COLUMNS-2
  146. void print_hollow_rectangle_puts_column_row(char ch, int column, int row, int howMuchColumns, int howMuchRows)
  147. {
  148.     int i;
  149.     char str1[MAX_COLUMNS+1];       // str1 is string for first and last line of rectangle
  150.     char str2[MAX_COLUMNS+1];       // str2 is string for middle lines of rectangle
  151.  
  152.     if( column + howMuchColumns < 2 )       // 2 for two vertical edges of the rectangle
  153.         howMuchColumns = 2;
  154.  
  155.     if( row + howMuchRows < 2 )             // 2 for two horizontal edges of the rectangle
  156.         howMuchRows = 2;
  157.  
  158.     if( column + howMuchColumns > MAX_COLUMNS ) // if right side od rectangle greather then
  159.         howMuchColumns = MAX_COLUMNS - column;  // MAX_COLUMNS, then trunc it
  160.  
  161.     if( row + howMuchRows > MAX_ROWS - 2 )      // if bottom side od rectangle greather then
  162.         howMuchRows = MAX_ROWS - row;           // MAX_ROWS, then trunc it
  163.  
  164.                                 // creating string for first and last line of rectangle
  165.     str1[0] = ch;                   // TL (top-left) character
  166.     for (i = 1; i < howMuchColumns-1; i++)  // middle characters of top line
  167.         str1[i] = ch;
  168.     str1[i] = ch;                   // TR (top-righth) character
  169.     str1[i+1] = '\0';               // end of first and last line string
  170.  
  171.                                 // creating string for middle lines of rectangle
  172.     str2[0] = ch;                   // first left character of middle lines
  173.     for (i = 1; i < howMuchColumns-1; i++) // middle characters of middle lines are blanko (space, ' ')
  174.         str2[i] = ' ';
  175.     str2[i] = ch;                   // last right character of middle lines
  176.     str2[i+1] = '\0';               // end of middle lines string
  177.  
  178.                                 // print rectangle
  179.     gotoxy( column, row );          // move cursor to (column, row)
  180.     fputs(str1, stdout);            // print string for first line of rectangle without '\n'
  181.  
  182.     for (i = 0; i < howMuchRows-2; i++)
  183.     {
  184.         gotoxy( column, row+i+1 );  // move cursor to (column, row+i+1)
  185.         fputs(str2, stdout);        // print string for middle lines of rectangle without '\n'
  186.     }
  187.  
  188.     gotoxy( column, row+i+1 );      // move cursor to (column, row+i+1)
  189.     fputs(str1, stdout);            // print string for last line of rectangle without '\n'
  190. }
  191.  
  192.  
  193. int main()
  194. {
  195. /*
  196.     i                               is iterator
  197.     xTL, yTL                        are coordinates of top-left (TL) corner of the rectangle
  198.     width                           is width of rectangle from TL corner
  199.     height                          is height of rectangle from TL corner
  200.     plusMinus                       is flag for sign
  201.     number_of_printed_rectangles    is counter
  202.     when_to_change_sign             is counter
  203.     ch                              is character for rectangle border
  204.  
  205. */
  206.     int i, xTL=0, yTL=0, width=MAX_COLUMNS, height=MAX_ROWS;
  207.     int plusMinus=1, number_of_printed_rectangles=0, when_to_change_sign=0;
  208.     char ch='A';
  209.  
  210. /*
  211.     // print ruler
  212.     puts("          1         2         3         4         5         6         7        ");
  213.     puts("0123456789012345678901234567890123456789012345678901234567890123456789012345678");
  214. */
  215.  
  216.     for (i = 0; i < 100; i++)                   // repeat loop more times
  217.     {
  218.             system("CLS");                      // clear the screen
  219.  
  220.             print_hollow_rectangle_puts_column_row(ch++, xTL, yTL, width, height);
  221.  
  222.             number_of_printed_rectangles++;     // count printed rectangles
  223.  
  224.             xTL    = xTL    + plusMinus *  5;   // calculate new arguments for next rectangle
  225.             yTL    = yTL    + plusMinus *  2;
  226.             width  = width  - plusMinus * 10;
  227.             height = height - plusMinus *  4;
  228.  
  229.             when_to_change_sign++;              // increase counter when_to_change_sign
  230.  
  231.             if( when_to_change_sign == 5 )      // then we change sign plus or minus
  232.             {
  233.                 plusMinus = -plusMinus;         // sign + for implosion, - for explosion
  234.                 when_to_change_sign = 0;        // reset counter when_to_change_sign
  235.             }
  236.  
  237.             if(ch=='Z')     // if ch reach 'Z' then start again from 'A'
  238.                 ch='A';
  239.                             // #define T    0   (default value)
  240.             //delay(T);     // you can change T at the beginning: #define T new_value
  241.     }
  242.  
  243.     _getch();   // with getch() was error: "Drawing operation was attempted when there was no current window"
  244.     gotoxy(0, MAX_ROWS);    // move cursor to first column of penultimate row
  245.     printf("\n There are %d printed triangles.\n", number_of_printed_rectangles);
  246.     return 0;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement