使用函数和while循环重新编写温度转换程序
时间: 2023-04-02 21:02:44 浏览: 51
好的,我可以回答这个问题。以下是使用函数和while循环重新编写的温度转换程序:
#include <stdio.h>
float celsius_to_fahrenheit(float celsius) {
return celsius * 9 / 5 + 32;
}
float fahrenheit_to_celsius(float fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
int main() {
int choice;
float temperature;
while (1) {
printf("请选择转换方式:\n");
printf("1. 摄氏度转华氏度\n");
printf("2. 华氏度转摄氏度\n");
printf("3. 退出程序\n");
scanf("%d", &choice);
if (choice == 1) {
printf("请输入摄氏度温度:");
scanf("%f", &temperature);
printf("%.2f 摄氏度 = %.2f 华氏度\n", temperature, celsius_to_fahrenheit(temperature));
} else if (choice == 2) {
printf("请输入华氏度温度:");
scanf("%f", &temperature);
printf("%.2f 华氏度 = %.2f 摄氏度\n", temperature, fahrenheit_to_celsius(temperature));
} else if (choice == 3) {
break;
} else {
printf("无效的选择,请重新输入\n");
}
}
return ;
}