如何在c中打印二次方程的虚根?
How to print the imaginary root of quadratic equation in c?
我需要打印二次方程的虚根。
但是当我执行我的代码时,结果显示虚根是 0.00000i。
即使我用到 也一样。
大家可以帮我看看我加粗的代码吗?
//C program to find the root of the quadratic equation
#include<stdio.h>
#include<math.h>
#include<complex.h>
int main()
{
double a, b, c, x, x1, x2, disc, xr, ximg1, ximg2;
printf("Please enter the value of quadratic equation, a: ");
scanf("%lf", &a);
printf("Please enter the value of quadratic equation, b: ");
scanf("%lf", &b);
printf("Please enter the value of quadratic equation, c: ");
scanf("%lf", &c);
if( a == 0 )
{
x = -(c/b);
printf("\nThis is not a quadratic equation.\n");
printf("x = %.3lf", x);
}
else{
disc = (b*b) - 4*a*c;
if( disc == 0 ){
x1 = -b / 2 * a;
x2 = -b / 2 * a;
printf("x1 = %lf, x2 = %lf", x1, x2);
}
else if(disc > 0){
x1 = ( -b + sqrt( disc ) ) / 2 * a;
x2 = ( -b - sqrt( disc ) ) / 2 * a;
printf("x1 = %.1lf, x2 = %.1lf", x1, x2);
}
else{
ximg1 = sqrt( disc ) / 2 * a;
ximg2 = - sqrt( disc ) / 2 * a;
xr = - b / ( 2 * a );
**printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi", crealf(xr), cimagf(ximg1), cimagf(ximg2));**
}
}
return 0;
}
输出结果如下:
Please enter the value of quadratic equation, a: 4
Please enter the value of quadratic equation, b: 1
Please enter the value of quadratic equation, c: 3
xr = -0.125000, ximg1 = 0.000000i, ximg2 = 0.000000i
Process returned 0 (0x0) execution time : 3.914 s
Press any key to continue.
由于您使用了复杂的头文件,因此您只需要使用 csqrt() 而不是 sqrt(),
//C program to find the root of the quadratic equation
#include<stdio.h>
#include<math.h>
#include<complex.h>
int main()
{
double a, b, c, x, x1, x2, disc, xr, ximg1, ximg2;
printf("Please enter the value of quadratic equation, a: ");
scanf("%lf", &a);
printf("Please enter the value of quadratic equation, b: ");
scanf("%lf", &b);
printf("Please enter the value of quadratic equation, c: ");
scanf("%lf", &c);
if( a == 0 )
{
x = -(c/b);
printf("\nThis is not a quadratic equation.\n");
printf("x = %.3lf", x);
}
else{
disc = (b*b) - 4*a*c;
if( disc == 0 ){
x1 = -b / 2 * a;
x2 = -b / 2 * a;
printf("x1 = %lf, x2 = %lf", x1, x2);
}
else if(disc > 0){
x1 = ( -b + csqrt( disc ) ) / 2 * a;//changed the function here
x2 = ( -b - csqrt( disc ) ) / 2 * a;//changed the function here
printf("x1 = %.1lf, x2 = %.1lf", x1, x2);
}
else{
ximg1 = sqrt( disc ) / 2 * a;
ximg2 = - sqrt( disc ) / 2 * a;
xr = - b / ( 2 * a );
**printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi", crealf(xr), cimagf(ximg1), cimagf(ximg2));**
}
}
return 0;
}
不需要复杂类型
当代码到达下面时,disc < 0
。求否定的平方根。
// ximg1 = sqrt( disc ) / 2 * a;
ximg1 = sqrt( -disc ) / 2 * a; // Use -disc
// ximg2 = - sqrt( disc ) / 2 * a;
ximg2 = -ximg1; // Simply negate ximg1
xr = - b / ( 2 * a );
printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi",
// crealf(xr), cimagf(ximg1), cimagf(ximg2));
xr, ximg1, ximg2);
提示:使用"%e"
,信息量更大。
printf("xr = %le, ximg1 = %lei, ximg2 = %lei",
xr, ximg1, ximg2);
错误
而不是/ 2 * a;
,在很多地方,当然你想要/ (2 * a);
。
您应该使用计算的实部和虚部将根打印为复数。不需要 <complex.h>
也不需要复杂类型:
double xr = - b / ( 2 * a );
double ximg1 = -sqrt( -disc ) / (2 * a);
double ximg2 = -ximg1;
printf("x1 = %lf%+lfi, x2 = %lf%+lfi\n", xr, ximg1, xr, ximg2);
我需要打印二次方程的虚根。
但是当我执行我的代码时,结果显示虚根是 0.00000i。
即使我用到
大家可以帮我看看我加粗的代码吗?
//C program to find the root of the quadratic equation
#include<stdio.h>
#include<math.h>
#include<complex.h>
int main()
{
double a, b, c, x, x1, x2, disc, xr, ximg1, ximg2;
printf("Please enter the value of quadratic equation, a: ");
scanf("%lf", &a);
printf("Please enter the value of quadratic equation, b: ");
scanf("%lf", &b);
printf("Please enter the value of quadratic equation, c: ");
scanf("%lf", &c);
if( a == 0 )
{
x = -(c/b);
printf("\nThis is not a quadratic equation.\n");
printf("x = %.3lf", x);
}
else{
disc = (b*b) - 4*a*c;
if( disc == 0 ){
x1 = -b / 2 * a;
x2 = -b / 2 * a;
printf("x1 = %lf, x2 = %lf", x1, x2);
}
else if(disc > 0){
x1 = ( -b + sqrt( disc ) ) / 2 * a;
x2 = ( -b - sqrt( disc ) ) / 2 * a;
printf("x1 = %.1lf, x2 = %.1lf", x1, x2);
}
else{
ximg1 = sqrt( disc ) / 2 * a;
ximg2 = - sqrt( disc ) / 2 * a;
xr = - b / ( 2 * a );
**printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi", crealf(xr), cimagf(ximg1), cimagf(ximg2));**
}
}
return 0;
}
输出结果如下:
Please enter the value of quadratic equation, a: 4
Please enter the value of quadratic equation, b: 1
Please enter the value of quadratic equation, c: 3
xr = -0.125000, ximg1 = 0.000000i, ximg2 = 0.000000i
Process returned 0 (0x0) execution time : 3.914 s
Press any key to continue.
由于您使用了复杂的头文件,因此您只需要使用 csqrt() 而不是 sqrt(),
//C program to find the root of the quadratic equation
#include<stdio.h>
#include<math.h>
#include<complex.h>
int main()
{
double a, b, c, x, x1, x2, disc, xr, ximg1, ximg2;
printf("Please enter the value of quadratic equation, a: ");
scanf("%lf", &a);
printf("Please enter the value of quadratic equation, b: ");
scanf("%lf", &b);
printf("Please enter the value of quadratic equation, c: ");
scanf("%lf", &c);
if( a == 0 )
{
x = -(c/b);
printf("\nThis is not a quadratic equation.\n");
printf("x = %.3lf", x);
}
else{
disc = (b*b) - 4*a*c;
if( disc == 0 ){
x1 = -b / 2 * a;
x2 = -b / 2 * a;
printf("x1 = %lf, x2 = %lf", x1, x2);
}
else if(disc > 0){
x1 = ( -b + csqrt( disc ) ) / 2 * a;//changed the function here
x2 = ( -b - csqrt( disc ) ) / 2 * a;//changed the function here
printf("x1 = %.1lf, x2 = %.1lf", x1, x2);
}
else{
ximg1 = sqrt( disc ) / 2 * a;
ximg2 = - sqrt( disc ) / 2 * a;
xr = - b / ( 2 * a );
**printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi", crealf(xr), cimagf(ximg1), cimagf(ximg2));**
}
}
return 0;
}
不需要复杂类型
当代码到达下面时,disc < 0
。求否定的平方根。
// ximg1 = sqrt( disc ) / 2 * a;
ximg1 = sqrt( -disc ) / 2 * a; // Use -disc
// ximg2 = - sqrt( disc ) / 2 * a;
ximg2 = -ximg1; // Simply negate ximg1
xr = - b / ( 2 * a );
printf("xr = %lf, ximg1 = %lfi, ximg2 = %lfi",
// crealf(xr), cimagf(ximg1), cimagf(ximg2));
xr, ximg1, ximg2);
提示:使用"%e"
,信息量更大。
printf("xr = %le, ximg1 = %lei, ximg2 = %lei",
xr, ximg1, ximg2);
错误
而不是/ 2 * a;
,在很多地方,当然你想要/ (2 * a);
。
您应该使用计算的实部和虚部将根打印为复数。不需要 <complex.h>
也不需要复杂类型:
double xr = - b / ( 2 * a );
double ximg1 = -sqrt( -disc ) / (2 * a);
double ximg2 = -ximg1;
printf("x1 = %lf%+lfi, x2 = %lf%+lfi\n", xr, ximg1, xr, ximg2);