PigInfo.h
#ifndef PIGINFO_H #define PIGINFO_H #include <iostream> #include<string.h> #include<math.h> using namespace std; /* 实体类 https://learn.microsoft.com/zh-cn/cpp/standard-library/cpp-standard-library-header-files?view=msvc-170 https://learn.microsoft.com/en-us/cpp/cpp/header-files-cpp?view=msvc-170 https://btechgeeks.com/cpp-how-to-sort-a-list-of-objects-with-custom-comparator-or-lambda-function/ 排序 */ /** * @brief 猪的类的简单概述 \n(换行) * 类的详细概述 * geovindu, Geovin Du */ class PigInfo { private: /** * @brief 成员变量猪的名称 * * 成员变量PigName的详细说明,这里可以对变量进行 * 详细的说明和描述,具体方法和函数的标注是一样的 */ std::string PigName; /** * @brief 成员变量猪的重量 * * 成员变量PigName的详细说明,这里可以对变量进行 * 详细的说明和描述,具体方法和函数的标注是一样的 */ int PigWeight; public: /** * @param [string] pigName 只能输入以下参数: * -# a:代表geovindu // 生成 1. a:代表geovindu * -# b:代表Geovin Du // 生成 2. b:代表Geovin Du * -# c:代表hong // 生成 3. c:代表hong */ void SetPigName(string pigName) { PigName = pigName; } /** * @param [int] pigWeight 只能输入以下参数: * -# 1:代表公斤 // 生成 1. 1:代表公斤 * -# 2:代表吨 // 生成 2. 1:代表吨 * -# 3:代表克 // 生成 3. 1:代表克 */ void SetPigWeight(int pigWeight) { PigWeight = pigWeight; } /** * @brief 得到猪的名称 * @param [string] 参数名称 * * @retval 0 成功 * @retval -1 失败 * @pre file 必须使用的返回值 */ string getPigName() { return PigName; } /** * @brief 得到猪的重量 * @param [string] 参数名称 * * @retval 0 成功 * @retval -1 失败 * @pre file 必须使用OpenFile的返回值 */ int getPigWeight() { return PigWeight; } //构造函数 //PigInfo(string pigname, int pigweight); /** * @brief 显示猪的属性 * */ void ShowInfo(); // PigInfo(int sRoll, std::string sName) : PigWeight(sRoll), PigName(sName) { } /** * */ bool operator <(const PigInfo& pigObj) const { return PigWeight < pigObj.PigWeight; } }; /* 构造函数 PigInfo::PigInfo(string PigName, int PigWeight) { //构造函数函数体内进行赋值操作 this->PigName = PigName; this->PigWeight = PigWeight; } */ /** * @brief 显示猪的属性 方法 * Geovin Du,涂聚文 */ void PigInfo::ShowInfo() { cout << "小猪名称:" << this->getPigName() << ",重量:" << this->getPigWeight() << endl; //cout << "" << this->getPigWeight() << endl; } #endif
调用:
// cppdemo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include<string.h> #include<math.h> #include<list> #include <algorithm> #include <vector> #include "PigInfo.h" #include "geoivndu.h" using namespace std; using namespace geovindu; class Rectangle { int width, height; public: void set_values(int, int); int area() { return width * height; } }; void Rectangle::set_values(int x, int y) { width = x; height = y; } int main() { //std::cout << "Hello World!涂聚文\n"; /*Rectangle rect; rect.set_values(3, 4); cout << "area: " << rect.area(); */ //if 比较小猪 Geovin geovin; geovin.getPig(); //PigInfo piginfo; std::list<PigInfo> pigInfoList; ; string name; int weight; std::cout << "输入三个小猪的重量\n"; //piginfo.SetPigName("geovindiu"); //piginfo.SetPigWeight(25); for (int i = 1; i < 4; i++) { std::cout << "请输入第"<<i<<"只小猪的名称" << endl; cin >> name; std::cout << "请输入第"<<i<<"只小猪的重量" << endl; cin >> weight; //piginfo.SetPigName(name); //piginfo.SetPigWeight(weight); //piginfo.ShowInfo(); PigInfo piginfo(weight,name); pigInfoList.push_back(piginfo); } cout << "名称:" << "\t" << "重量:" << "\t" << endl; for (auto info : pigInfoList) { cout <<""<< info.getPigName() << "\t"<<info.getPigWeight() << endl; } int len = sizeof(pigInfoList);// / sizeof(pigInfoList[0]); //排序 pigInfoList.sort(); //sort(pigInfoList, sizeof(pigInfoList)); //vector<PigInfo> v(PigInfo, size(pigInfoList)); cout << "重量排序\n名称:" << "\t" << "重量:" << "\t" << endl; for (PigInfo p:pigInfoList) { cout << "" << p.getPigName() << "\t" << p.getPigWeight() << endl; } system("pause"); return 0; } // 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单 // 调试程序: F5 或调试 >“开始调试”菜单 // 入门使用技巧: // 1. 使用解决方案资源管理器窗口添加/管理文件 // 2. 使用团队资源管理器窗口连接到源代码管理 // 3. 使用输出窗口查看生成输出和其他消息 // 4. 使用错误列表窗口查看错误 // 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 // 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
输出
请输入第一只小猪pig1的重量 20 请输入第二只小猪pig2的重量 8 请输入第三只小猪pig3的重量 87 此时三只小猪的重量分别为:pig1=20,pig2=8,pig3=87, 小猪3最重,它的重量为:87 三只小猪一样重 大小排序:第三只小猪:87>第一只小猪:20>第二只小猪:8 输入三个小猪的重量 请输入第1只小猪的名称 小花 请输入第1只小猪的重量 45 请输入第2只小猪的名称 小白 请输入第2只小猪的重量 20 请输入第3只小猪的名称 小草 请输入第3只小猪的重量 880 名称: 重量: 小花 45 小白 20 小草 880 重量排序 名称: 重量: 小白 20 小花 45 小草 880 请按任意键继续. . .