显示虚数的C中的类型

时间:2014-03-22 02:59:28

标签: c types quadratic

我对C中的类型有疑问,我认为它与"真实" ... ...这个程序是一个二次方程求解器,用户输入a,b和c在ax ^ 2 + bx + c = 0方面。我的程序工作,我很抱歉代码中的评论湖,所以我将尝试在我的问题中非常具体。如果你输入说2,2,2,二次方的区别是负的意思是没有真正的答案或"虚数" (好老的代数天)。因此,当您执行此操作时,您会得到类似enter image description here

的内容

发生这种情况的代码中的特定部分: (首先在while循环中)

discriminate = b*b - 4 * a*c;
            if (discriminate < 0)
            {
                root1 = (-b + sqrt(discriminate)) / (2 * a);
                root2 = (-b - sqrt(discriminate)) / (2 * a);
                printf("\nNOTE: Roots are not real.\n");
                printf("The roots are, %.3f, and %.3f\n", root1, root2);
                break;
            }

所以我的问题是两部分。 1)-1.#IO, and -1.#IO是什么意思? (我知道这意味着什么)但是什么是#IO 2)如何正确显示数字?有办法吗?

完整代码:

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

int main(void)
{
    //declarations
    float a;
    float b;
    float c;
    float root1, root2;
    int count;
    float discriminate;

    //Initialization
    count = 0;

    //starting propmts
    printf("\nHello, this program will compute the real roots");
    printf("of a quadratic equation.\n");
    printf("In terms of a(x)^2 + b(x) + c = 0\n");
    printf("\nPlease enter in the \"a\" value: ");
    scanf("%f", &a);
    printf("Please enter in the \"b\" value: ");
    scanf("%f", &b);
    printf("Please enter in the \"c\" value: ");
    scanf("%f", &c);


    while (count == 0)
    {
        if (a == 0)
        {
            if (a == 0 && b == 0)
            {
                printf("There is no soultion...\n");
                break;
            }
            else
            {
                root1 = (-c / b);
                printf("\nNOTE: Input is not quadratic but uesing \"(-c / b)\" ");
                printf("the root is %.3f\n", root1);
                break;
            }   
        }
        else
        {
            discriminate = b*b - 4 * a*c;
            if (discriminate < 0)
            {
                root1 = (-b + sqrt(discriminate)) / (2 * a);
                root2 = (-b - sqrt(discriminate)) / (2 * a);
                printf("\nNOTE: Roots are not real.\n");
                printf("The roots are, %.3f, and %.3f\n", root1, root2);
                break;
            }
            else
            {
                root1 = (-b + sqrt(discriminate)) / (2 * a);
                root2 = (-b - sqrt(discriminate)) / (2 * a);
                if (root1 == root2)
                {
                    printf("The root is, %.3f.\n", root1);
                    break;
                }
                else
                {
                    printf("The roots are, %.3f, and %.3f.\n", root1, root2);
                    break;
                }

            }
        }
    }
    printf("Goodbye.\n");
    return 0;
}

3 个答案:

答案 0 :(得分:1)

如果判别式小于零,那么你还有一些额外的工作要做。使用当前代码,您可以取负数的平方根,结果应该是非数字(NAN)。我不确定为什么printf不会这么说。

要解决问题,您需要取判别式的负数的平方根。然后,您需要计算答案的实部和虚部,并将它们显示为复数。请注意,printf对复数没有任何内置支持,因此您可以自己格式化数字,例如

printf( "%f + %f i", realpart, imagpart );

答案 1 :(得分:1)

如果判别式小于零,那么你有2个复杂的根。如果判别式大于零,那么你有两个真正的根。如果判别式为零,那么你就有一个真正的根。

        if (discriminate < 0)
        {
            float rootr = -b / (2 * a);
            float rooti = sqrt(-discriminate) / (2 * a);
            printf("\nNOTE: Roots are not real.\n");
            printf("The roots are, %.3f + %.3f i, and %.3f - %.3f i\n",
                rootr, rooti,
                rootr, rooti);
            break;
        }
        else if(discriminate > 0)
        {
            float s = sqrt(discriminate);
            float root1 = (-b + s) / (2 * a);
            float root2 = (-b - s) / (2 * a);
            printf("The roots are, %.3f, and %.3f.\n", root1, root2);
            break;
        }
        else
        {
            float root = -b / (2 * a);
            printf("The root is, %.3f.\n", root);
            break;
        }

答案 2 :(得分:1)

enter image description here

我的代码:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
   float a,b,c,d,x,y,i,j;
   clrscr();
   printf("\t\t\t QUADRATIC EQUATION SOLVING\n");
   printf("Enter the co-efficients of x^2,x and constant \n");
   scanf("%f%f%f",&a,&b,&c);
   d=(b*b)-(4*a*c);
   if(d>=0)
   {
      x=(-b+sqrt(d))/(2*a);
      y=(-b-sqrt(d))/(2*a);
      printf("The roots of the equation are %.2f %.2f",x,y);
   }
   else
   {
      d*=-1;
      i=b/(2*a);
      j=sqrt(d)/(2*a);
      printf("The roots are %.2f+%.2fi and %.2f-%.2fi",i,j,i,j);
   }
   getch();
}