基本数学函数的意外值
Unexpected values for basic math function
我正在学习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;
}
您甚至在初始化x
并y
之前就正在执行所有计算。未初始化的变量具有不确定的内容,因此您获得的任何结果都是不确定的。你冒着用你的一些语句调用未定义行为的风险(这意味着不可能说你的代码在任何确定性的情况下会做什么)。只有在读取 x
和 y
的值后才执行操作。
#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是如何工作的。
- C++析构函数被意外调用双链表
- 概念解析为使用 std::make_signed_t 时意外的函数模板
- 类中静态函数C++意外结果
- C++意外调用继承类的函数
- 模板函数意外的结果
- 在调用 std::bind 的产品后意外调用析构函数
- C++将派生类的const值传递给基意外行为的构造函数
- 移动构造函数的意外调用
- 使用类/函数模板组合进行意外诊断
- 函数调用中出现意外编译错误 (C++)
- 使用相同的函数时意外收到"std::out_of_range"错误
- 在第三个 pary 函数中使用矢量引用时出现意外错误
- C++ std::find() 寻址返回向量的类函数时的意外行为
- 基于范围的 std::move 调用意外复制构造函数
- 意外输出..函数绑定在虚拟表中的发生方式
- 在 c++ 中使用函数装饰器(使用闭包)时出现意外的分段错误
- C++中出现意外的编译错误:将默认值传递给函数参数
- 是什么导致了构造函数之后的这些"意外令牌"错误?
- 意外缺少隐式声明的复制/移动构造函数
- 在 winapi 回调函数上返回 FALSE 时出现意外行为(循环被跳过?