c++二次方程代码输出错误
c++ quadratic equation code output error
好的,所以我在c++中创建了一个二次方程求解器,似乎不能在虚数上得到正确的输出。实数根很好(例如x= 2和x= 5),但当虚数出来时,奇怪的事情发生了(x = -1.#IND)?有人能帮我弄明白吗?我想让它更像x = 5.287* I。这是我的代码
#include <iostream>
#include <string>
#include <cmath>
#include <complex>
using namespace std;
int main() {
cout << "Project 4 (QUADRATIC EQUATION)nLong Beach City College nAuthor: Mathias Pettersson nJuly 15, 2015n" << endl;
cout << "This program will provide solutions for trinomial expressions.nEXAMPLE: A*x^2 + B*x^2 + C = 0" << endl;
double a, b, c;
double discriminant;
//Variable Inputs
cout << "Enter the value of a: ";
cin >> a;
cout << "Enter the value of b: ";
cin >> b;
cout << "Enter the value of c: ";
cin >> c;
//Computations
discriminant = (b*b) - (4 * a * c);
double x1 = (((-b) + sqrt(discriminant)) / (2 * a));
double x2 = (((-b) - sqrt(discriminant)) / (2 * a));
//Output
if (discriminant == 0)
{
cout << "The discriminant is ";
cout << discriminant << endl;
cout << "The equation has a single root.n";
}
else if (discriminant < 0)
{
cout << "The discriminant is ";
cout << discriminant << endl;
cout << "The equation has two complex roots.n";
cout << "The roots of the quadratic equation are x = " << x1 << "*i, and" << x2 << "*i" << endl;
}
else
{
cout << "The discriminant is ";
cout << discriminant << endl;
cout << "The equation has two real roots.n";
}
//Final Root Values
cout << "The roots of the quadratic equation are x = ";
cout << x1;
cout << ", ";
cout << x2 << endl << endl;
system("PAUSE");
return 0;
}
double
不表示复数。
将double
传递给sqrt
:
sqrt(discriminant)
传递一个复数来得到一个复数结果:
sqrt(std::complex<double>(discriminant))
相关文章:
- 显示错误输出的简单数组排序程序
- Dijkstra 中的错误输出
- 自定义runtime_error,如果我在 #what 中使用#c_str(),则错误输出始终为空
- 流字符串错误输出
- C++ 异常处理错误输出
- C ++中的错误输出,i = 65530,当我们打印i的值时,它给出-6状态65530
- SSE和iostream:浮点类型的错误输出
- C++将 popen 错误输出读取为字符串
- 部分透视/高斯消除 - 交换列而不是产生错误输出的行
- C++,向量:获得包含 0 的错误输出
- 数组函数错误输出
- CC1plus 错误:输出文件名指定两次
- 字符串错误输出
- 子斯特的错误输出
- 对大量c++错误输出完全一无所知.我认为这与预定义的函数和迭代器有关
- 简单程序的错误输出
- XCode调试器lldb中断,没有错误输出,但程序运行良好
- std::string::append(std::string)错误输出
- 查看大错误输出的顶行
- 使用结构的错误输出