2013-10-08 69 views
1

我写了几个函数,我已经多次检查过,看看我是否写错了公式或任何已定义的变量,但这些看起来是正确的。 测试用例由我的教师提供,因此我假设这些人必须能够工作!我不确定我的代码中存在问题的位置或内容。顺便说一下,之前所有函数的测试用例都已经过去了,它只是最后一个函数,估计问题给了我很多问题。为什么我的测试案例不能保持失败?

注:我已经声明了很多常量变量,其中一些似乎现在可能未被使用,但是您可以忽略它。

#include "grove.h" 
#include <math.h> 
#include <stdlib.h> 

#define SOILQUALACONST 10 /* Number subtracted from both x and y in typeA. */ 
#define SOILQUALBCONST 10 /* Number subtracted from both x and y in typeB. */ 
#define SUNEXPXTERM 8 /* Number you subtract from x in exponent.*/ 
#define SUNEXPDIV1 10 /* First denominator term in first fraction in exp.*/ 
#define SUNEXPYTERM 12 /* Number you subtract from y in exponent.*/ 
#define SUNEXPDIV2 5 /* Second denominator term in second fraction in exp.*/ 
#define SUNEXPEMULT 10 /* The constant you are multiplying e^(exp.) by.*/ 
#define IRRIEXPONUM 10 /* The numerator in irrigation exposure function.*/ 
#define ESTYIELDNUM1 7 /* First term in fraction part of estimated yield.*/ 
#define ESTYIELDNUM2 7 /* Last term in fraction part of estimated yield.*/ 

double soilQuality(int x, int y) { 
    double typeA, typeB, soilQual; 

    typeA = 1 + (sqrt((pow(x - SOILQUALACONST, 2)) + (pow(y - SOILQUALACONST, 2))  * (1.0))); 
    typeB = (1 + ((abs(x - SOILQUALBCONST) + abs(y - SOILQUALBCONST))/(2.0))); 
    soilQual = (((x + y) % 2) * typeB) + ((1 - ((x + y) % 2)) * typeA); 

    return soilQual; 
} 

double sunExposure(int x, int y) { 
    double exponent, sunexp; 

    exponent = (-0.5) * (((pow(x - SUNEXPXTERM, 2))/(SUNEXPDIV1)) + ((pow(y - 
     SUNEXPYTERM, 2))/(SUNEXPDIV2))); 
    sunexp = SUNEXPEMULT * exp(exponent); 

    return sunexp; 
} 

double irrigationExposure(int x, int y) { 
    double denominator, waterexp; 

    denominator = (1 + abs(x - y)) * (1.0); 
    waterexp = ((IRRIEXPONUM)/(denominator)); 

    return waterexp; 
} 

double estimateYield(int x, int y) { 
    double waterexp, soilqual, sunexp, numerator, estyield; 

    waterexp = irrigationExposure(x, y); 
    soilqual = soilQuality(x, y); 
    sunexp = sunExposure(x, y); 

    numerator = ((ESTYIELDNUM1) - (abs(waterexp - ESTYIELDNUM2))) + 1; 
    estyield = (soilqual) * (sunexp) * ((numerator)/(2.0)); 

    return estyield; 
} 

所以基本上,在过去的功能数的测试用例不断失败,我似乎无法找出原因。下面是测试用例通过我的教练牵连:

#include <stdio.h> 
#include "grove.h" 
#include "checkit.h" 

int main(){ 

    checkit_double(estimateYield(3,3), 0.023697 ); 
    checkit_double(estimateYield(1,19),0.067322); 
    checkit_double(estimateYield(7,8), 20.165240); 
    checkit_double(estimateYield(12,3), 0.007501); 
    checkit_double(estimateYield(4,17), 2.371061); 

    return(0); 
} 

这里是我所得到的,当我运行它们:

Test passed on line 6. 
Test FAILED on line 7. estimateYield(1,19) is 0.088215, expected 0.067322. 
Test passed on line 8. 
Test passed on line 9. 
Test FAILED on line 10. estimateYield(4,17) is 2.766238, expected 2.371061. 

以防万一你需要它,对于estimateYield公式为:

soilQuality(X,Y)* sunExposure(X,Y)*((7-(ABS(irrigationExposure(X,Y) - 7))+ 1)/(2))

+0

请从闲置或不相关的东西清理代码 – xmoex

+0

当然,没问题! – Karen

+0

您是否尝试手动检查结果?顺便说一句:这是作业吗? – xmoex

回答

4

的问题是这里:

numerator = ((ESTYIELDNUM1) - (abs(waterexp - ESTYIELDNUM2))) + 1; 

您正在使用整数abs功能与double价值,因此,你会得到一个截断int结果。更改此行中使用fabs似乎来解决这个问题:

numerator = ((ESTYIELDNUM1) - (fabs(waterexp - ESTYIELDNUM2))) + 1; 

混合整数和浮点运算时,这是一个常见的问题。我建议你使所有的常量和int变量double,并在整个过程中使用浮点运算(和fabs()!)。

还要注意gcc -Wall -Wconversion ...本来是可以赶上的错误给你:

$ gcc -Wall -Wconversion soil.c 
soil.c:58:48: warning: implicit conversion turns floating-point number into integer: 'double' to 'int' [-Wconversion] 
    numerator = ((ESTYIELDNUM1) - (abs(waterexp - ESTYIELDNUM2))) + 1; 
                                  ~~~ ~~~~~~~~~^~~~~~~~~~~~~~ 
+1

这是正确的我刚刚在本地进行了验证并且得到了预期的结果 –

+1

非常感谢!找到为什么我的测试不起作用! – Karen

+1

@Paul,它看起来像你给铛:)的输出。 gcc给了我:'警告:从'double'转换为'int'可能会改变它的值[-Wconversion]'而叮当给我和你一样的警告。 –