c++ - 如何按降序排序?

标签 c++ sorting pointers struct

我正在尝试对 totalRevenue 及其 name 的最高值和第二高值进行排序。我试图以这种方式按降序对它们进行排序,但我无法弄清楚如何使其工作。谁能帮帮我? 前两个条目:

1002 Hammer  23.65  203 
1024 Nails  6.95  400

代码如下:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

// Structure to hold statistics
struct Selling {
    int productNumber;
    string name;
    double price;
    int soldNumber;
    double totalRevenue[];
};

int main()
{
    ifstream statFile;
    string productName;
    double price;
    int productNumber, soldNumber;
    Selling *productArray[100];
    Selling *aSelling;
    int numProduct = 0;
    int man = 0;

    statFile.open("sales.txt");

    while (numProduct < 100 && statFile >> productNumber >> productName >> price >> soldNumber)
    {
        Selling *aSelling = new Selling;
        aSelling->productNumber = productNumber;
        aSelling->name = productName;
        aSelling->price = price;
        aSelling->soldNumber = soldNumber;
        aSelling->totalRevenue[] = aSelling->price * aSelling->soldNumber;
        productArray[numProduct++] = aSelling;

        //cout << aSelling->productNumber<< " " << aSelling->name << " " << aSelling->price << " " << aSelling->soldNumber << " " << aSelling->totalRevenue << endl;
    }

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (aSelling->totalRevenue[i] > aSelling->totalRevenue[j]) {
                man = aSelling->totalRevenue[i];
                aSelling->totalRevenue[i] = aSelling->totalRevenue[j];
                aSelling->totalRevenue[i] = man;
            }
        }
    }

    for (int i = 0; i < 2; i++) {
        cout << "The top selling product is " << aSelling->name << "with total sales of " << aSelling->totalRevenue[i] << endl;
        cout << "The second top selling product is " << aSelling->name << "with total sales of " << aSelling->totalRevenue[i - 1] << endl;
    }
}

aSelling->totalRevenue[] = aSelling->price * aSelling->soldNumber; 行出现意外的表达式错误,我不明白。

最佳答案

数组排序有些困惑:

  • 你应该定义totalRevenue作为double ,不是 double 数组,
  • 应该排在第一个 numProduct数组的元素 productArray基于标准totalRevenuename ,顺序由使用的比较运算符决定。只有在第一个标准给出相同值时才比较第二个标准。

修改后的版本:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

// Structure to hold statistics
struct Selling {
    int productNumber;
    string name;
    double price;
    int soldNumber;
    double totalRevenue;
};

int compareSellings(const Selling *a, const Selling *b) {
    // sort in decreasing order of totalRevenue
    if (a->totalRevenue > b->totalRevenue) return -1;  // a comes before b
    if (a->totalRevenue < b->totalRevenue) return +1;  // b comes before a
    // sort in increasing order of name for the same totalRevenue
    if (a->name < b->name) return -1;  // a comes before b
    if (a->name > b->name) return +1;  // b comes before a
    return 0;
}

int main() {
    ifstream statFile;
    string productName;
    double price;
    int productNumber, soldNumber;
    Selling *productArray[100];
    int numProduct = 0;

    statFile.open("sales.txt");

    while (numProduct < 100 && statFile >> productNumber >> productName >> price >> soldNumber) {
        Selling *aSelling = new Selling;
        aSelling->productNumber = productNumber;
        aSelling->name = productName;
        aSelling->price = price;
        aSelling->soldNumber = soldNumber;
        aSelling->totalRevenue = price * soldNumber;
        productArray[numProduct++] = aSelling;

        //cout << aSelling->productNumber<< " " << aSelling->name << " " 
        //     << aSelling->price << " " << aSelling->soldNumber << " " 
        //     << aSelling->totalRevenue << endl;
    }

    for (int i = 0; i < numProduct; i++) {
        for (int j = 0; j < numProduct; j++) {
            if (compareSellings(productArray[i], productArray[j]) > 0) {
                Selling *aSelling = productArray[i];
                productArray[i] = productArray[j];
                productArray[j] = aSelling;
            }
        }
    }

    cout << "The top selling product is " << productArray[0]->name
         << ", with total sales of " << productArray[0]->totalRevenue << endl;
    cout << "The second selling product is " << productArray[1]->name
         << ", with total sales of " << productArray[1]->totalRevenue << endl;

    return 0;
}

进一步说明:

  • 你可能会使用更高效的排序方法
  • 你应该释放分配的对象
  • 你应该使用<algorithms>和动态对象数组,而不是使用 new 分配对象并操纵裸指针。
  • 你应该处理异常

关于c++ - 如何按降序排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58298503/

相关文章:

c++ - 在模板方法c++中传递结构参数

c++ - C++中静态多态性背后的动机是什么?

java - 通过将二维数组复制到一维数组来对二维数组进行排序,输出正确,但迭代重复

c++ 指向字符串数组的指针

c++ - 如何使用 ULARGE_INTEGER 进行算术运算

c++ - GLFW 3 初始化了,还没有?

arrays - Ruby:插入和排序数字

python - 按多个值对字典进行排序

string - Go 中对字符串字面量的引用

c - 结构指针丢失数据