C++ 对两个成对整数数组的"percentage"进行排序
C++ Sorting the "percentage" of two paired integer arrays
我有一个程序,里面有两个"成对"的整数数组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;
- 二叉排序树无法编译
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- C++选择排序算法中的逻辑错误
- 使用C++程序合并排序没有得到正确的输出
- 计算排序向量的向量中唯一值的计数
- 排序算法c++
- 使用2个键的cpp-stl::优先级队列排序不正确
- 将结构向量排序为子组
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 如何对点云数据进行排序
- 对字符串进行排序时,在c++中处理sort()
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- 下面是排序算法O(n)吗
- std::sort()函数无法对向量的一部分进行排序
- shell排序中的交换和比较
- clang格式:禁用排序包含
- 显示错误输出的简单数组排序程序
- 为什么我的排序算法会更改数组值
- 试图在c++中对数字列表进行排序
- C++ 对两个成对整数数组的"percentage"进行排序