伙计们,我正在尝试制作一个可以考虑二次方程的代码,但这仅在方程为正时才有效。这是代码。请帮忙
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
int main()
{
double a,b,c,ac,n,m;
cout<<"Enter a: ";cin>>a;cout<<endl;
cout<<"Enter b: ";cin>>b;cout<<endl;
cout<<"Enter c: ";cin>>c;cout<<endl;
ac = a*c;
for(n=0;n<=ac;n=n+2)
{
m= ac/n;
if(n+m==b&&n*m==c)
{
cout<<"(x + "<<n<<") (x + "<<m<<")"<<endl;
break;
}
}
return 0;
}
答案 0 :(得分:1)
在@Aditi Rawat所说的基础上,我写了一个小程序,使用二次根公式完成所需的工作。不确定你是否需要这样做,但这解决了问题:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a, b, c;
cout<<"Enter a: "; cin>>a; cout<<endl;
cout<<"Enter b: "; cin>>b; cout<<endl;
cout<<"Enter c: "; cin>>c; cout<<endl;
double x_positive, x_negative;
if((pow(b,2) - 4*a*c) >= 0) {
x_positive = (-b + sqrt(pow(b,2) - 4*a*c)) / 2*a;
x_negative = (-b - sqrt(pow(b,2) - 4*a*c)) / 2*a;
cout << "Root 1: " << x_positive << endl;
cout << "Root 2: " << x_negative << endl;
} else cout << "Roots are complex" << endl;
return 0;
}