基本数学函数的意外值

Unexpected values for basic math function

本文关键字:意外 函数      更新时间:2023-10-16

我正在学习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 = %fn", sum);
    else if (symbol=='-')
       printf("The difference between x and y = %fn", difference);
    else if (symbol=='*')
       printf("The product of x and y = %fn", product);
    else if (symbol=='/')
       printf("x divided by y = %fn", quotient);
    else if (symbol=='^')
       printf("x multiplied exponentially by y = %fn", exponent);
    else if (symbol=='%')
       printf("x is this much percent of y =%fn", modulus);
    return 0;
}

正弦 这是一个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 = %fn", sum);
    else if (symbol=='-')
       printf("The difference between x and y = %fn", difference);
    else if (symbol=='*')
       printf("The product of x and y = %fn", product);
    else if (symbol=='/')
       printf("x divided by y = %fn", quotient);
    else if (symbol=='^')
       printf("x multiplied exponentially by y = %fn", exponent);
    else if (symbol=='%')
       printf("x is this much percent of y =%fn", modulus);
    return 0;
    }

您甚至在初始化xy之前就正在执行所有计算。未初始化的变量具有不确定的内容,因此您获得的任何结果都是不确定的。你冒着用你的一些语句调用未定义行为的风险(这意味着不可能说你的代码在任何确定性的情况下会做什么)。只有在读取 xy 的值后才执行操作。

#include <math.h>

如果您不使用 <math.h> 中的任何函数,为什么会有这个包含?

float sum = x+ y;

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

float quotient = x/y;

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

float exponent = x^y;

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

scanf(" %c", &symbol);

为什么前面有空间?

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

你需要拿起一本好书并开始阅读以了解C是如何工作的。