gpt4 book ai didi

c - 传递 double 返回未知值 C

转载 作者:行者123 更新时间:2023-11-30 14:50:22 25 4
gpt4 key购买 nike

我很难理解这个问题。我们有一个 C 语言的基本作业,就是处理简单的数学。我似乎没有将值传递给模块的问题,但计算值没有返回,我不明白为什么。这是代码中的一些片段。

声明的变量:

 #include <stdio.h>
#include <stdlib.h>

int main()
{
//Variables for building the array:
int a,b,operator,values;
int tracker=0;
int quiz[30];

//Variables for traversing array:
int i,x,y,z;
int k=0;
int result;
double division;
char symbol1;

//Variable for user input
int userInput=0;
double userDivision=0.00;

//Varia ble for correct Answers:
int total;

srand(time(NULL));

然后,我使用 1 个值构建一个整数数组来确定运算符是什么

    //Build Array
for(a=0; a<10; a++)
{
operator = rand()%(4)+1;
quiz[tracker]=operator;
tracker++;
for(b=0; b<2; b++)
{
values = rand()%10;
if(operator==4 && b==1)
{
if(values==0)
{
values = rand()%10+1;
}
}
quiz[tracker]=values;
tracker++;
}
}

完成后,我们将 3 人分成一组,并向用户提出适当的数学问题。我遇到的问题当然是 split 。我将所有用于跟踪正在发生的情况的打印留在了那里,正如您将在示例打印输出中看到的那样。

           if(x==4)
{
division = 0.00;
printf("Here is the variable before: %.6f\n\n", division);
division = divide(y,z);
printf("Here is the variable after: %.6f\n\n",division);
symbol1 = '/';
askthatquestion(k/3,symbol1,y,z);
scanf("%lf", &userDivision);
printf("Here's the user input: %lf\n", userDivision);
printf("Here's what we're comparing against: %lf\n", (double)division);
if(division==userDivision)
{
printf("Good job!\n\n");
total++;
}
else
{
printf("Try again\n\n");
}

这里是数据发送到的相应模块/方法

 #include <stdio.h>

double divide(y,z)
{
double param1 = y;
double param2 = z;

printf("Here's the values passed %.2f & %.2f\n", param1, param2);
double result;
int temp;

result = ((double)y/(double)z);
printf("Here's Step 1: %.6f\n", result);

result = result+.005;
printf("Here's Step 2: %.6f\n", result);

result = result *100;
printf("Here's Step 3: %.6f\n", result);

temp = result;
printf("Here it is as an int: %d\n", temp);

result = (double) temp/100;

printf("Here it is rounded to 2 decimal places: %.6f\n", result);
printf("The result is %f\n", result);

return result;
}

正如你所看到的,我希望结果四舍五入到小数点后两位,而且它似乎有效。这是收到的输出;

Here is the variable before: 0.000000

Here's the values passed 5.00 & 9.00
Here's Step 1: 0.555556
Here's Step 2: 0.560556
Here's Step 3: 56.055556
Here it is as an int: 56
Here it is rounded to 2 decimal places: 0.560000
The result is 0.560000
Here is the variable after: 515396076.000000

Question 8: Please answer the following:
5 / 9 =
515396076.00
Here's the user input: 515396076.000000
Here's what we're comparing against: 515396076.000000

所以不知道从这里该做什么。传递 INT 效果很好,但 double 和 float 似乎不太合适。

提前致谢。

最佳答案

我看到的直接问题是,没有更深入地消化您的代码/设计:

这一行:

if(operator=4 && b==1)

应该是:

if(operator==4 && b==1)

正如您所写,测试始终返回“true”,并且“operator”被手动覆盖为值“4”...将“operator”赋值给“4”会产生中间返回值 4,即不为 0,因此为“true”。

不用担心我批评得太严厉...Python 是极少数不允许无效使用“=”代替“==”的语言之一...即使是经验丰富的 C/Java 开发人员也必须这样做注意这一点。

当我发现更多内容时,将会报告,但这可能会解决问题。

关于c - 传递 double 返回未知值 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49041299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com