ex4_01.c

来自「[C语言入门经典(第4版)]整本书的源码!值得推荐!全部是最简单的源码!」· C语言 代码 · 共 52 行

C
52
字号
/*Exercise 4.1 Generate a multiplication table */
#include <stdio.h>

int main(void)
{
  int table_size = 0;                       /* Table size        */

  printf("Enter the table size (from 2 to 12): ");
  scanf("%d", &table_size);
  if(table_size>12)
  {
    printf("\nTable size must not exceed 12 - setting to 12");
    table_size = 12;
  }
  else if(table_size<2)
  {
    printf("\nTable size must be at least 2 - setting to 2");
    table_size = 2;
  }

  for(int row = 0 ; row<=table_size ; row++)
  {
    printf("\n");                      /* Start new row */
    for(int col = 0 ; col<=table_size ; col++)
    {
      if(row == 0)                     /* 1st row?                     */
      {                                /* Yes - output column headings */
        if(col == 0)                   /* 1st column?                  */
          printf("    ");              /* Yes - no heading             */
        else
          printf("|%4d", col);         /*No - output heading           */
      }
      else
      {                               /* Not 1st row - output rows     */
        if(col == 0)                  /* 1st column?                   */
          printf("%4d", row);         /* Yes - output row label        */
        else
          printf("|%4d", row*col);    /* No - output table entry       */
      }
    }
    if(row == 0 )                     /* If we just completed 1st row  */
    {                                 /* output separator dashes       */
      printf("\n");
      for(int col=0 ; col<=table_size ; col++)
        printf("_____");
    }
  }
  printf("\n");
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?