#include <iostream>
#include <ctime>
#include <math.h>
#include <cctype>
#include <string>
#include <malloc.h>
#include <cwctype>
#include <vector>
#include <chrono>
#include <stdlib.h>
#include <locale>
#include <ctime>
#ifdef _WIN32
#include <windows.h>
#include <stdio.h>
#include <stddef.h>
#include <io.h>
#include <fcntl.h>
#include <stringapiset.h>
#endif
std::wstring readWStringFromConsole()
{
std::wstring wstr;
bool isFailed = false;
int tryCount = 4;
do
{
try
{
std::fflush(NULL);
std::wcin.sync();
std::wcin.ignore(std::wcin.rdbuf()->in_avail());
}
catch (...) {}
const std::chrono::steady_clock::time_point timeStart = std::chrono::steady_clock::now();
std::getline(std::wcin, wstr);
const std::chrono::steady_clock::time_point timeEnd = std::chrono::steady_clock::now();
isFailed = std::chrono::duration_cast<std::chrono::milliseconds>(timeEnd - timeStart).count() <= 10;
if (tryCount <= 0)
{
std::wcout << L"Ошибка: не удалось ввести строку из консоли." << std::endl;
break;
}
--tryCount;
} while (isFailed && wstr.empty());
std::fflush(NULL);
return wstr;
}
void waitForAnyKey()
{
std::fflush(NULL);
#ifdef _WIN32
system("pause");
#else
system("bash -c \'read -n 1 -s -r -p \"Нажмите любую клавишу чтобы продолжить...\"\'");
#endif
}
void clearConsole()
{
std::fflush(NULL);
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
class Human
{
public:
Human() : m_name(L"Роман"), m_age(25){}
Human(std::wstring name, int age) : m_name(name), m_age(age){}
virtual ~Human(){}
const std::wstring &name() const;
const int &age() const;
const std::wstring narration() const;
virtual const std::wstring greeting(const Human &h1);
virtual const std::wstring typeOfGroup() const;
protected:
std::wstring m_name;
int m_age;
};
const std::wstring Human::narration() const
{
std::wstring ending;
if(m_age % 10 == 1)
{
ending = L" год";
}
else if(m_age % 10 >= 2 && m_age % 10 <= 4)
{
ending = L" года";
}
else if(m_age % 10 >= 5)
{
ending = L" лет";
}
else
{
ending = L" лет";
}
return L"Меня зовут "+m_name+L", мой возраст "+std::to_wstring(m_age)+ending+L" и я "+typeOfGroup()+L".\n";
}
const std::wstring &Human::name() const
{
return m_name;
}
const int &Human::age() const
{
return m_age;
}
const std::wstring Human::greeting(const Human &h1)
{
return L"Здравствуйте, "+h1.name()+L"!\n";
}
const std::wstring Human::typeOfGroup() const
{
return L"человек";
}
class Informal : public Human
{
public:
Informal() : Human() {}
Informal(std::wstring name, int age) : Human(name, age) {}
const std::wstring greeting(const Human &h1) override;
const std::wstring typeOfGroup() const override;
protected:
};
const std::wstring Informal::greeting(const Human &h1)
{
return L"Привет, "+h1.name()+L"!\n";
}
const std::wstring Informal::typeOfGroup() const
{
return L"неформал";
}
class Formal : public Human
{
public:
Formal() : Human() {}
Formal(std::wstring name, int age) : Human(name, age) {}
const std::wstring greeting(const Human &h1) override;
const std::wstring typeOfGroup() const override;
protected:
};
const std::wstring Formal::greeting(const Human &h1)
{
return L"Здравствуйте, "+h1.name()+L"!\n";
}
const std::wstring Formal::typeOfGroup() const
{
return L"формалист";
}
class Realist : public Human
{
public:
Realist() : Human() {}
Realist(std::wstring name, int age) : Human(name, age) {}
const std::wstring greeting(const Human &h1) override;
const std::wstring typeOfGroup() const override;
protected:
};
const std::wstring Realist::greeting(const Human &h1)
{
if(h1.age() > m_age + 5)
{
return L"Здравствуйте, "+h1.name()+L"!\n";
}
return L"Привет, "+h1.name()+L"!\n";
}
const std::wstring Realist::typeOfGroup() const
{
return L"реалист";
}
int main()
{
#ifdef _WIN32
setlocale(LC_ALL, "");
_setmode(_fileno(stdout), _O_U16TEXT);
_setmode(_fileno(stderr), _O_U16TEXT);
_setmode(_fileno(stdin), _O_U16TEXT);
#else
//Т.к. ввели автотранслитерацию с русского на английский,
//возвращаем все обратно;
std::ios_base::sync_with_stdio(false);
std::wcout.imbue(std::locale("en_US.UTF-8"));
std::wcin.imbue(std::locale("en_US.UTF-8"));
#endif
srand(time(NULL));
std::vector<std::wstring> nameArr = {L"Александр", L"Андрей", L"Анастасия", L"Ирина", L"Наталья", L"Павел", L"Роман", L"Светлана", L"Сергей", L"Татьяна"};
std::vector<Human*> humanArr;
for(int i = 0;i<5;++i)
{
int a = (rand() % 3) + 1;
int b = rand() % nameArr.size();
int c = rand() % (40 - 20 + 1) + 20;
if(a == 1)
{
humanArr.push_back(new Informal(nameArr[b], c));
nameArr.erase(nameArr.begin()+b);
}
if(a == 2)
{
humanArr.push_back(new Formal(nameArr[b], c));
nameArr.erase(nameArr.begin()+b);
}
if(a == 3)
{
humanArr.push_back(new Realist(nameArr[b], c));
nameArr.erase(nameArr.begin()+b);
}
}
for(size_t i = 0;i<humanArr.size();++i)
{
std::wcout<<humanArr[i]->narration();
}
std::wcout<<std::endl;
for(size_t i = 1;i<humanArr.size();++i)
{
std::wcout<<L"Приветствие №"<<i<<std::endl;
std::wcout<<humanArr[0]->greeting(*humanArr[i]);
std::wcout<<humanArr[i]->greeting(*humanArr[0])<<std::endl;
}
for(size_t i = 0;i<humanArr.size();++i)
{
delete humanArr[i];
}
humanArr.clear();
return 0;
}