gpt4 book ai didi

c - 为什么C程序的输出是错误的?我查了好久

转载 作者:行者123 更新时间:2023-11-30 21:44:40 24 4
gpt4 key购买 nike

这是我写的C源文件,运行后,我输入 1 1 -2 1 ,但输出不是x1=1.000000,x2=1.000000?输出为 x1=2.000000,x2=2.000000 。

#include <stdio.h>
#include <math.h>

void fun(float a, float b, float c);

int main()
{
int n,i;
float a, b, c;

printf("please enter the times you want to try:\n");

scanf("%d", &n);

for (i = 0; i < n; i++) {
printf("please enter the a,b,c:\n");
scanf("%f%f%f", &a, &b, &c);
fun(a, b, c);
}
return 0;
}

void fun(float d, float e, float f) {
float x1, x2;

if (e*e - 4 * d*f < 0)
printf("no answer\n");
if (e*e - 4 * d*f >= 0) {
x1 = (-e + sqrt(e*e - 4 * d*f)) / (2 * d); //should work better
x2 = (-e - sqrt(e*e - 4 * d*f)) / (2 * d);
printf("the roots are x1=%f x2=%f\n", x1, x2);
}
}

最佳答案

x1 = (-e + sqrt(e*e - 4 * d*f)) / (2 * d);
x2 = (-e - sqrt(e*e - 4 * d*f)) / (2 * d);

关于c - 为什么C程序的输出是错误的?我查了好久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43335800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com