『代码』··By/蜜汁炒酸奶

函数模板之返回形参的绝对值—C++

/*
功能:编写一个函数模板来返回形参的绝对值
作者:wins
日期:2013-12-11
*/
#include <iostream>
using namespace std;
template<typename T>
T Facs(T a)
{
	if (a<0)
	{
		return -a;
	}
	else
	{
		return a;
	}
}
int main(void)
{

	double nu1,nu2;
	int nu3,nu4;
	nu1 = 2.23;
	nu2 = -2.45;
	nu3 = 5;
	nu4 = -9;

	cout<< Facs(nu1)<<endl;
	cout<< Facs(nu2)<<endl;
	cout<< Facs(nu3)<<endl;
	cout<< Facs(nu4)<<endl;



	system("pause");
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

预览
Loading comments...
0 条评论

暂无数据

example
预览