读取二次方程ax^2+bx+c的系数a、b、c,并将其很好地打印为x+iy形式的虚根打印

  • 本文关键字:打印 很好 x+iy 2+bx+c ax 二次方程 读取 c
  • 更新时间 :
  • 英文 :

#include <math.h>
#include <stdio.h>
main() {
int a, b, c, x, x1, x2;
printf("enter the values of a,b,c:");
scanf("%d%d%d", &a, &b, &c);
printf("The quadratic equation is %d*pow(x,2)+%d*x+%d=0", a, b, c);
if (pow(b, 2) - 4 * a * c >= 0) {
x1 = (-b + sqrt(pow(b, 2) - 4 * a * c)) / 2 * a;
x2 = (-b - sqrt(pow(b, 2) - 4 * a * c)) / 2 * a;
printf("the roots of the equation are x1=%d,x2=%d", x1, x2);
}
else
printf("roots of the equation in the form of x+iy and x-iy");
return 0;
}

对于给定的问题,这个代码可以吗?我对打印假想根有点困惑。你能帮吗

  1. 使用正确的main原型
  2. 使用浮点数而不是整数
  3. pow(b,2(==b*b
  4. / 2 * a->/ (2 * a)
int main(void) {
double a, b, c, x, x1, x2;
printf("enter the values of a,b,c:");
if(scanf("%lf %lf %lf", &a, &b, &c) != 3) { /* handle error */}
printf("nThe quadratic equation is %f*x^2+%f*x+%f=0n", a, b, c);
if (b*b - 4 * a * c >= 0) {
x1 = (-b + sqrt(b*b - 4 * a * c)) / (2 * a);
x2 = (-b - sqrt(b*b - 4 * a * c)) / (2 * a);
printf("the roots of the equation are x1=%f,x2=%fn", x1, x2);
}
else
{
double r = -b / (2*a);
double z = sqrt(fabs(b*b - 4 * a * c));
printf("the roots of the equation are x1 = %f + i%f, x2 = %f - i%f", r,z,r,z);
}
}

https://gcc.godbolt.org/z/Ys1s8bWY7

相关内容

最新更新