fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5.  
  6. typedef unsigned char uchar_t;
  7.  
  8. static void printline( FILE* stream, const char* head, int min, int max, int mul, const char* fmt, char end)
  9. {
  10. int i;
  11. fprintf( stream, head, mul );
  12. for( i=min; i<=max; ++i )
  13. fprintf( stream, fmt, i * mul );
  14. fprintf( stream, "%c", end );
  15. }
  16.  
  17. static void strpad( char* s, int size, char pad, char end )
  18. {
  19. if( size < 2 ){
  20. *s = '\0' ;
  21. return ;
  22. }
  23. memset( s, pad, size - 2 );
  24. s[ size - 2 ] = end ;
  25. s[ size - 1 ] = '\0';
  26. }
  27.  
  28. int main(int argc, char* argv[])
  29. {
  30. uchar_t nketa;
  31. char* fmt;
  32. char* head1;
  33. char* head2;
  34. char tmp[8];
  35. int min;
  36. int max;
  37. int i;
  38. FILE* stream;
  39.  
  40. //if( argc < 3 ){
  41. // fprintf( stderr, "Usage %s <min> <max>\n", argv[0] );
  42. // return 1;
  43. //}
  44. //min = atoi(argv[1]);
  45. //max = atoi(argv[2]);
  46. fscanf( stdin, "%d %d", &min, &max );
  47. snprintf( tmp, sizeof tmp, "%d", max*max );
  48. nketa = strlen( tmp );
  49.  
  50. if( nketa <= 1 )
  51. nketa = 2;
  52. if( min < 0 || max < 0 )
  53. nketa++;
  54.  
  55. fmt = (char* )malloc( nketa + 4 );
  56. head1 = (char* )malloc( nketa + 2 );
  57. head2 = (char* )malloc( nketa + 2 );
  58.  
  59. snprintf( fmt, nketa + 4, "%%%dd|", nketa );
  60. strpad( head1, nketa + 2, ' ', '|' ) ;
  61. strpad( head2, nketa + 2, '-', '+' ) ;
  62.  
  63. //if( argc == 4 ){
  64. // stream = fopen( argv[3], "w" );
  65. // if(! stream ){
  66. // perror( argv[3] );
  67. // exit(EXIT_FAILURE);
  68. // }
  69. //} else
  70. stream = stdout;
  71.  
  72. printline( stream, head1, min, max, 1, fmt, '\n' );
  73. printline( stream, head2, min, max, 1, head2, '\n' );
  74. for( i=min; i<=max; ++i )
  75. {
  76. printline( stream, fmt, min, max, i, fmt, '\n' );
  77. printline( stream, head2, min, max, 1, head2, '\n' );
  78. }
  79. if( stream )
  80. fclose( stream );
  81. free( fmt );
  82. free( head1 );
  83. free( head2 );
  84. return 0;
  85. }
Success #stdin #stdout 0s 2380KB
stdin
1 9
stdout
  | 1| 2| 3| 4| 5| 6| 7| 8| 9|
--+--+--+--+--+--+--+--+--+--+
 1| 1| 2| 3| 4| 5| 6| 7| 8| 9|
--+--+--+--+--+--+--+--+--+--+
 2| 2| 4| 6| 8|10|12|14|16|18|
--+--+--+--+--+--+--+--+--+--+
 3| 3| 6| 9|12|15|18|21|24|27|
--+--+--+--+--+--+--+--+--+--+
 4| 4| 8|12|16|20|24|28|32|36|
--+--+--+--+--+--+--+--+--+--+
 5| 5|10|15|20|25|30|35|40|45|
--+--+--+--+--+--+--+--+--+--+
 6| 6|12|18|24|30|36|42|48|54|
--+--+--+--+--+--+--+--+--+--+
 7| 7|14|21|28|35|42|49|56|63|
--+--+--+--+--+--+--+--+--+--+
 8| 8|16|24|32|40|48|56|64|72|
--+--+--+--+--+--+--+--+--+--+
 9| 9|18|27|36|45|54|63|72|81|
--+--+--+--+--+--+--+--+--+--+