我有一个2"配对"整数数组newNumerator []和newDenominator [],它们都有9个整数。我编写了一个函数,按升序对它们进行排序(最低百分比或比率最高),然而它按照以前的顺序输出元素,并且根本不对它进行排序。这是代码,sortData是(假设)对它进行排序的函数。
const static char * regex_string = "([a-zA-Z0-9]+).*";
void find_first(const std::string str);
int main(int argc, char ** argv)
{
find_first("0s7fg9078dfg09d78fg097dsfg7sdg\r\nfdfgdfg");
}
void find_first(const std::string str)
{
std::cout << str << std::endl;
std::regex rgx(regex_string);
std::smatch matcher;
if(std::regex_match(str, matcher, rgx))
{
std::cout << "Found : " << matcher.str(0) << std::endl;
} else {
std::cout << "Not found" << std::endl;
}
}
数据文件 -
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
typedef int arrayType[];
void readData(int numerator[], int denominator[], int size);
void reportData(arrayType, arrayType, int);
void reportOverall(int numerator[], int denominator[], int size);
void cleanData(int* numerator, int* denominator, const int size, int *newNumerator, int *newDenominator, int &newSize);
void reportMin(int newNumerator[], int newDenominator[], int newSize);
void sortData(int newNumerator[], int newDenominator[], int newSize);
void hrule();
int main()
{
const int size = 12;
int numerator[size];
int denominator[size];
int newSize = 12;
int newNumerator[newSize];
int newDenominator[newSize];
cout << fixed << showpoint << setprecision(1);
readData(numerator, denominator, size);
reportData(numerator, denominator, size);
reportOverall(numerator, denominator, size);
hrule();
cleanData(numerator, denominator, size, newNumerator, newDenominator, newSize);
cout << "There are " << newSize << " scores that are not bonuses:\n";
reportData(newNumerator, newDenominator, newSize);
reportMin(newNumerator, newDenominator, newSize);
hrule();
sortData(newNumerator, newDenominator, newSize);
cout << "The scores from highest to lowest are: " << endl;
reportData(newNumerator, newDenominator, newSize);
system("pause");
return 0;
}
void readData(int numerator[], int denominator[], int size)
{
ifstream dataIn;
dataIn.open("data.txt");
if(!dataIn)
{
cout << "File not found\n";
system("pause");
exit(1);
}
int count;
for(count = 0; count < size; count++)
{
dataIn >> numerator[count];
}
for (count = 0; count < size; count++)
{
dataIn >> denominator[count];
}
dataIn.close();
}
void reportData(arrayType numerator, arrayType denominator, int size)
{
int count;
for (count = 0; count < size; count++)
{
if (denominator[count] == 0)
{
cout << "Score " << (count + 1) << " is " << numerator[count] << "/" << denominator[count] << " = " << "Bonus points!\n";
}
else
{
double percent = 100.0 * static_cast<double>(numerator[count]) / denominator[count];
cout << "Score " << (count + 1) << " is " << numerator[count] << "/" << denominator[count] << " = " << (percent) << "%\n";
}
}
}
void reportOverall(int numerator[], int denominator[], int size)
{
int count;
int totalNumerator = 0.0;
int totalDenominator = 0.0;
for (count = 0; count < size; count++)
{
totalNumerator += numerator[count];
}
for (count = 0; count < size; count++)
{
totalDenominator += denominator[count];
}
double overallPercent = 100.0 * static_cast<double>(totalNumerator) / (totalDenominator);
cout << "Total Points Earned (numerators): " << totalNumerator << endl;
cout << "Total Points Possible (denominators): " << totalDenominator << endl;
cout << "Overall Grade: " << overallPercent << "%\n";
}
void cleanData(int* numerator, int* denominator, const int size, int *newNumerator, int *newDenominator, int &newSize)
{
int count;
int count2 = 0;
for(count = 0; count < size; count++)
{
if(denominator[count] != 0)
{
newNumerator[count2] = numerator[count];
newDenominator[count2] = denominator[count];
count2++;
}
else if(denominator[count] == 0)
{
newSize--;
}
}
}
void reportMin(int newNumerator[], int newDenominator[], int newSize)
{
double minimum;
int count;
int location = 0;
double quotient;
for(count = 0; count < newSize; count++)
{
quotient = 100.0 * static_cast<double>(newNumerator[count]) / newDenominator[count];
if (count == 0 || quotient < minimum)
{
minimum = quotient;
location = count;
}
}
cout << "The lowest earned percentage grade is " << newNumerator[location] << "/" << newDenominator[location] << " = " << minimum << "%\n";
}
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);
}
void hrule()
{
cout << "\n****************************************\n\n";
}
答案 0 :(得分:0)
您在循环外计算percentageLeft
和percentageRight
。当你交换东西时,这些价值观永远不会改变。
您可能希望将它们作为for循环中的第一行。
因为前两个不需要交换,所以它认为不需要交换任何元素。如果需要交换前两个DID,那么您的程序将永远运行,因为if语句if(percentageLeft > percentageRight)
将始终为真,swap
将始终设置为true,从而导致无限循环。