【问题标题】:Unexpected values for basic math function基本数学函数的意外值
【发布时间】:2012-06-13 11:55:29
【问题描述】:

我正在学习 C++ 编程的基础知识,我需要编写一个使用 if/else 语句执行一些基本数学函数的程序:减法、加法、乘法等。我没有收到任何编译或运行时错误,但我最终得到的值总是不正确的。

我改变了一些东西,试图看看我哪里出错了,但就文字而言,屏幕上打印的内容是正确的,而不是答案。例如,当我扫描 -(减号)作为输入时,它会读取 x 和 y 之间的差异是一个高得离谱的数字。

无论如何,这是我的代码,任何帮助将不胜感激!

 /*
 * This program performs some basic math calculations
 */

#include <stdio.h>
#include <math.h>

int main() {
    int x,y;
    float sum = x+ y;
    float difference = x-y;
    float product = x*y;
    float quotient = x/y;
    float exponent = x^y;
    float modulus = x%y;
    char symbol;


    printf("What is the value of x?\n");
    scanf("%d", &x);

    printf("What is the value of y?\n");
    scanf("%d", &y);

    printf("What is your operator?\n");
    scanf(" %c", &symbol);

    if (symbol== '+') 
       printf("The sum of x and y = %f\n", sum);
    else if (symbol=='-')
       printf("The difference between x and y = %f\n", difference);
    else if (symbol=='*')
       printf("The product of x and y = %f\n", product);
    else if (symbol=='/')
       printf("x divided by y = %f\n", quotient);
    else if (symbol=='^')
       printf("x multiplied exponentially by y = %f\n", exponent);
    else if (symbol=='%')
       printf("x is this much percent of y =%f\n", modulus);

    return 0;
}

【问题讨论】:

  • 在 C 和 C++ 中,类似 "sum = x + y" 的东西是一个顺序执行的操作,而不是一个等式...... sum 立即接收一个值 x + y,但是 x 和y 那时没有定义,所以 sum 的值是垃圾。

标签: c++ c calculator


【解决方案1】:

因为这是一个 C++ 应用程序,您可以根据需要定义变量。但请记住,用 C 或 C++ 以及所有其他语言编写的程序将从上到下执行,因此当您声明 x 和 y 时,它们没有从用户那里读取的值。始终从上到下跟踪您的代码。在这里,我声明 x 和 y 并首先从用户那里读取它们的值。然后我计算它们的不同操作。我认为除了除法之外,它们中的大多数都可以像 x 和 y 一样再次成为整数,但我会尝试更正您的代码。

如果您想在 C 或 C++ 中进行求幂运算,您必须编写自己的代码。

    #include <stdio.h>
    #include <math.h>

    float power( int x , int y )
    {
        float ret = 1 ;
        for( int i = 1 ; i <= y ; i++ )
            ret *= x ;
        return ret ;
    }

    int main() {

    int x,y;

    printf("What is the value of x?\n");
    scanf("%d", &x);

    printf("What is the value of y?\n");
    scanf("%d", &y);

    printf("What is your operator?\n");
    scanf(" %c", &symbol);


    float sum = (float)x+ y;
    float difference = (float)x-y;
    float product = (float)x*y;
    float quotient = (float)x/y;
    //float exponent = (float)x^y; //Exponentiation should be implemented using multiplication and loops.

   float exponent = power( x , y ) ;
    float modulus = (float)x%y;
    char symbol;

    if (symbol== '+') 
       printf("The sum of x and y = %f\n", sum);
    else if (symbol=='-')
       printf("The difference between x and y = %f\n", difference);
    else if (symbol=='*')
       printf("The product of x and y = %f\n", product);
    else if (symbol=='/')
       printf("x divided by y = %f\n", quotient);
    else if (symbol=='^')
       printf("x multiplied exponentially by y = %f\n", exponent);
    else if (symbol=='%')
       printf("x is this much percent of y =%f\n", modulus);

    return 0;
    }

【讨论】:

    【解决方案2】:

    您甚至在初始化x 和y 之前就已执行所有计算。未初始化的变量具有不确定的内容,因此您拥有的任何结果都是不确定的。您冒着使用某些语句调用未定义行为的风险(这意味着无法确定您的代码将做什么)。只有在读入x 和y 的值后才能进行操作。

    #include <math.h>
    

    如果你不使用&lt;math.h&gt; 的任何函数,为什么还要包含这个?

    float sum = x+ y;
    

    两个整数的和(或差)总是一个整数。你为什么在这里使用float?

    float quotient = x/y;
    

    这会进行整数除法并将结果转换为float。所以,结果可能不是你想要的。尝试将任何一个参数转换为 float/double 以获得正确的结果。此外,您应该始终检查除以零。模数计算也是如此。

    float exponent = x^y;
    

    C 中没有求幂运算符。您需要使用库函数(例如pow)或自己滚动。

    scanf(" %c", &symbol);
    

    为什么前面有空位?

    也不需要这个巨大的 if-else 阶梯。尝试使用switch。

    您需要拿起一本好书开始阅读以了解 C 的工作原理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      相关资源
      最近更新 更多