传递双精度值返回未知值C

时间:2018-03-01 01:32:08

标签: c methods module double division

我很难绕过这个人。我们有一个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\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工作正常,但是双倍和漂浮似乎没有好好。

提前致谢。

1 个答案:

答案 0 :(得分:1)

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

这一行:

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

应该是:

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

正如你所写的,测试总是返回“true”并且“operator”被手动覆盖为值“4”...将“operator”赋值为“4”使得中间返回值为4,这是不是0因此“真实”。

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

我会发现更多报道,但这可能会解决问题。