我用C语言写了个程序 不知道为什么当输入整数的时候没结果 输入负数的时候 可以算出结果

2025-02-23 19:47:42
推荐回答(2个)
回答1:

double型的数据输入必须用格式符"%lf",输出时可以用"%lf",也可以用%f。
printf("f(%.2f)=%.2\n",x,sqrt(x));这一句中少了个f,应为%.2f。

回答2:

/*
Enter x : 12
f(12.00) = 3.46
Press any key to continue
*/
#include
#include
void main() {
double x;
printf("Enter x : ");
scanf("%lf",&x);
if(x < 0) printf("f(%.2lf) = %.2lf\n",x,pow((x + 1),2) + 2 * x + 1/x);
else printf("f(%.2lf) = %.2lf\n",x,sqrt(x));
}