2017-02-26 16 views
-6

這樣的功能是代碼我收到:將轉出雙入一小部分

char fractionCalculator(long double interest) 
{ 
    char multiples_of_one_eighth[7] = { 1/8, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8 }; 

    char Return; 
    char * returnValue; 

    if (interest - trunc(interest) < 0.0625) 
    { 
    Return = '\0'; 
    } 
    else if (interest - trunc(interest) < 0.1875) 
    { 
    Return = multiples_of_one_eighth[1]; 
    } 
    else if (interest - trunc(interest) < 0.3125) 
    { 
    Return = multiples_of_one_eighth[2]; 
    } 
    else if (interest - trunc(interest) < 0.4375) 
    { 
    Return = multiples_of_one_eighth[3]; 
    } 
    else if (interest - trunc(interest) < 0.5625) 
    { 
    Return = multiples_of_one_eighth[4]; 
    } 
    else if (interest - trunc(interest) < 0.6875) 
    { 
    Return = multiples_of_one_eighth[1]; 
    } 
    else if (interest - trunc(interest) < 0.8125) 
    { 
    Return = multiples_of_one_eighth[1]; 
    } 

} 

,它甚至沒有運行。當我做到這一點時,我一直在想,這個分數將不得不顯示爲一個字符數組,但是我猜測我犯了這麼大的錯誤。我相信它不會運行的原因與函數的輸出和輸入有關?我一直在這工作幾個小時,仍然無法弄清楚。感謝任何人,可以幫助!

+1

你所說的「混合號」是什麼意思?這是不是你覺得像「1月8日」值可以有意義放入一個'char'一個有效的數據類型在C? – abelenky

+0

什麼是「混合號碼」?而你的意思是「甚至沒有運行」。什麼是確切的行爲?請提供[mcve]幷包含輸入,預期輸出/行爲和實際輸出/行爲。 – kaylum

+0

打開警告,與它的'-Wall'大多數編譯器。它會指出很多問題。 – Schwern

回答

0

在調試器中檢查'multiples_of_one_eighth'數組。你會發現它們都是零,因爲你要求編譯器在整數空間中執行像(1/8)一樣的分割。如果我正確地理解了你的願望,你可能希望它是一個char *(c-string)數組,用每個條目的雙引號括起來。然後,將您的返回類型更改爲char *。

1

我猜這是一個函數,需要一個數字並將其舍入到最接近的八分之一。像0.26會返回"1/4"

主要問題是您無法在一個char中存儲小數的字符串表示形式(即字符串"1/4",而不是數字1/4)。 char存儲單個1字節字符。 1/8是一個返回數字的數學公式。 "1/8"是一個4字符的字符串,它存儲在char *中,該指針指向包含字符的某個內存。你想要最後一個。所以你回來了char *

您可能會返回一個像½的Unicode分數,但這會很快變得複雜。


然後就是那個什麼也沒做的interest - trunc(interest)

首先,trunc需要double,但interestlong double。它返回一個double,但是然後從long double減去它的損失精度。編譯器警告會告訴你類似的事情,用-Wall打開它們。你需要使用truncl。這不是什麼大不了的事情,它不會公開地破壞代碼,但它會失去一些精確度long double

但是你完全不需要使用truncltruncl(0.1)0.0truncl(0.9)0.0。所有interest - trunc(interest)正在做,在您的範圍內,是interest - 0。所以擺脫它。


但也沒有必要爲Return變量,這樣一個簡單的功能,你可以立即返回。你可能被告知類似於「應該只有一次函數返回」,這是來自Structured Programming之類的過時建議。它應該讓事情變得簡單,但如果遵循得太嚴格,會導致複雜性。

你仍然不應該返回無情。對於這樣一個小功能來說,這並不重要。在這裏,我們可以使用多個返回值作爲early exit,而不是存儲要在循環結束時返回的值或if/else鏈,我們可以立即返回。使代碼更簡單。


但也沒有必要爲multiples_of_one_eighth陣列。這將是有用的,如果我們可以通過數組索引引用的部分,如:

int index = ...some calculation involving interest... 
return multiples_of_one_eight[index]; 

但由於我們通過硬編碼每個索引到的if/else反正,還不如刪除一些複雜性和硬編碼數。然後很容易看到錯誤。像:

else if (interest - trunc(interest) < 0.1875) 
    { 
    Return = multiples_of_one_eighth[1]; 
    } 

你返回multiples_of_one_eighth[1]但是這1/4。我很確定你的意思是1/8

把它放在一起,我們可以得到這樣的事情。

#include <stdio.h> 

const char *fractionCalculator(long double interest) 
{ 
    if (interest < 0.0625) { 
     return "0"; 
    } 
    else if (interest < 0.1875) { 
     return "1/8"; 
    } 
    else if (interest < 0.3125) { 
     return "1/4"; 
    } 
    else if (interest < 0.4375) { 
     return "3/8"; 
    } 
    else if (interest < 0.5625) { 
     return "1/2"; 
    } 
    else if (interest < 0.6875) { 
     return "5/8"; 
    } 
    else if (interest < 0.8125) { 
     return "3/4"; 
    } 
    else { 
     return "1"; 
    } 
} 

int main() { 
    printf("%s\n", fractionCalculator(0.8)); 
} 

請注意,這將返回不能修改的常量字符串,因此它們被聲明爲const char *

還要注意的是,數學是有點不對,我基本上是複製你有什麼,所以我把它留給你來解決。

+0

非常感謝!有效! –