2013-10-05 72 views
-5

我正在努力使歐姆法律計劃。 V = IR。任何人都可以幫我用我的C代碼嗎?

#include <stdio.h> 

int main(int argc, const char * argv[]) { 
    int V,I,R; 

    printf("please enter the value of the current if the value is not known make I=0 "); 
    scanf("%d", &I); 
    printf("please entre the value of the resistance if the value is not known make R=0"); 
    scanf("%d", &R); 
    printf("please enter the value of the voltage, if unknown make V=0"); 
    scanf("%d", &V); 

    if (V == 0) 
     V = I*R; 
    { 
     printf(" V = %d",V); 
    } 
    else if (I==0) 
     I = V/R; 
    { 
     printf("I = %d ",I); 
    } 
    else 
     R = V/I; 
    { 
     printf("R= %d",R); 

    } 

    return 0; 
} 

我是初學者,我該如何改進我的代碼,所以它的工作原理? 任何幫助如此讚賞謝謝。

+3

歡迎使用stackoverflow。爲了幫助你,我們需要知道你的實際問題。你期望程序做什麼以及它做什麼? – Philipp

+1

請閱讀[C tutorial](http:// www.tutorialspoint.com/cprogramm荷蘭國際集團/)。 – Gangadhar

+0

這個問題似乎是無關緊要的,因爲它太侷限了。 –

回答

1

使用浮點變量:

#include <stdio.h> 

int main(int argc, const char * argv[]) 
{ 
    float V,I,R; 

    printf("welcome to my lovely program"); 
    printf("please enter the value of the current if the value is not known make I=0 "); 
    scanf("%f", &I); 
    printf("please entre the value of the resistance if the value is not known make R=0"); 
    scanf("%f", &R); 
    printf("please enter the value of the voltage, if unknown make V=0"); 
    scanf("%f", &V); 
    if (V == 0) 
    { 
     V = I*R; 
     printf(" V = %f",V); 
    } 
    else if (I==0) 
    { 
     I = V/R; 
     printf("I = %f ",I); 
    } 
    else 
    { 
     R = V/I; 
     printf("R= %f",R); 
    } 
return 0; 
} 

分割時,如果使用int,而不是floatdouble你會被截斷值。並請學會縮進你的代碼 - 你的if-else塊都搞砸了。

1

你需要學習縮進和發言應內給予的if/else if/else語句塊

if (V == 0) { 
    V = I*R; 
    printf(" V = %d",V); 
} else if (I == 0) { 
    I = V/R; 
    printf("I = %d ",I); 
} else { 
    R = V/I; 
    printf("R= %d",R); 
} 

讓你所有的declartions浮因爲你是在第二和第三計算我和R分割由於整數你只會得到整數部分。

0

首先 - 如果條件跟在圓括號之後,如果在「if」條件中有多於1行要執行,所以你應該把第一行放在括號內的if條件之後,而不是隻有print statement.same您使用的條件語句elseif

+1

對於大多數說英語的人(具有編程背景),「括號」保留給「(」和「)」。您所說的「{}」符號將是「大括號」或「大括號」(與「[]」「方括號」區分開來)。你應該總是使用它們來包圍'if'和'else'子句。 –

0

儘管您可以使用if語句而不使用括號來編寫決策,但是您做了什麼(在if語句後添加決策,然後放入括號,然後添加另一個決定)是不正確的語法C.通常,我總是將決定放在括號內的if或else語句中,因爲它使您的代碼更容易閱讀和編輯。因此,對於它的工作做的是根據上述解釋

#include <stdio.h> 

int main(int argc, const char * argv[]) { 
    int V,I,R; 

    printf("please enter the value of the current if the value is not known make I=0 "); 
    scanf("%d", &I); 
    printf("please entre the value of the resistance if the value is not known make R=0"); 
    scanf("%d", &R); 
    printf("please enter the value of the voltage, if unknown make V=0"); 
    scanf("%d", &V); 

    if (V == 0) 

    { 
    V = I*R; 
     printf(" V = %d",V); 
    } 
    else if (I==0) 
     I = V/R; 
    { 
     printf("I = %d ",I); 
    } 
    else 

    { 
    R = V/I; 
     printf("R= %d",R); 

    } 

    return 0; 
} 

但是這隻適用於整數,所以如果我是你,我會用浮筒或雙數據類型(雖然副歌使用雙數據低於因爲它使用更多的內存而不是浮點數,加上你不會真的需要一個數字來保留太多的小數位,所以當你甚至不使用它們的時候,你爲什麼要把參數放在int main函數中?

相關問題