1.定义重载函数maxinum,分别求三个整数、三个浮点数和三个字符中最大的一个并输出。
输入样例1:45 67 100 34 2.3 78.2 a A B
输出样例1:100 78.2 a
输入样例2:0 1 2 2.3 3.4 8.0 d r z
输出样例2:2 8.0 z
#include <iostream>
using namespace std;
int maxinum(int b1,int b2)
{
return b1>b2?b1:b2;
}
int maxinum(int b1,int b2,int b3){
int maxinum=b1>b2?b1:b2;
return maxinum>b3?maxinum:b3;
}
float maxinum(float b1,float b2)
{
return b1>b2?b1:b2;
}
float maxinum(float b1,float b2,float b3){
float maxinum=b1>b2?b1:b2;
return maxinum>b3?maxinum:b3;
}
char maxinum(char b1,char b2)
{
return b1>b2?b1:b2;
}
char maxinum(char b1,char b2,char b3){
char maxinum=b1>b2?b1:b2;
return maxinum>b3?maxinum:b3;
}
int main()
{
int b1, b2, b3;
float c1, c2, c3;
char a1, a2, a3;
cin >> b1 >> b2 >> b3 >> c1 >> c2 >> c3>> a1 >> a2 >> a3;
cout << maxinum(b1, b2, b3) << ' ' << maxinum(c1, c2, c3) << ' ' << maxinum(a1, a2, a3);
return 0;
}
2.建立一个名为middle的函数,其功能是返回三个数当中中间那个数的值。重载middle函数3次,分别完成返回3个整数、3个双精度数和3个无符号整数的中间那个数的值。要求这9个数分别存放在3个相应类型的数组中。
#include<iostream>
using namespace std;
int middle(int a[3]) {
if(a[0] > a[1] && a[0] < a[2] || a[0] > a[2] && a[0] < a[1]) {
return a[0];
}
if(a[1] > a[0] && a[1] < a[2] || a[1] > a[2] && a[1] < a[0]) {
return a[1];
}
if(a[2] > a[1] && a[2] < a[0] || a[2] > a[0] && a[2] < a[1]) {
return a[2];
}
}
double middle(double a[3]) {
if(a[0] > a[1] && a[0] < a[2] || a[0] > a[2] && a[0] < a[1]) {
return a[0];
}
if(a[1] > a[0] && a[1] < a[2] || a[1] > a[2] && a[1] < a[0]) {
return a[1];
}
if(a[2] > a[1] && a[2] < a[0] || a[2] > a[0] && a[2] < a[1]) {
return a[2];
}
}
unsigned middle(unsigned a[3]) {
if(a[0] > a[1] && a[0] < a[2] || a[0] > a[2] && a[0] < a[1]) {
return a[0];
}
if(a[1] > a[0] && a[1] < a[2] || a[1] > a[2] && a[1] < a[0]) {
return a[1];
}
if(a[2] > a[1] && a[2] < a[0] || a[2] > a[0] && a[2] < a[1]) {
return a[2];
}
}
int main()
{
int iNumber[3]={45,3,89};
double dNumber[3]={12.4,6,55.8};
unsigned uNumber[3]={345,776,565};
cout<<"\nThe numbers are ";
for( int i=0;i<3;i++)
cout<<iNumber[i]<<"\t";
cout<<endl<<"the middle of them is "<<middle(iNumber)<<endl;
cout<<"\nThe numbers are ";
for( int i=0;i<3;i++)
cout<<dNumber[i]<<"\t";
cout<<endl<<"the middle of them is "<<middle(dNumber)<<endl;
cout<<"\nThe numbers are ";
for( int i=0;i<3;i++)
cout<<uNumber[i]<<"\t";
cout<<endl<<"the middle of them is "<<middle(uNumber)<<endl;
return 0;
}