传递双精度值返回未知值C.
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了传递双精度值返回未知值C.相关的知识,希望对你有一定的参考价值。
我很难缠绕这个。我们有一个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值构建一个int数组,以确定运算符是什么
//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个组,并向用户显示相应的数学问题。我遇到问题的当然是分裂。我已将所有打印件留在那里,用于跟踪正在进行的操作,如您在示例中打印出来的那样。
if(x==4)
{
division = 0.00;
printf("Here is the variable before: %.6f
", division);
division = divide(y,z);
printf("Here is the variable after: %.6f
",division);
symbol1 = '/';
askthatquestion(k/3,symbol1,y,z);
scanf("%lf", &userDivision);
printf("Here's the user input: %lf
", userDivision);
printf("Here's what we're comparing against: %lf
", (double)division);
if(division==userDivision)
{
printf("Good job!
");
total++;
}
else
{
printf("Try again
");
}
这是数据发送到的相应模块/方法
#include <stdio.h>
double divide(y,z)
{
double param1 = y;
double param2 = z;
printf("Here's the values passed %.2f & %.2f
", param1, param2);
double result;
int temp;
result = ((double)y/(double)z);
printf("Here's Step 1: %.6f
", result);
result = result+.005;
printf("Here's Step 2: %.6f
", result);
result = result *100;
printf("Here's Step 3: %.6f
", result);
temp = result;
printf("Here it is as an int: %d
", temp);
result = (double) temp/100;
printf("Here it is rounded to 2 decimal places: %.6f
", result);
printf("The result is %f
", 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工作正常,但双打和浮动只是似乎没有好好。
提前致谢。
答案
我看到的直接问题,没有更深入消化您的代码/设计:
这一行:
if(operator=4 && b==1)
应该:
if(operator==4 && b==1)
正如您所写的那样,测试总是返回“true”,并且“operator”被手动覆盖为值“4”...将“operator”赋值为“4”使得中间返回值为4且不为0且因此“真实”。
不用担心我过于严厉批评...... Python是少数几种不允许无效使用“=”代替“==”的语言之一......即使是经验丰富的C / Java开发人员也必须注意这一点。
我发现它会报告更多,但这可能会解决问题。
以上是关于传递双精度值返回未知值C.的主要内容,如果未能解决你的问题,请参考以下文章