编程论坛
注册
登录
编程论坛
→
C++教室
求解二次方程的解 要求调用函数 请帮个忙
lindayanglong
发布于 2008-07-24 21:11, 849 次点击
求a*x*x+b*x+c=0的解 要求调用函数
void findroot(double a,double b,double c);
由于返回的值可能没有 可能有一个解 可能有两个解,所以本人不知道该怎么调用函数了,请大家帮忙
2 回复
#2
zjl138
2008-07-24 22:13
你测试一下:
/*****************************************************************
** HighlightCodeV3.0 software by yzfy(雨中飞燕) http:// **
*****************************************************************/
#include<iostream>
#include<cmath>
void
findroot
(
double
a,
double
b,
double
c)
{
double
disc = b*b -
4
*a*c;
if
(a==
0
)
std
::
cout
<<
"x="
<<-(c/b)<<
std
::
endl
;
else if
(disc==
0
)
std
::
cout
<<
"x1=x2="
<<(-b)/(
2
*a)<<
std
::
endl
;
else if
(disc>
0
)
std
::
cout
<<
"x1="
<<((-b)+
sqrt
(disc))/(
2
*a)<<
"\t"
<<
"x2="
<<((-b)-
sqrt
(disc))/(
2
*a)<<
std
::
endl
;
else if
(disc<
0
)
std
::
cout
<<
"x1="
<<(-b)/(
2
*a)<<
"+"
<<
sqrt
(-b)/(
2
*a)<<
"i"
<<
"\t"
<<
"x2="
<<(-b)/(
2
*a)<<
"-"
<<
sqrt
(-b)/(
2
*a)<<
"i"
<<
std
::
endl
;
}
int
main
(
void
)
{
double
a,b,c;
std
::
cin
>>a>>b>>c;
findroot
(a,b,c);
system
(
"pause"
);
return
0
;
}
#3
lindayanglong
2008-07-30 15:32
谢谢楼上的
1