用户输入和输出显示

时间:2019-12-15 02:02:40

标签: c

我正在编写一个简单的代码,要求用户输入数字;然后,金额将自身相乘并显示在输出面板中;

(myNumber)乘以(myNumber)等于(myTotal)。

但是,不是将自身乘以而是将原始数字的平方乘以。我如何才能使数字乘以本身而不是平方。

这是我的代码:

       #include <stdio.h>
        int main(){
        int num1;
        int total;

         printf("Enter a Number: ");
         scanf(" %d", &num1);


          total = num1 * num1;

          printf("The value of your number is %d", num1);
          printf("\n %d multiplied by %d is %d ", num1, num1, total);


         }

2 个答案:

答案 0 :(得分:1)

您的代码很好,但是您的输出语句混乱了。使用:

  #include <stdio.h>
  int main(){
      int num1;
      int total;

      printf("Enter a Number: ");
      scanf(" %d", &num1);

      total = num1 * num1;

      printf("The value of your number is %d\n", num1);
      printf("%d multiplied by %d is %d\n", num1, num1, total);
  }

答案 1 :(得分:1)

首先,始终验证每个用户输入。如果发生匹配失败,如果您没有捕获到错误,则会导致未定义行为,例如

    fputs ("Enter a Number: ", stdout);
    if (scanf (" %d", &num1) != 1) {    /* validate EVERY user-input */
        fputs ("error: invalid integer input.\n", stderr);
        return 1;
    }

注意space中的" %d"是多余的。"%d"转换说明符默认忽略前导空白)

在编译期间,编译器将连接相邻的字符串文字。不需要多个printf语句-但是有必要将参数精确映射到您在格式字符串中包含的每个转换说明符

    /* compiler will concatenate adjacent strings */
    printf ("\nThe value of your number is %d\n"
            "%d multiplied by %d is %d\n",
            num1, num1, num1, total);

完整示例:

#include <stdio.h>

int main(){
    int num1;
    int total;

    fputs ("Enter a Number: ", stdout);
    if (scanf (" %d", &num1) != 1) {    /* validate EVERY user-input */
        fputs ("error: invalid integer input.\n", stderr);
        return 1;
    }

    total = num1 * num1;

    /* compiler will concatenate adjacent strings */
    printf ("\nThe value of your number is %d\n"
            "%d multiplied by %d is %d\n",
            num1, num1, num1, total);
}

使用/输出示例

$ ./bin/readsquareint
Enter a Number: 25

The value of your number is 25
25 multiplied by 25 is 625

处理整数乘法中的溢出

您还有其他注意事项。对整数值进行平方运算时,存在整数溢出的风险,将结果保存回类型int。例如,46340可以平方,结果保存在total中,但是46431不能。

一个简单的检查溢出的方法是将乘法的初始结果保存为具有更大存储大小的类型,并进行快速除法检查以查看是否返回了适合整数值的结果,例如< / p>

    int num1, total;
    long long tmp;      /* temporary value to hold result of num1 * num1 */
    ...
    tmp = num1 * num1;  /* square value save results in tmp */

    if (num1 && tmp / num1 != num1) {   /* check for overflow */
        fputs ("error: overflow occurred in num1 * num1.\n", stderr);
        return 1;
    }
    total = tmp;

根据您的评论,带有溢出测试的完整代码将是:

#include <stdio.h>

int main (void) {

    int num1, total;
    long long tmp;      /* temporary value to hold result of num1 * num1 */

    fputs ("Enter a Number: ", stdout);
    if (scanf (" %d", &num1) != 1) {    /* validate EVERY user-input */
        fputs ("error: invalid integer input.\n", stderr);
        return 1;
    }

    tmp = num1 * num1;  /* square value save results in tmp */

    if (num1 && tmp / num1 != num1) {   /* check for overflow */
        fputs ("error: overflow occurred in num1 * num1.\n", stderr);
        return 1;
    }
    total = tmp;

    /* compiler will concatenate adjacent strings */
    printf ("\nThe value of your number is %d\n"
            "%d multiplied by %d is %d\n",
            num1, num1, num1, total);
}

如果您还有其他问题,请告诉我。