program_5_1.cpp
来自「清华关于C++ 的程序讲义 值得一看 关于算法」· C++ 代码 · 共 25 行
CPP
25 行
// Program 5.1: Determines roots of a quadratic equation
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << "Coefficients for quadratic equation: ";
double a;
double b;
double c;
cin >> a >> b >> c;
if ((a != 0) && ((b*b - 4*a*c) > 0)) {
double radical = sqrt(b*b - 4*a*c);
double root1 = (-b + radical) / (2*a);
double root2 = (-b - radical) / (2*a);
cout << "The roots of " << a << "x^2 + " << b
<< "x + " << c << " are:" << endl << root1 << " and "
<< root2 << endl;
}
else {
cout << a << "x^2 + " << b << "x + " << c
<< " does not have two real roots" << endl;
}
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?