Консультация № 177894
17.04.2010, 15:31
0.00 руб.
0 3 3
Здравствуйте, помогите пожалуйста оформить и решить следующую задачу: С помощью механизма перегрузки функций реализовать функции для нахождения максимального из:
1) 2-х чисел;
2) длин 2-х строк;
3) длины строки и числа;
4) числа и длины строки.
Пункты разбиваются на отдельные решения, т.е., можно просто блоками коментировать. Заранее огромное СПАСИБО!

Обсуждение

Неизвестный
17.04.2010, 20:58
общий
это ответ
Здравствуйте, Викол Василий.

Решение в приложении. Не помню, как использовать в консольном приложении русский шрифт, поэтому написал на английском. Так как задачей был поиск максимума, вариант совпадения длин, чисел и тп не рассматривается.

Приложение:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

bool comp(unsigned int num1, unsigned int num2)
{
if (num1 > num2) return true;
else return false;
}

bool comp(string str1, string str2)
{
if (str1.length() > str2.length()) return true;
else return false;
}

bool comp(string str, unsigned int num)
{
if (str.length() > num) return true;
else return false;
}

bool comp(unsigned int num, string str)
{
if (num > str.length()) return true;
else return false;
}

int _tmain(int argc, _TCHAR* argv[])
{
string str1, str2;
unsigned int num1, num2;
char choice;

cout << "Input first string: ";
cin >> str1;
cout << "Input second string: ";
cin >> str2;
cout << "Input first positive number: ";
cin >> num1;
cout << "Input second positive number: ";
cin >> num2;

do {
cout << endl << "Select comparison method:" << endl;
cout << "1 - Compare two numbers" << endl;
cout << "2 - Compare the lengths of two strings" << endl;
cout << "3 - Compare the length of first string and second number" << endl;
cout << "4 - Compare first number and the length of second string" << endl;
cin >> choice;
}
while ((choice < '1') || (choice > '4'));

cout << endl;
switch (choice) {
case '1':
if (comp(num1 , num2)) cout << "First number is bigger than second";
else cout << "Second number is bigger than first"; break;
case '2':
if (comp(str1 , str2)) cout << "First string is longer than second";
else cout << "Second string is longer than first"; break;
case '3':
if (comp(str1 , num2)) cout << "The length of first string is bigger than second number";
else cout << "Second number is bigger than the length of first string"; break;
case '4':
if (comp(num1 , str2)) cout << "First number is bigger than the length of second string";
else cout << "The length of second string is bigger than first number"; break;
}

_getch();
return 0;
}
давно
Академик
320937
2216
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;
}
Неизвестный
18.04.2010, 12:05
общий
это ответ
Здравствуйте, Викол Василий.

предложу вариант с строками типа char*



Приложение:
#include <iostream>
#include <conio.h>

using namespace std;

int MAX(const int a,const int b)
{
return (a>b) ? a : b;
}

int MAX(const char *s1,const char *s2)
{
int l1=strlen(s1);
int l2=strlen(s2);
return (l1>l2) ? l1 : l2;
}

int MAX(const char *s1,const int n)
{
int l1=strlen(s1);
return (l1>n) ? l1 : n;
}

int MAX(const int n,const char *s1)
{
int l1=strlen(s1);
return (l1>n) ? l1 : n;
}

int main()
{
char str1[256],str2[256];
int n1,n2;

cout<<"n1 = ";
cin>>n1;
cout<<"n2 = ";
cin>>n2;
cout<<"str1 = ";
cin>>str1;
cout<<"str2 = ";
cin>>str2;

cout<<"\nMaximum from n1 and n2 is "<<MAX(n1,n2);
cout<<"\nMaximum from strlen(s1) and strlen(s2) is "<<MAX(str1,str2);
cout<<"\nMaximum from strlen(str1) and n1 is "<<MAX(str1,n1);
cout<<"\nMaximum from strlen(str1) and n2 is "<<MAX(str1,n2);
cout<<"\nMaximum from strlen(str2) and n1 is "<<MAX(str2,n1);
cout<<"\nMaximum from strlen(str2) and n2 is "<<MAX(str2,n2);

_getch();
}
Форма ответа