C++ 对两个成对整数数组的"percentage"进行排序

C++ Sorting the "percentage" of two paired integer arrays

本文关键字:percentage 排序 数组 C++ 两个 整数      更新时间:2023-10-16

我有一个程序,里面有两个"成对"的整数数组newNumerator[]和newDenominator[],它们都有9个整数。我写了一个函数,按照升序对它们进行排序,但我不确定它是否有效,因为我还没有成功编译它。我在打字方面也遇到了一些问题。这是功能定义-

void sortData(int *newNumerator[], int *newDenominator[], int newSize)
{
    int temp1;
    int temp2;
    bool swap;
    int count = 0;
    double percentageLeft = 100.0 * static_cast<double>(newNumerator[count]) / newDenominator[count];
    double percentageRight = 100.0 * static_cast<double>(newNumerator[count + 1]) / newDenominator[count + 1];
    do
    {  swap = false;
        for(count = 0; count < (newSize - 1); count++)
        {
            if(percentageLeft > percentageRight)
            {
                temp1 = *newNumerator[count];
                *newNumerator[count] = *newNumerator[count + 1];
                *newNumerator[count + 1] = temp1;
                temp2 = *newDenominator[count];
                *newDenominator[count] = *newDenominator[count + 1];
                *newDenominator[count + 1] = temp2;
                swap = true;
            }
        }
    } while (swap);
}

我遇到的类型转换问题是percentageLeft和percentageRight,因为newNumerator[]和newDenominator[]是整数指针,但要获得它们的"百分比",我需要它是一个双精度。不知道该怎么办。基本上,我只需要弄清楚如何解决这个问题,也知道我的功能是否达到了目的。感谢您的帮助,如果有什么我可以进一步澄清的,请告诉我

我建议使用STL进行排序。让事情变得更简单、更容易理解。

#include <vector>
#include <algorithm>
struct Ratio
{
    int numerator;
    int denominator;
    double toDouble() const
    {
        return static_cast<double>(numerator)
             / static_cast<double>(denominator);
    }
    bool operator < (Ratio const& other) const
    {
        return toDouble() < other.toDouble();
    }
};
void main()
{
    std::vector<Ratio> ratios = { {1, 2}, {5, 7}, {2, 11} };
    std::sort(ratios.begin(), ratios.end());
}

处理原始数组和手动排序几乎总是一种更乏味、更容易出错的方法。

请参阅http://en.cppreference.com/w/cpp/algorithm/sort和http://en.cppreference.com/w/cpp/container/vector供参考

您的主要错误是向static_cast<>提供了错误的值。稍后,您可以通过取消引用来正确使用它们,但在强制转换中,您会错过这一点。

您需要的是:

double percentageLeft = 100.0 * static_cast<double>((*newNumerator)[count]) / *newDenominator[count];
double percentageRight = 100.0 * static_cast<double>((*newNumerator)[count + 1]) / *newDenominator[count + 1];

添加了括号,以明确地说明取消引用是什么。

此外,如果您不更改实际的数组指针,而只更改数组的内容,则可以完全消除取消引用的麻烦。如果您将功能定义为

void sortData(int newNumerator[], int newDenominator[], int newSize)

您可以只使用普通索引,而不必每次使用都取消引用。

你说:

我有一个程序,有两个"成对"的整数数组newNumerator[]和newDenominator[]

然而,您的功能定义为:

void sortData(int *newNumerator[], int *newDenominator[], int newSize) 

我认为应该是:

void sortData(int newNumerator[], int newDenominator[], int newSize) 

如果真的是这样,那么行:

temp1 = *newNumerator[count];
*newNumerator[count] = *newNumerator[count + 1];
*newNumerator[count + 1] = temp1;
temp2 = *newDenominator[count];
*newDenominator[count] = *newDenominator[count + 1];
*newDenominator[count + 1] = temp2;

需要:

temp1 = newNumerator[count]; // Remove the *
newNumerator[count] = newNumerator[count + 1];
newNumerator[count + 1] = temp1;
temp2 = newDenominator[count];
newDenominator[count] = newDenominator[count + 1];
newDenominator[count + 1] = temp2;