2015-06-03 93 views
1

一個簡單的問題,但我已經嘗試了幾個小時,無法確定最後一點。 如何輕鬆獲得程序只顯示正確答案攝氏或華氏,而不是兩者?如何輸出溫度轉換的正確答案?

int farenheit(int x); 
int celcius(int x); 

int main() 
{ 
    int temperature, converted, converted2; 
    int answer; 

    printf("Temperature please\n"); 
    scanf("%d",&temperature); 
    printf ("Enter conversion to be completed F/C\n"); 
    scanf ("%s",&answer); 

    converted=farenheit(temperature); 
    converted2=celcius(temperature); 

    switch(answer) 
    { 
     case 'f': 
      converted; 
      break; 

     case 'c': 
      converted2; 
      break; 

    } 

    getch(); 
    return 0; 
} 

int farenheit(int x) 
{ 
    int answer; 
    answer = (x-32) * (float)5/9 ; 
    printf ("Temperature in farenheit %d",answer); 
} 

int celcius(int x) 
{ 
    int ans; 
    ans = ((float)9/5*x) + 32; 
    printf ("Temperature in celcius %d",ans); 
} 

回答

0
#include <stdio.h> 
void farenheit(int x); 
void celcius(int x); 
int main() 
{ 
    int temperature,converted,converted2; 
    char answer; 
    printf("Temperature please\n"); 
    scanf("%d\n",&temperature); 
    printf ("Enter conversion to be completed F/C\n"); 
    scanf ("%c",&answer); 
    switch(answer) 
    { 
     case 'f': 
     case 'F': 
      farenheit(temperature); 
      break; 

     case 'c': 
     case 'C': 
      celcius(temperature); 
       break; 

    } 
     return 0; 
} 

void farenheit(int x) 
{ 
    int answer; 
    answer= (x-32)*(float)5/9 ; 
    printf ("Temperature in farenheit %d\n",answer); 
} 

void celcius(int x) 
{ 
    int ans; 
    ans=((float)9/5*x)+32; 
    printf ("Temperature in celcius %d\n",ans); 
} 
+0

非常感謝您的幫助。對不起,反應遲緩。 –