考虑以下几点:如何打印字符串,而不是浮动用C
#include <stdio.h>
int main()
{
int first=9;
int second=0;
double ratio;
if(second==0)
ratio="n/a";
else
ratio=(double)singularCount/pluralCount;
printf("ratio is: %f", ratio);
return 0;
}
这显然不会运行,因为它应该。
如何在没有问题的情况下修改我的代码/打印语句以打印浮点数,并且如果除以0,则不适用n/a?
我将打印许多在一个列表中这些比值,所以我想看到“N/A”时,有除以0
所需的输出:
ratio is: n/a
这是我想到的一种方式,但我试图避免有2个打印语句。虽然生病使用这种解决方案,还有其他方法吗? – user3487243
@ user3487243你真的不想担心把你的浮点输出转换为字符串(见http://stackoverflow.com/questions/7228438/convert-double-float-to-string),这是一个更好的解。 – Nit