gpt4 book ai didi

c - C 中显示虚数的类型

转载 作者:太空宇宙 更新时间:2023-11-04 02:04:20 24 4
gpt4 key购买 nike

我对 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 是什么意思? (我知道这是什么意思)但是什么是 #IO2)如何正确显示号码?有办法吗?

完整代码:

#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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com