2012-01-25 25 views
-4

我正在製作一個計算流量的程序,但每次在程序執行時輸入變量時,都會返回錯誤答案。我從這個網站「http://www.flowmeterdirectory.com/flowcalculator.php」得到了計算流量的公式,我將它用作檢查我的程序結果的參考,但它們從來不工作。C程序返回不正確的結果

#include <stdio.h> 
#include <math.h> 
#include "PI.h" 

int flowRateFormula(int,int); //Finally, finds out the flow rate 
int square(int);   

int square(int x) 
{ 
    x=pow(x,2); 

    return x; 
} 

int flowRateFormula(int pipeDiameter,int velocity) 
{ 
    int integer3=0.25*(PI*square(pipeDiameter)*velocity); 

    return integer3; 
} 

int main() 
{ 
    int flowRate; 
    int velocity; 
    int pipeDiameter; 


    printf("Enter velocity of liquid(in./s):"); 
    scanf("%d",&velocity); 
    printf("Velocity=%d in/s.\n",velocity); 

    printf("Enter pipe diameter(inches):"); 
    scanf("%d",&pipeDiameter); 
    printf("Pipe Diameter=%d inches.\n",pipeDiameter); 

    printf("Applying formula for Flow Rate.........\n"); 
    flowRate=flowRateFormula(pipeDiameter,velocity); 

    printf("Pipe Diameter=%d inches.\n",pipeDiameter); 

    printf("Velocity of liquid=%d in/s.\n",velocity); 

    printf("Thus, the flow rate=%d ft/s.\n",flowRate); 

    return 0; 
} 
+0

...把​​它扔進一個調試器,看看會發生什麼? – Bart

+1

啊,明白了。所有其他問題都有「家庭作業」標籤,所以你試圖讓我們爲你做你的工作,而沒有人知道它是爲了學校。很好的嘗試 - 沒有工作。 :) –

+1

爲什麼,哦,爲什麼人們使用'pow(x,2)'來計算一個數字? –

回答

3

這很可能是因爲整型轉換。根據您的輸入,結果可能會很接近,並且由於在整型投射期間舍入,結果似乎具有相同的結果。相反,在你的函數中使用double。

1

由於您在任何地方都使用int,您將會遇到很多錯誤。 floatdouble將是更好的選擇。這一點可以解釋爲什麼儘管輸入不同的值,你仍然可以得到相同的結果。例如,int flow=0.25*x將給出flow對於所有的x的值相同,其中x> -4和X < 4.

0

你正在做的浮點運算並返回結果爲int。我會改變變數浮動或雙打需要。

1

要獲得準確的結果,您需要floatdouble類型。

請試試這個:

#include <stdio.h> 
#include <math.h> 

double PI = 3.14; 

double flowRateFormula(float, float); 

double flowRateFormula(float pipeDiameter, float velocity) 
{ 
    double p = pow(pipeDiameter, 2); 
    double result = 0.25 * PI * p * velocity; 
    return result; 
} 

int main() 
{ 
    double flowRate; 
    float velocity; 
    float pipeDiameter; 

    printf("Enter velocity of liquid(in./s):"); 
    scanf("%f",&velocity); 
    printf("Velocity=%f in/s.\n",velocity); 

    printf("Enter pipe diameter(inches):"); 
    scanf("%f",&pipeDiameter); 
    printf("Pipe Diameter=%f inches.\n",pipeDiameter); 

    printf("Applying formula for Flow Rate.........\n"); 
    flowRate=flowRateFormula(pipeDiameter,velocity); 

    printf("Pipe Diameter=%f inches.\n",pipeDiameter); 
    printf("Velocity of liquid=%f in/s.\n",velocity); 
    printf("Thus, the flow rate=%f ft/s.\n",flowRate); 

    return 0; 
} 

輸出:

$ ./a.out 
Enter velocity of liquid(in./s):10 
Velocity=10.000000 in/s. 
Enter pipe diameter(inches):20 
Pipe Diameter=20.000000 inches. 
Applying formula for Flow Rate......... 
Pipe Diameter=20.000000 inches. 
Velocity of liquid=10.000000 in/s. 
Thus, the flow rate=3140.000000 ft/s. 
$ 
$ 
$ 
$ 
$ ./a.out 
Enter velocity of liquid(in./s):12.34 
Velocity=12.340000 in/s. 
Enter pipe diameter(inches):435.345 
Pipe Diameter=435.345001 inches. 
Applying formula for Flow Rate......... 
Pipe Diameter=435.345001 inches. 
Velocity of liquid=12.340000 in/s. 
Thus, the flow rate=1835912.361516 ft/s. 
$ 

希望這有助於!