CHAPTER3-7.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:1k
- //文件名:CHAPTER3-7.cpp
- #include <iostream.h>
- #include <string.h>
- // 定义函数模板,找出三个值中最小的值,与数据类型无关
- template <class T>
- T min(T ii, T jj, T kk)
- {
- T temp;
- if((ii<jj)&&(ii<kk)){ temp=ii; }
- else if((jj<ii)&&(jj<kk)){ temp=jj; }
- else { temp=kk; }
- return temp;
- }
- //非模板函数重载
- const char* min(const char* ch1, const char* ch2,const char* ch3)
- {
- const char* temp;
- int result1 = strcmp(ch1,ch2);
- int result2 = strcmp(ch1,ch3);
- int result3 = strcmp(ch2,ch1);
- int result4 = strcmp(ch2,ch3);
- if((result1<0)&&(result2<0)) { temp = ch1; }
- else if((result3<0)&&(result4<0)) { temp=ch2; }
- else { temp=ch3; }
- return temp;
- }
- // 下面是主函数
- void main()
- {
- cout<<min(100,20,30)<<endl;
- cout<<min(10.60,10.64,53.21)<<endl;
- cout<<min('c','a','C')<<endl;
- cout<<min("Anderson","Washington","Smith")<<endl;
- }