#include "strategy.hpp" SortPerson::SortPerson(ICompare* pICompare) { m_CompareStrategy = pICompare; } void SortPerson::sort(vector& 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; }