fork(2) 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 uchar_t ketasuu( int n )
  18. {
  19. uchar_t count = 0;
  20. do {
  21. n /= 10;
  22. ++count;
  23. } while( n > 0 );
  24. return count;
  25. }
  26.  
  27. static void strpad( char* s, int size, char pad, char end )
  28. {
  29. if( size < 2 ){
  30. *s = '\0' ;
  31. return ;
  32. }
  33. memset( s, pad, size - 2 );
  34. s[ size - 2 ] = end ;
  35. s[ size - 1 ] = '\0';
  36. }
  37.  
  38. int main(int argc, char* argv[])
  39. {
  40. uchar_t nketa;
  41. char *fmt, *head1, *head2;
  42. int min, max, i;
  43. FILE *stream;
  44.  
  45. //if( argc < 3 ){
  46. // fprintf( stderr, "Usage %s <min> <max>\n", argv[0] );
  47. // return 1;
  48. //}
  49. //min = atoi(argv[1]);
  50. //max = atoi(argv[2]);
  51. fscanf( stdin, "%d %d", &min, &max );
  52. nketa = ketasuu(max*max);
  53.  
  54. if( nketa <= 1 )
  55. nketa = 2;
  56. if( min < 0 || max < 0 )
  57. nketa++;
  58.  
  59. fmt = (char*)malloc( nketa + 4 );
  60. head1 = (char*)malloc( nketa + 2 );
  61. head2 = (char*)malloc( nketa + 2 );
  62.  
  63. snprintf( fmt, nketa + 4, "%%%dd|", nketa );
  64. strpad( head1, nketa + 2, ' ', '|' ) ;
  65. strpad( head2, nketa + 2, '-', '+' ) ;
  66.  
  67. //if( argc == 4 ){
  68. // stream = fopen( argv[3], "w" );
  69. // if(! stream ){
  70. // perror( argv[3] );
  71. // exit(EXIT_FAILURE);
  72. // }
  73. //} else
  74. stream = stdout;
  75.  
  76. printline( stream, head1, min, max, 1, fmt, '\n' );
  77. printline( stream, head2, min, max, 1, head2, '\n' );
  78. for( i=min; i<=max; ++i )
  79. {
  80. printline( stream, fmt, min, max, i, fmt, '\n' );
  81. printline( stream, head2, min, max, 1, head2, '\n' );
  82. }
  83. if( stream )
  84. fclose( stream );
  85. free(fmt);
  86. free(head1);
  87. free(head2);
  88. return 0;
  89. }
Success #stdin #stdout 0s 2424KB
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|
--+--+--+--+--+--+--+--+--+--+