C++建立重载函数sroot( )


编写一个C++风格的程序,建立重载函数sroot( ),让它返回整数、长整数与双精度参数的立方,保留8位有效数字。(实数保留有效数字位数建议使用操纵符setprecision(8),头文件iomanip,使用方法网上查看相关文档)。用cout输出时系统自动会根据数据大小选择小数或指数形式输出。


代码如图

#include<iostream>
#include<iomanip>
using namespace std;
double sroot(int a) 
{
	return(a * a * a);
}
double sroot(long a)
{
	return(a * a * a);
}
double sroot(double a)
{
	return(a * a * a);
}
int main()
{
	int a;
	long b;
	double c;
	cin >> a;
	cin >> b;
	cin >> c;
	cout << a << "的立方是:" << setprecision(8) << sroot(a)<<endl;
	cout << b << "的立方是:" << setprecision(8) << sroot(b)<<endl;
	cout << c << "的立方是:" << setprecision(8) << sroot(c)<<endl;
	system("pause");
	return 0;
}

运行结果:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_48106407/article/details/108457014