【问题标题】:How can I sort in descending order?如何按降序排序?
【发布时间】:2019-10-09 06:48:05
【问题描述】:

我正在尝试对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-&gt;totalRevenue[] = aSelling-&gt;price * aSelling-&gt;soldNumber; 行有一个我不理解的意外表达式错误。

【问题讨论】:

  • aSelling-&gt;totalRevenue[???] - 您需要访问数组中的元素。 [] 本身毫无意义。但是double totalRevenue[] 到底是什么意思呢?您只需要一个值,而不是数组吗?然后使用double totalRevenue
  • @Minh Nguyen 你为什么不使用 std::vector?
  • 另外,这是某种家庭作业,您可以使用标准库的哪些部分受到限制?
  • 使用std::sort
  • 请描述你想用你的程序做什么。 double totalRevenue[]; 是指针。你不为它分配内存。你不能给它赋值。

标签: c++ sorting pointers struct


【解决方案1】:

要排序的数组有些混乱:

  • 您应该将totalRevenue 定义为double,而不是双精度数组,
  • 应根据totalRevenuename 的条件对数组productArray 的第一个numProduct 元素进行排序,顺序由使用的比较运算符确定。仅当第一个条件给出的值相等时才比较第二个条件。

这是修改后的版本:

#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;
}

补充说明:

  • 您可以使用更有效的排序方法
  • 你应该释放分配的对象
  • 您应该使用&lt;algorithms&gt; 和对象的动态数组,而不是使用new 分配对象并操作裸指针。
  • 你应该处理异常

【讨论】:

  • 啊!滑手指综合症 - 了解!
  • @chqrlie 谢谢。你能告诉我在哪里可以阅读更多关于如何使用你的排序方法的信息吗?或者任何方式来练习这个。我想了解更多。
  • @MinhNguyen:我没有更改排序方法:您使用了冒泡排序/插入排序的变体,它对大集合来说简单但效率低下。将比较代码移至单独的函数使其更具可读性且更易于更改。有更有效的排序方法,例如合并排序和快速排序,请在维基百科上查找。另请注意,通过两次线性扫描可以更有效地选择前 2 个元素。
猜你喜欢
  • 1970-01-01
  • 2014-04-14
  • 1970-01-01
  • 1970-01-01
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多