从键盘输入多个浮点数

Multiple floating point input from keyboard

我正在从单个 scanf 输出中读取多个浮点数。

#include<stdio.h>
#include<math.h>
int main()
{ float a,b,c;
  float d,rot1,rot2;
  scanf("%f%f%f ", &a, &b, &c);
  d = sqrt(b*b - 4*a*c);
  rot1 = (-b-d)/(2*a);
  rot2 = (-b+d)/(2*a);
  printf("%f %f", rot1, rot2);
  return 0;
}

需要4个输入;但是,我只想为变量 a、b 和 c 的地址取 3 个输入。我不知道它从哪个变量中获取额外的输入。当我编写用于为 2 个变量获取输入的代码时,它需要 3 个输入。

scanf("%f%f%f ", &a, &b, &c);
             ^

%f 之后删除这个额外的 space。

注意 - 您应该确保 sqrt 中的表达式不会计算为负数。您可能想避免这种情况。