求解bool的返回,怎么返回两个数值?
1楼题目是解ax*x+b*x+c=0的解,必须用bool solution (double a,double b,double c,double& x1,double& x2),输入a,b,c求解x1,x2.以下是我的程序
#include <iostream>
#include <cmath>
using namespace std;
bool solution (double a,double b,double c,double x1,double x2)
{
double D,p;
D=b*b-4*a*c;
if (D<0)
{
return false;
}
if (D==0)
{
p=-b/(2*a);
x1=p+sqrt(D)/(2*a);
cout<<"方程有两个相同的实根。"<<endl;
cout<<"x1=x2="<<x1<<endl;
return true;
}
if (D>0)
{
p=-b/(2*a);
x1=p+sqrt(D)/(2*a);
x2=p-sqrt(D)/(2*a);
cout<<"方程有两个不同的实根。"<<endl;
cout<<"x1="<<x1<<endl<<"x2="<<x2<<endl;
return true;}}
int main()
{ double a,b,c,x1,x2;
cin>>a>>b>>c
bool solution (a,b,c,&x1,&x2)
cout<<"请输入3个数:"<<endl;
cout<<"方程为:"<<a<<"x^2+"<<b<<"x+"<<c<<"=0"<<endl;
if (bool=false)
cout<<"方程无解!"<<endl;
if (bool==true)
cout<<"x1="<<x1<<";"<<"x2="<<x2;