48 lines
1.2 KiB
C++
48 lines
1.2 KiB
C++
#include "strategy.hpp"
|
|
|
|
SortPerson::SortPerson(ICompare* pICompare)
|
|
{
|
|
m_CompareStrategy = pICompare;
|
|
}
|
|
|
|
void SortPerson::sort(vector<StrategyPerson>& personList)
|
|
{
|
|
for (size_t i = 0; i < personList.size() - 1; i++) // Bubble sort
|
|
{
|
|
for (size_t j = i + 1; j < personList.size(); j++)
|
|
{
|
|
if (m_CompareStrategy->comparable(&personList[i], &personList[j]))
|
|
{
|
|
StrategyPerson temp = personList[i];
|
|
personList[i] = personList[j];
|
|
personList[j] = temp;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
bool CompareByWeight::comparable(StrategyPerson* person1, StrategyPerson* person2)
|
|
{
|
|
return person1->m_weight < person2->m_weight;
|
|
}
|
|
|
|
bool CompareByHeight::comparable(StrategyPerson* person1, StrategyPerson* person2)
|
|
{
|
|
return person1->m_height < person2->m_height;
|
|
}
|
|
|
|
bool CompareByAge::comparable(StrategyPerson* person1, StrategyPerson* person2)
|
|
{
|
|
return person1->m_age < person2->m_age;
|
|
}
|
|
|
|
StrategyPerson::StrategyPerson(string name, int age, double weight, double height)
|
|
{
|
|
m_name = name, m_age = age, m_weight = weight, m_height = height;
|
|
}
|
|
|
|
void StrategyPerson::showMyself()
|
|
{
|
|
cout << m_name << " ÄêÁ䣺" << m_age << "Ëê, ÌåÖØ£º" << m_weight << "kg, Éí¸ß£º" << m_height << "m" << endl;
|
|
}
|