首页 > 试题广场 >

编写一个C ++风格的程序,建立一个被称为sroot()的函

[问答题]
编写一个C ++风格的程序,建立一个被称为sroot()的函数,返回其参数的一次方根。重载sroot( ) 3次·让它返回整数、长整数与双精度数的二次方根(计算二次方根时,可以使用标准库函数sqrt())。

推荐
实现本题功能的程序如下:
#include<iostream> 
# include< cmath> 
using namespace std; 
double sroot(int i)
{ return sqrt(i);
}
double sroot (long 1) 
{ return sqrt (1);
}
double sroot (double d) 
{ return sqrt (d) ;
}
int main ( )
{ int i=12;
 long l=1234;
 double d=12.34;
 cout<<"i 的二次方根是:"<<sroot(i)<<endl;
 cout<<"l 的二次方根是:"<<sroot(l)<<endl;
 cout<<"d 的二次方根是:"<<sroot(d)<<endl;
return 0;
}

发表于 2018-05-05 21:15:58 回复(0)