如何按降序排序?

时间:2019-10-09 06:48:05

标签: 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;行出现意外的表达错误,我不理解。

1 个答案:

答案 0 :(得分:1)

要排序的数组有些混乱:

  • 您应该将totalRevenue定义为double,而不是双精度数组
  • 应该根据准则numProductproductArray对数组totalRevenue的前name个元素进行排序,顺序由使用的比较运算符确定。如果第一个条件的值相等,则仅比较第二个条件。

这是修改后的版本:

#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分配对象并操纵裸指针。
  • 您应该处理异常