【问题标题】:How to print the imaginary root of quadratic equation in c?如何在c中打印二次方程的虚根?
【发布时间】:2022-01-12 13:29:42
【问题描述】:

我需要打印二次方程的虚根。 但是当我执行我的代码时,结果显示虚根是 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()(原型在&lt;complex.h&gt;
  • 不要取负数的 sqrt()。你的意思可能是 sqrt(-disc)
  • 请不要有文字图片

标签: c complex-numbers


【解决方案1】:

您应该使用计算的实部和虚部将根打印为复数。不需要&lt;complex.h&gt; 也不需要复杂类型:

    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);

【讨论】:

    【解决方案2】:

    不需要复杂类型

    当代码到达下面时,disc &lt; 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);

    【讨论】:

    • 非常感谢!我成功地解决了我的问题。我将尝试如何改进我的代码并使其更高效。
    • 我也了解到可以去掉因为不需要复杂类型,tqsm!!!
    【解决方案3】:

    由于您使用了复杂的头文件,您只需要使用 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      相关资源
      最近更新 更多