定义学生类包含姓名和成绩两个数据成员,重载比较运算符(按成绩和按姓名两个模式比较)和输出运算符
C++模板【定义学生类包含姓名和成绩两个数据成员,重载比较运算符(按成绩和按姓名两个模式比较)和输出运算符】
实现下列功能。
1)定义学生类包含姓名和成绩两个数据成员,重载比较运算符(按成绩和按姓名两个模式比较)和输出运算符。
2)实现greater_than函数模板,用于比较两个对象的大小,如果a>b,返回true,否则返回false。
3)实现less_than函数模板,用于比较两个对象的大小,如果a<b,返回true,否则返回false。
4)实现Print函数模板,用于打印一维数组。
5)实现冒泡排序的函数模板,输入参数是数组、数组大小和用于比较的数组元素的函数指针。
void bubble_sort(T arr[], int len, bool(*compare)(T&, T&));
//学生类部分
enum SortType{
BY_SCORE,BY_NAME
};
static SortType sort_type ;
class Student{
public:
Student(string name, int score):name_(name),score_(score){
}
bool operator > (const Student& rhs){
if(sort_type == BY_SCORE){
if(score_>rhs.score_){
return true;
}
return false;
}
else if(sort_type == BY_NAME){
if(name_>rhs.name_){
return true;
}
return false;
}
}
bool operator < (const Student& rhs){
if(sort_type == BY_SCORE){
if(score_<rhs.score_){
return true;
}
return false;
}
else if(sort_type == BY_NAME){
if(name_<rhs.name_){
return true;
}
return false;
}
}
//重载输出运算符
friend ostream& operator<<(ostream& os,Student& student){
os<<"["<<student.name_<<","<<student.score_<<"]";
}
private:
string name_;
int score_;
};
// 模板函数部分
template<class T>
bool greater_than(T& a,T& b){
if(a>b){
return true;
}
return false;
}
template<class T>
bool less_than(T& a,T& b){
if(a<b){
return true;
}
return false;
}
template<class T>
void Print(T t[],int len){
for(int i = 0;i<len;i++){
cout<<t[i]<<" ";
}
cout<<endl;
}
template<class T>
void bubble_sort(T arr[],int len, bool(*compare)(T&,T&)){
int i, j;
for (j = 0; j < len - 1; j++){
for (i = 0; i < len - 1 - j; i++){
if (compare(arr[i],arr[i+1]) == true){
swap(arr[i],arr[i+1]);
}
}
}
}
// 测试部分
int main() {
int int_array[] = { 61, 17, 29, 22, 34, 60, 72, 21, 50, 1, 62 };
int len = (int) sizeof(int_array) / sizeof(*int_array);
cout << "origin int array:" << endl;
Print(int_array, len);
bubble_sort(int_array, len, greater_than);
cout << "sort: ascending order:" << endl;
Print(int_array, len);
bubble_sort(int_array, len, less_than);
cout << "sort: descending order:" << endl;
Print(int_array, len);
cout << "\n-------------------------\n" << endl;
float float_array[] = { 17.5f, 19.1f, 0.6f, 1.9f, 10.5f, 12.4f, 3.8f, 19.7f, 1.5f, 25.4f, 28.6f, 4.4f, 23.8f, 5.4f };
len = (int) sizeof(float_array) / sizeof(*float_array);
cout << "origin float array:" << endl;
Print(float_array, len);
bubble_sort(float_array, len, greater_than);
cout << "sort: ascending order:" << endl;
Print(float_array, len);
bubble_sort(float_array, len, less_than);
cout << "sort: descending order:" << endl;
Print(float_array, len);
cout << "\n----------------------------\n" << endl;
Student student_array[] = { Student("John",100), Student("Mary",60), Student("Gen",70) };
len = (int) sizeof(student_array) / sizeof(*student_array);
//sort by name
cout << "sort student object by name------" << endl;
sort_type = BY_NAME;
cout << "origin student array:" << endl;
Print(student_array, len);
bubble_sort(student_array, len, greater_than);
cout << "sort by name: ascending order:" << endl;
Print(student_array, len);
bubble_sort(student_array, len, less_than);
cout << "sort by name: descending order:" << endl;
Print(student_array, len);
cout << "\n-----------------------------\n" << endl;
//sort by score
cout << "sort student object by score------" << endl;
sort_type = BY_SCORE;
cout << "origin student array:" << endl;
Print(student_array, len);
bubble_sort(student_array, len, greater_than);
cout << "sort by score: ascending order:" << endl;
Print(student_array, len);
bubble_sort(student_array, len, less_than);
cout << "sort by score: descending order:" << endl;
Print(student_array, len);
return 0;
· 完整代码
#include<iostream>
#include<string.h>
//@Author YE
using namespace std;
enum SortType{
BY_SCORE,BY_NAME
};
static SortType sort_type ;
class Student{
public:
Student(string name, int score):name_(name),score_(score){
}
bool operator > (const Student& rhs){
if(sort_type == BY_SCORE){
if(score_>rhs.score_){
return true;
}
return false;
}
else if(sort_type == BY_NAME){
if(name_>rhs.name_){
return true;
}
return false;
}
}
bool operator < (const Student& rhs){
if(sort_type == BY_SCORE){
if(score_<rhs.score_){
return true;
}
return false;
}
else if(sort_type == BY_NAME){
if(name_<rhs.name_){
return true;
}
return false;
}
}
friend ostream& operator<<(ostream& os,Student& student){
os<<"["<<student.name_<<","<<student.score_<<"]";
}
private:
string name_;
int score_;
};
template<class T>
bool greater_than(T& a,T& b){
if(a>b){
return true;
}
return false;
}
template<class T>
bool less_than(T& a,T& b){
if(a<b){
return true;
}
return false;
}
template<class T>
void Print(T t[],int len){
for(int i = 0;i<len;i++){
cout<<t[i]<<" ";
}
cout<<endl;
}
template<class T>
void bubble_sort(T arr[],int len, bool(*compare)(T&,T&)){
int i, j;
for (j = 0; j < len - 1; j++)
{
for (i = 0; i < len - 1 - j; i++)
{
if (compare(arr[i],arr[i+1]) == true)
{
swap(arr[i],arr[i+1]);
}
}
}
}
int main() {
int int_array[] = { 61, 17, 29, 22, 34, 60, 72, 21, 50, 1, 62 };
int len = (int) sizeof(int_array) / sizeof(*int_array);
cout << "origin int array:" << endl;
Print(int_array, len);
bubble_sort(int_array, len, greater_than);
cout << "sort: ascending order:" << endl;
Print(int_array, len);
bubble_sort(int_array, len, less_than);
cout << "sort: descending order:" << endl;
Print(int_array, len);
cout << "\n---------------------------\n" << endl;
float float_array[] = { 17.5f, 19.1f, 0.6f, 1.9f, 10.5f, 12.4f, 3.8f, 19.7f, 1.5f, 25.4f, 28.6f, 4.4f, 23.8f, 5.4f };
len = (int) sizeof(float_array) / sizeof(*float_array);
cout << "origin float array:" << endl;
Print(float_array, len);
bubble_sort(float_array, len, greater_than);
cout << "sort: ascending order:" << endl;
Print(float_array, len);
bubble_sort(float_array, len, less_than);
cout << "sort: descending order:" << endl;
Print(float_array, len);
cout << "\n---------------------------\n" << endl;
Student student_array[] = { Student("John",100), Student("Mary",60), Student("Gen",70) };
len = (int) sizeof(student_array) / sizeof(*student_array);
//sort by name
cout << "sort student object by name------" << endl;
sort_type = BY_NAME;
cout << "origin student array:" << endl;
Print(student_array, len);
bubble_sort(student_array, len, greater_than);
cout << "sort by name: ascending order:" << endl;
Print(student_array, len);
bubble_sort(student_array, len, less_than);
cout << "sort by name: descending order:" << endl;
Print(student_array, len);
cout << "\n---------------------------\n" << endl;
//sort by score
cout << "sort student object by score------" << endl;
sort_type = BY_SCORE;
cout << "origin student array:" << endl;
Print(student_array, len);
bubble_sort(student_array, len, greater_than);
cout << "sort by score: ascending order:" << endl;
Print(student_array, len);
bubble_sort(student_array, len, less_than);
cout << "sort by score: descending order:" << endl;
Print(student_array, len);
return 0;
}
运行结果截图:
(:3<()_)菜鸟一枚,如有问题欢迎指出……