題組內容
三、
⑵攝氏溫度(Celsius)零度等於華氏溫度(Fahrenheit)32 度,攝氏刻度 1 度等於
華氏 1.8 度。以 C 語言設計函式 CelsiustoFahrenheit()將輸入之攝氏溫度轉換為華
氏溫度傳回。(10 分) float CelsiustoFahrenheit(int tc) { ⋯ }
詳解 (共 1 筆)
詳解
#include <stdio.h>
// 函式宣告
float CelsiustoFahrenheit(int tc);
float CelsiustoFahrenheit(int tc);
int main() {
int tc;
float tf;
int tc;
float tf;
// 提示使用者輸入攝氏溫度
printf("請輸入攝氏溫度: ");
scanf("%d", &tc);
printf("請輸入攝氏溫度: ");
scanf("%d", &tc);
// 調用函數轉換溫度並列印結果
tf = CelsiustoFahrenheit(tc);
printf("%d 攝氏度等於 %.2f 華氏度。\n", tc, tf);
tf = CelsiustoFahrenheit(tc);
printf("%d 攝氏度等於 %.2f 華氏度。\n", tc, tf);
return 0;
}
}
// 函式定義
float CelsiustoFahrenheit(int tc) {
return tc * 1.8 + 32;
}
這段程式首先定義了CelsiustoFahrenheit函數,用於實現攝氏溫度到華氏溫度的轉換。然後,在main函數中,程式提示使用者輸入攝氏溫度值,接收這個值並存儲在變數tc中。之後,程式調用CelsiustoFahrenheit函數將攝氏溫度轉換為華氏溫度,並將轉換後的華氏溫度值存儲在變數tf中。最後,程式列印出攝氏溫度和對應的華氏溫度。
float CelsiustoFahrenheit(int tc) {
return tc * 1.8 + 32;
}
這段程式首先定義了CelsiustoFahrenheit函數,用於實現攝氏溫度到華氏溫度的轉換。然後,在main函數中,程式提示使用者輸入攝氏溫度值,接收這個值並存儲在變數tc中。之後,程式調用CelsiustoFahrenheit函數將攝氏溫度轉換為華氏溫度,並將轉換後的華氏溫度值存儲在變數tf中。最後,程式列印出攝氏溫度和對應的華氏溫度。