c - C 中显示虚数的类型

标签 c types quadratic

我对 C 中的类型有疑问,我认为它与“实数”有关...该程序是一个二次方程求解器,用户输入 a、b 和 c,表示为 ax^2 + bx + c = 0。我的程序可以运行,我很抱歉代码中有很多评论,所以我会尽量在我的问题中非常具体。如果你输入 2, 2, 2 二次方的判别式是负的,这意味着没有真正的答案或“虚数”(哦,古老的代数时代)。所以当你这样做时,你会得到这样的东西 enter image description here

发生这种情况的代码中的特定部分: (while 循环中的第一个 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;
            }

所以我的问题分为两个部分。 1) -1.#IO 和 -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;
}

最佳答案

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

关于c - C 中显示虚数的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22572893/

相关文章:

c++ - QVector 和 std::vector 的大小方法

ruby-on-rails - Ruby/Rails ActiveRecord 属性查找 bool 值

c++ - 二次方程程序输出数字

java - 二次方的导数

c - 没有 malloc 或 calloc 的 free() 函数

c - f_open 变量中的目录名

c - 短路逻辑或行为

c - 两个 unsigned long 表示之间的区别

sql-server - Varchar 列 : Nullable or not

python - 为什么 SymPy 无法求解具有复杂系数的二次方程