int Distance() {
char from[40], to[40];
double val;
double result;
printf("Enter what you want to convert from: ");
fflush(stdin);
fgets(from, 40, stdin);
printf("Enter what you want to convert to: \n");
fflush(stdin);
fgets(to, 40, stdin);
printf("Enter the numerical value of conversion: \n");
scanf("%lf", &val);
(strcmp(from, "cm") == 0 && strcmp(to, "mm") == 0) ? printf("Answer is %lf MilliMeters", val / 10) :
(strcmp(from, "mm") == 0 && strcmp(to, "cm") == 0) ? printf("Answer is %lf CentiMeters", val * 10) :
(strcmp(from, "cm") == 0 && strcmp(to, "km") == 0) ? printf("Answer if %lf KiloMeters", val * 100000) :
(strcmp(from, "km") == 0 && strcmp(to, "cm") == 0) ? printf("Answer is %lf CentiMeters", val / 100000) :
(strcmp(from, "mm") == 0 && strcmp(to, "km") == 0) ? printf("Anser is %lf KiloMeters", val * 1000000) :
(strcmp(from, "km") == 0 && strcmp(to, "mm") == 0) ? printf("Answer is %lf MilliMeters", val / 1000000) :
printf("Please enter valid conversion units");
}
我正在试着做一个单位转换器.所以我为不同的转换做了不同的函数--比如int distance()
,
int time()
等等.
在我的计算距离的函数中,我使用fgets()
来获得多个字符的输入,如‘cm’、‘mm’等.
我想要实现的是
如果char from
中fgets
的值是"任意字符串",char to
中的fgets
是"任何其他字符串":打印结果,其公式是-(Something)
它不计算结果,直接跳到最后printf("Please enter valid conversion units")
我还在初学阶段,可能是个小错误,请帮帮忙.