fork 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, *head1, *head2;
  32. char tmp[8];
  33. int min, max, i;
  34. FILE *stream;
  35.  
  36. //if( argc < 3 ){
  37. // fprintf( stderr, "Usage %s <min> <max>\n", argv[0] );
  38. // return 1;
  39. //}
  40. //min = atoi(argv[1]);
  41. //max = atoi(argv[2]);
  42. fscanf( stdin, "%d %d", &min, &max );
  43. snprintf( tmp, sizeof tmp, "%d", max*max );
  44. nketa = strlen( tmp );
  45.  
  46. if( nketa <= 1 )
  47. nketa = 2;
  48. if( min < 0 || max < 0 )
  49. nketa++;
  50.  
  51. fmt = (char*)malloc( nketa + 4 );
  52. head1 = (char*)malloc( nketa + 2 );
  53. head2 = (char*)malloc( nketa + 2 );
  54.  
  55. snprintf( fmt, nketa + 4, "%%%dd|", nketa );
  56. strpad( head1, nketa + 2, ' ', '|' ) ;
  57. strpad( head2, nketa + 2, '-', '+' ) ;
  58.  
  59. //if( argc == 4 ){
  60. // stream = fopen( argv[3], "w" );
  61. // if(! stream ){
  62. // perror( argv[3] );
  63. // exit(EXIT_FAILURE);
  64. // }
  65. //} else
  66. stream = stdout;
  67.  
  68. printline( stream, head1, min, max, 1, fmt, '\n' );
  69. printline( stream, head2, min, max, 1, head2, '\n' );
  70. for( i=min; i<=max; ++i )
  71. {
  72. printline( stream, fmt, min, max, i, fmt, '\n' );
  73. printline( stream, head2, min, max, 1, head2, '\n' );
  74. }
  75. if( stream )
  76. fclose( stream );
  77. free(fmt);
  78. free(head1);
  79. free(head2);
  80. return 0;
  81. }
Success #stdin #stdout 0s 2380KB
stdin
-5 5
stdout
   | -5| -4| -3| -2| -1|  0|  1|  2|  3|  4|  5|
---+---+---+---+---+---+---+---+---+---+---+---+
 -5| 25| 20| 15| 10|  5|  0| -5|-10|-15|-20|-25|
---+---+---+---+---+---+---+---+---+---+---+---+
 -4| 20| 16| 12|  8|  4|  0| -4| -8|-12|-16|-20|
---+---+---+---+---+---+---+---+---+---+---+---+
 -3| 15| 12|  9|  6|  3|  0| -3| -6| -9|-12|-15|
---+---+---+---+---+---+---+---+---+---+---+---+
 -2| 10|  8|  6|  4|  2|  0| -2| -4| -6| -8|-10|
---+---+---+---+---+---+---+---+---+---+---+---+
 -1|  5|  4|  3|  2|  1|  0| -1| -2| -3| -4| -5|
---+---+---+---+---+---+---+---+---+---+---+---+
  0|  0|  0|  0|  0|  0|  0|  0|  0|  0|  0|  0|
---+---+---+---+---+---+---+---+---+---+---+---+
  1| -5| -4| -3| -2| -1|  0|  1|  2|  3|  4|  5|
---+---+---+---+---+---+---+---+---+---+---+---+
  2|-10| -8| -6| -4| -2|  0|  2|  4|  6|  8| 10|
---+---+---+---+---+---+---+---+---+---+---+---+
  3|-15|-12| -9| -6| -3|  0|  3|  6|  9| 12| 15|
---+---+---+---+---+---+---+---+---+---+---+---+
  4|-20|-16|-12| -8| -4|  0|  4|  8| 12| 16| 20|
---+---+---+---+---+---+---+---+---+---+---+---+
  5|-25|-20|-15|-10| -5|  0|  5| 10| 15| 20| 25|
---+---+---+---+---+---+---+---+---+---+---+---+