我正在开发一个模仿“动物收养机构”的计划。我从文件中读到,其中包含动物名称,品种,年龄,价格和性别的列表。我有两个文件,分别用于猫和狗。
用户可以选择按上面列出的类别对列表进行排序。我目前有一个for循环,可以准确地对他们选择的类别进行排序;但是,其他类别不会相应地自行订购。我不知道该如何解决这个问题。 这是我的代码的精简版本,它只允许访问dog部分并按名称排序,而不是选择如何排序。
#include <iostream>
#include <fstream>
#include <istream>
#include <cctype>
#include <string>
#include <string.h>
#include <cstring>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;
int animalMenu, animalCount, animAge[50], animPrice[50], entry = 0, total;
string animType, animName[50], animBreed[50], animSex[50], takeHomeWith;
ifstream animalInform;
const int WIDTH = 8, BIG_WIDTH = 12;
void sortingHat(string[]);
void innerSorting(string[], int);
int main() {
animalInform.open("Dog Information.txt");
animType = "dogs";
// SET NUMBER OF ANIMALS IN FILE
animalInform >> animalCount;
cout << "There are " << animalCount << " " << animType << "! \n";
// SETS ALL THE VALUES BY READING FROM FILE
for (int entry = 0; entry < animalCount; entry++) {
animalInform >> animName[entry] >> animBreed[entry] >> animAge[entry] >> animPrice[entry] >> animSex[entry];
cout << setw(BIG_WIDTH) << animName[entry] << setw(BIG_WIDTH) << animBreed[entry] << setw(WIDTH) << animAge[entry] << setw(WIDTH) << animPrice[entry] << setw(WIDTH) << animSex[entry] << endl;
}
// CLOSE FILE
animalInform.close();
// CALL FUNCTION TO SORT (BY NAME ONLY)
sortingHat(animName);
cout << endl;
// DISPLAY NEWLY SORTED LIST
for (int entry = 0; entry < animalCount; entry++) {
cout << setw(BIG_WIDTH) << animName[entry] << setw(BIG_WIDTH) << animBreed[entry] << setw(WIDTH) << animAge[entry] << setw(WIDTH) << animPrice[entry] << setw(WIDTH) << animSex[entry] << endl;
}
system("pause");
}
void sortingHat(string sortingString[])
{ // SORTS DATA AND PUTS IT IN ORDER, ALPHABETICAL --
for (int outer = 0; outer <= animalCount; outer++)
{
for (int entry = 0; entry <= (animalCount - 2); entry++) {
string temporary[50];
if (sortingString[entry] > sortingString[entry + 1])
innerSorting(sortingString, entry);
}
}
}
void innerSorting(string sorter[], int entry)
{
string temporary[50];
temporary[entry] = sorter[entry];
sorter[entry] = sorter[entry + 1];
sorter[entry + 1] = temporary[entry];
}
所以我显然没有任何可以使其他条目效仿的东西。 因此,如果我选择要排序的名称,我的输出(这是我的文件中写的)将来自
Brienne Shepard 6 $150 F
Jon Labrador 3 $200 M
Aemon ShihTzu 10 $50 M
到
Aemon Shepard 6 $150 F
Brienne Labrador 3 $200 M
Jon ShihTzu 10 $50 M
我希望它能做到这一点(如果选择按名称排序):
Aemon ShihTzu 10 $50 M
Brienne Shepard 6 $150 F
Jon Labrador 3 $200 M
答案 0 :(得分:0)
如果我理解正确,你有一组包含动物特征的数组。并且您将按一种特性排序,以便对所有数组进行排序。如果是这样,那么你可以为所有数组编写一个通用函数。
例如
enum SortType { SortByName, /* other types of sorting */, SortByAge };
//...
void bubble_sort( std::string animName[],
/* other characteristics */
int animAge[],
size_t n,
SortType type )
{
for ( size_t last; not ( n < 2 ); n = last )
{
for ( size_t i = last = 1; i < n; i++ )
{
bool less = false;
switch ( type )
{
case SortByName:
less = animName[i] < animName[i-1];
break;
/* other cases */
case SortByAge:
less = animAge[i] < animAge[i-1];
break;
}
if ( less )
{
/* swapping elements of all the arrays */
last = i;
}
}
}
}
考虑到这个交换功能
void innerSorting(string sorter[], int entry)
{
string temporary[50];
^^^^^^^^^^^^^^^^^^^^
temporary[entry] = sorter[entry];
sorter[entry] = sorter[entry + 1];
sorter[entry + 1] = temporary[entry];
}
不应使用字符串数组。它可以写成
void innerSorting(string sorter[], size_t entry)
{
string temporary;
^^^^^^^^^^^^^^^^^
temporary = sorter[entry];
sorter[entry] = sorter[entry + 1];
sorter[entry + 1] = temporary;
}