2013-10-08 96 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]'而叮噹給我和你一樣的警告。 –