用C编程制作乘法表(1-12)

时间:2018-10-17 01:00:15

标签: c formatting

我想制作一个使用单个整数命令行参数(n)并使n x n乘法表类似于以下4x4示例的程序。它只能是1到12,但不能小于或等于大。因此,如果参数不足,则应打印使用权:Usage: program <n> \n,或者如果超出范围,则应打印:n out of range (1-12)\n,然后退出。

示例: ./程序4

*    1   2   3   4
  +----------------
 1|   1   2   3   4
 2|   2   4   6   8
 3|   3   6   9  12
 4|   4   8  12  16

我尝试过但没有成功的代码。我已复制了编译后的内容,而不是下面的预期。

#include <stdio.h>

int main(void) 
{
    int i, j;

    printf(" +");
    for (i = 1; i < 13; ++i) 
    {
        printf("%#3d ", i);
        printf("\n");
    }

    for (i = 1; i < 64; ++i) 
    {
        printf("-");
        printf("\n");
    }

    for (i = 1; i < 13; ++i) 
    {
        printf("%#2d |", i);

        for (j = 1; j < 11; ++j) 
        {
            printf("%#3d ", i * j);
            printf("\n");
        }
    }

    return 0;
}
  

编译后:

gcc version 4.6.3

 +  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
 1 |  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 2 |  2 
  4 
  6 
  8 
 10 
 12 
 14 
 16 
 18 
 20 
 3 |  3 
  6 
  9 
 12 
 15 
 18 
 21 
 24 
 27 
 30 
 4 |  4 
  8 
 12 
 16 
 20 
 24 
 28 
 32 
 36 
 40 
 5 |  5 
 10 
 15 
 20 
 25 
 30 
 35 
 40 
 45 
 50 
 6 |  6 
 12 
 18 
 24 
 30 
 36 
 42 
 48 
 54 
 60 
 7 |  7 
 14 
 21 
 28 
 35 
 42 
 49 
 56 
 63 
 70 
 8 |  8 
 16 
 24 
 32 
 40 
 48 
 56 
 64 
 72 
 80 
 9 |  9 
 18 
 27 
 36 
 45 
 54 
 63 
 72 
 81 
 90 
10 | 10 
 20 
 30 
 40 
 50 
 60 
 70 
 80 
 90 
100 
11 | 11 
 22 
 33 
 44 
 55 
 66 
 77 
 88 
 99 
110 
12 | 12 
 24 
 36 
 48 
 60 
 72 
 84 
 96 
108 
120

我已经坚持了2天,试图弄清楚为什么它会一直下降。。谢谢。

2 个答案:

答案 0 :(得分:0)

以下代码:

  1. 更正输出格式,
  2. 检查是否有命令行参数
  3. 遵循公理: 每行只有一个语句,每个语句(最多)一个变量声明。
  4. 检查命令行参数以确保其为数字,且范围为1 ... 12
  5. 限制每个变量的scope
  6. puts()适当的情况下调用printf()在CPU周期中非常昂贵
  7. 使用适当的水平间距以提高可读性

现在,建议的代码:

#include <stdio.h>
#include <stdlib.h>   // exit(), EXIT_FAILURE
#include <ctype.h>    // isdigit()

#define MAX_VALUE 12
#define MIN_VALUE 1

int main( int argc, char *argv[] ) 
{

    if( argc != 2 )
    {
        fprintf( stderr, "USAGE: %s <maxTableSize: range 1...12>\n", argv[0] );
        exit( EXIT_FAILURE );
    }

    // implied else, user entered a command line parameter

    if( !isdigit( argv[1][0] ) )
    {
        fprintf( stderr, "command line parameter not numeric\n" );
        exit( EXIT_FAILURE );
    }

    // implied else, command line parameter starts with digit

    int maxTableSize = atoi( argv[1] );

    if( MIN_VALUE > maxTableSize || maxTableSize > MAX_VALUE )
    {
        fprintf( stderr, "command line parameter not in valid range ( 1...12 )\n");
        exit( EXIT_FAILURE );
    }

    // implied else, command line parameter contains valid value

    printf(" +  ");
    for ( int i = 1; i <= maxTableSize; ++i ) 
    {
        printf("%4d ", i);
    }
    puts("");

    for ( int i = 1; i < 5+(maxTableSize*5); ++i ) 
    {
        printf( "-" );
    }
    puts("");

    for ( int i = 1; i <= maxTableSize; ++i ) 
    {
        printf( "%2d |", i );

        for ( int j = 1; j <= maxTableSize; ++j ) 
        {
            printf( "%4d ", i * j );
        }
        puts("");
    }

    return 0;
}

以下内容演示了运行程序的各种条件。 (其中untitled是程序的名称。)

./untitled
USAGE: ./untitled <maxTableSize: range 1...12>

./untitled a
command line parameter not numeric

./untitled 0
command line parameter not in valid range ( 1...12 )

./untitled 13
command line parameter not in valid range ( 1...12 )


./untitled 1
 +     1 
--------
 1 |   1 

./untitled 2
 +     1    2 
------------
 1 |   1    2 
 2 |   2    4 

 .....

./untitled 12
 +     1    2    3    4    5    6    7    8    9   10   11   12 
----------------------------------------------------------------
 1 |   1    2    3    4    5    6    7    8    9   10   11   12 
 2 |   2    4    6    8   10   12   14   16   18   20   22   24 
 3 |   3    6    9   12   15   18   21   24   27   30   33   36 
 4 |   4    8   12   16   20   24   28   32   36   40   44   48 
 5 |   5   10   15   20   25   30   35   40   45   50   55   60 
 6 |   6   12   18   24   30   36   42   48   54   60   66   72 
 7 |   7   14   21   28   35   42   49   56   63   70   77   84 
 8 |   8   16   24   32   40   48   56   64   72   80   88   96 
 9 |   9   18   27   36   45   54   63   72   81   90   99  108 
10 |  10   20   30   40   50   60   70   80   90  100  110  120 
11 |  11   22   33   44   55   66   77   88   99  110  121  132 
12 |  12   24   36   48   60   72   84   96  108  120  132  144 

答案 1 :(得分:0)

在您的代码中,您犯了两个错误。

  1. 您正在将printf ("\n")添加到for循环中。如果是单循环,则应在for循环之外。如果是嵌套循环,则新行应位于每个内部for循环之后。

  2. 您在printf中使用%#3d#应该被删除。

除此之外,在对printf对齐进行少量更改后,更新后的代码在下面

#include <stdio.h>

int main(void) 
{
    int i, j;

    printf("   +");
    for (i = 1; i < 13; ++i) 
    {
        printf("%4d ", i);
    }
    printf("\n");

    for (i = 1; i < 64; ++i) 
    {
        printf("-");
    }
    printf("\n");

    for (i = 1; i < 13; ++i) 
    {
        printf("%2d |", i);

        for (j = 1; j < 13; ++j) 
        {
            printf("%4d ", i * j);
        }
        printf("\n");
    }

    return 0;
}
  

输出如下

   +   1    2    3    4    5    6    7    8    9   10   11   12
---------------------------------------------------------------
 1 |   1    2    3    4    5    6    7    8    9   10   11   12
 2 |   2    4    6    8   10   12   14   16   18   20   22   24
 3 |   3    6    9   12   15   18   21   24   27   30   33   36
 4 |   4    8   12   16   20   24   28   32   36   40   44   48
 5 |   5   10   15   20   25   30   35   40   45   50   55   60
 6 |   6   12   18   24   30   36   42   48   54   60   66   72
 7 |   7   14   21   28   35   42   49   56   63   70   77   84
 8 |   8   16   24   32   40   48   56   64   72   80   88   96
 9 |   9   18   27   36   45   54   63   72   81   90   99  108
10 |  10   20   30   40   50   60   70   80   90  100  110  120
11 |  11   22   33   44   55   66   77   88   99  110  121  132
12 |  12   24   36   48   60   72   84   96  108  120  132  144