应该怎么写这三种的转化,选择输入,转化为另两个数据不是很懂这种的,怎么写,有没有,求啊
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_INPUT_LENGTH 10
#define ERROR_MESSAGE "输入的温度格式不正确,请重新输入"
int main() {
char input[MAX_INPUT_LENGTH];
double temperature;
printf("请输入一个温度值,格式如下:\n");
printf("[数字][单位],其中单位为 F/C/K,例如:32F、100C、273.15K\n");
fgets(input, MAX_INPUT_LENGTH, stdin);
if (sscanf(input, "%lf", &temperature) != 1) {
printf(ERROR_MESSAGE);
return 1;
}
char unit[2];
if (sscanf(input, "%*f%1s", unit) != 1) {
printf(ERROR_MESSAGE);
return 1;
}
double celsius, fahrenheit, kelvin;
switch (tolower(unit[0])) {
case 'c':
celsius = temperature;
fahrenheit = celsius * 1.8 + 32;
kelvin = celsius + 273.15;
break;
case 'f':
fahrenheit = temperature;
celsius = (fahrenheit - 32) / 1.8;
kelvin = (fahrenheit + 459.67) / 1.8;
break;
case 'k':
kelvin = temperature;
celsius = kelvin - 273.15;
fahrenheit = kelvin * 1.8 - 459.67;
break;
default:
printf(ERROR_MESSAGE);
return 1;
}
printf("%.2lf摄氏度 = %.2lf华氏度 = %.2lf开尔文度\n", celsius, fahrenheit, kelvin);
return 0;
}