17.04.2010, 22:13
общий
это ответ
Здравствуйте, Викол Василий. Ответ в приложении. CodeBlocks/G++
Приложение:
// С помощью механизма перегрузки функций реализовать функции
// для нахождения максимального из:
// 1) 2-х чисел;
// 2) длин 2-х строк;
// 3) длины строки и числа;
// 4) числа и длины строки.
#include <iostream>
#include <string>
using namespace std;
unsigned int max(const unsigned int num1, const unsigned int num2);
unsigned int max(const string& str1, const string& str2);
unsigned int max(const string& str, const unsigned int num);
unsigned int max(const unsigned int num, const string& str);
int main()
{
string str1, str2;
unsigned int num1, num2;
cout << "Первая строка: ";
getline(cin, str1);
cout << "Вторая строка: ";
getline(cin, str2);
cout << "Первое число: ";
cin >> num1;
cout << "Второе число: ";
cin >> num2;
cout << endl;
cout << "max(" << num1 << "," << num2 << ")=" << max(num1,num2) << endl;
cout << "max(length(" << str1 << "), length(" << str2 << "))=" << max(str1,str2) << endl;
cout << "max(length(" << str1 << "), " << num2 << "))=" << max(str1,num2) << endl;
cout << "max(" << num1 << ", length(" << str2 << "))=" << max(num1,str2) << endl;
system("Pause");
return 0;
}
unsigned int max(const unsigned int num1, const unsigned int num2)
{
return (num1 > num2) ? num1 : num2;
}
unsigned int max(const string& str, const unsigned int num)
{
unsigned int len = str.length();
return (len > num) ? len : num;
}
unsigned int max(const string& str1, const string& str2)
{
unsigned int len1 = str1.length();
unsigned int len2 = str2.length();
return (len1 > len2) ? len1 : len2;
}
unsigned int max(const unsigned int num, const string& str)
{
unsigned int len = str.length();
return (num > len) ? num : len;
}