gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:08:24 25 4
gpt4 key购买 nike

我正在尝试对 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com