我寫了幾個函數,我已經多次檢查過,看看我是否寫錯了公式或任何已定義的變量,但這些看起來是正確的。 測試用例由我的教師提供,因此我假設這些人必須能夠工作!我不確定我的代碼中存在問題的位置或內容。順便說一下,之前所有函數的測試用例都已經過去了,它只是最後一個函數,估計問題給了我很多問題。爲什麼我的測試案例不能保持失敗?
注:我已經聲明瞭很多常量變量,其中一些似乎現在可能未被使用,但是您可以忽略它。
#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))
請從閒置或不相關的東西清理代碼 – xmoex
當然,沒問題! – Karen
您是否嘗試手動檢查結果?順便說一句:這是作業嗎? – xmoex