2014-12-01 34 views
-1

我應該得到一個十進制數,分別總結了整個部分的數字,並分別小數部分,如何在C中分別在小數點前後求和?

因此,例如,如果我得到: 1321.0365 輸出應該是:7.14

我米應該這樣做,而不使用數組。 這是我從現在開始嘗試做的,從函數中獲取數字後,首先分離兩個部分,然後將十進制數乘以10,直到數字等於int部分。 然後我試圖將每個部分分開求和,並且在我這樣做之後,除以小數部分,直到數字等於整數。理論上它聽起來應該起作用,但我想我錯過了一些路上的東西。 這是我迄今所做的:

double number; 
double sumReal=0; 
int wholePart; 
int sumWhole=0; 
int multiply=1; 

number=getDecimalNumber(); 

wholePart=number; 
number-=wholePart; 

while(number!=(int)number) 
number*=10; 

while (number!=0) 
{ 
    sumReal+=(int)number%10; 
    number/=10; 
} 

while (wholePart!=0) 
{ 
    sumWhole+=(int)wholePart%10; 
    wholePart/=10; 
} 

while(sumReal!=(int)sumReal) 
    sumReal/10; 

number=wholePart+sumReal; 

莫名其妙大部分部件似乎工作,但乘以10的實部總是讓我爲0,並總結實部,並在整數的時候最後也讓我0,這導致我輸出0。

+0

這種情況下'(數字!=(int)的數量)'永遠是正確的,因爲你是因爲精度比較有限,雙整數。所以你會被困在循環中。 – 2014-12-01 21:07:53

+0

請注意,由於「double」中的有界精度(有效數字的數量),嘗試除法和減法直到達到零是沒有意義的。您必須在小數點後面預先定義最大所需的小數位數,以便計算每位數字。 – 2014-12-01 21:20:09

+0

getDecimalNumber如何工作? double可能無法完全按照給定的原樣包含原始輸入數字。 – 2014-12-01 21:28:53

回答

2
#include <stdio.h> 

int main(){ 
    int ch; 
    int n1 = 0, n2 = 0; 
    while((ch=getchar())!= '.'){ 
     n1 += ch - '0'; 
    } 
    while((ch=getchar())!= '\n'){ 
     n2 += ch - '0'; 
    } 
    printf("%d.%d\n", n1, n2); 
    return 0; 
} 
+0

謝謝!最終這就是我如何做到的,因爲分配只是爲了打印輸出,而不一定要將原始數字表示並保持兩倍。 – Ryan 2014-12-02 14:42:43

+0

該方法忽略了這樣的事實,即初始值是從'getDecimalNumber()'調用返回的值,因此從getDecimalNumber()返回的值的getchar()將不起作用。這個答案使得這個數字來自stdin,這個數字沒有被列爲問題基礎的一部分,所以不能假設。 – user3629249 2014-12-02 18:10:19

+0

我不太清楚getDecimalNumber, 也許有可能替換getDecimalNumber。 – BLUEPIXY 2014-12-02 18:13:55

0

它更容易走這條路:

  • 使用數字作爲字符串(得到一個char *指針上的繩子,讓你不積極使用數組)
  • 分裂小數點分隔符 (你會看到兩個char *指針,每串之一)
  • 迭代對焦炭一串字符(遞增指針),轉換當前字符的int和添加
  • 同樣與第二個字符串
  • 你完成了

我強烈懷疑這是你的老師要你做的,比異國情調的計算。

+0

我希望我能,但我不能使用數組,字符串也不:( – Ryan 2014-12-01 21:33:11

+1

嗬!沒關係呢。 – Tchou 2014-12-01 21:40:33

0
here is one way to write the algorithm 
It may still have bugs that will need the OPs attention 


#include <math.h> 

// prototypes 
double getDoubleNumber(void); 
double sumOfDigits(void); 


double sumOfDigits() 
{ 
    double doubleNumber = 0.0; 

    double wholePart; 
    double fractionPart; 

    int sumWhole=0; 
    int sumFraction = 0; 

    double sumParts=0.0; 

    // get the original number 
    doubleNumber=getDoubleNumber(); 

    wholePart = trunc(doubleNumber); 

    // calc fractional part 
    fractionPart = doubleNumber - wholePart; 

    // this may need modification due to inexact representation of value 
    // move fractionPart to left of decimal point 
    while(fractionPart > floor(fractionPart)) 
    { 
     fractionPart *= 10.0; 
    } 

    while(trunc(fractionPart)) 
    { 
     sumFraction += trunc(fmod(fractionPart, 10.0); 
     fractionPart /= 10.0; 
    } 

    // move fractionPart back to right of decimal point 
    while(trunc(sumFraction)) 
    { 
     sumFraction /= 10.0; 
    } 

    // calc sum of whole part digits 
    while (trunc(wholePart)) 
    { 
     sumWhole += trunc(fmod(wholePart, 10.0)); 
     wholePart /= 10.0; 
    } 



    sumParts = sumWhole+sumFraction; 

    return(sumParts); 
} // end function: sumOfDigits 
+0

截斷是多餘的,因爲強制轉換爲整數類型總是截去數字。此外,這不能正常工作,因爲二進制浮點不能完全表示大多數小數值,並截斷數字可能會導致不同的int部分 – 2014-12-02 06:23:33

相關問題