无法按字母顺序对文件内容进行排序

Having trouble sorting a file's contents in alphabetical order

本文关键字:排序 文件 顺序      更新时间:2023-10-16

我正在创建一个程序,该程序根据用户希望的方式对文件的内容进行排序。该文件包含学生的姓、名、gpa和家庭收入。我已经让我的程序根据用户的选择(包括姓氏、收入和gpa)进行排序。我的问题是,当程序对文件进行排序时,它最终只对收入、gpa或姓氏进行排序。我想让它交换整行

例如,我下面有4个名字,从左到右依次显示了姓、名、gpa和家庭收入。

Hernandez Joshua 3.40 65000

苏哈利3.33 60000

唐·爱德华4.00 100000

关杰西卡3.20 50000

在我的程序按姓氏对文件进行排序之后,它最终只对姓氏进行排序,而不更改其他数据以适应姓氏的位置。

关约书亚3.40 65000

Hernandez Harry 3.33 60000

苏爱德华4.00 100000

唐杰西卡3.20 50000

这是我的Class Person

void getData(Person student[], int& item)
{
    ifstream fin;
    fin.open("C:students.txt");
    item = 0;
    while(!fin.eof())
    {
        fin >> student[item].lastName >> student[item].firstName >> student[item].gpa >> student[item].income;
        item++;
    }

}
void swap(string& name1, string& name2)
{
   //this is a swap function that swaps the data of the two string arguments.
   string temp;
   temp  = name1;
   name1 = name2;
   name2 = temp;
}
void swap2(float& num1, float& num2)
{
    //this is a swap function that swaps the data of the two float arguments
    float temp;
    temp = num1;
    num1 = num2;
    num2 = temp;
}
void sortByLastName(Person student[], int item)
{
   //This for loop will put the items in alphabetical order. 
   for(int j=0; j<item-1; j++)
   {
       //will perform the swapping until all items are in alphabetical order.
       for(int i=0; i<item-1; i++)
          //will swap the two items next to each other if the first item is bigger than the next item.
          if(student[i].lastName > student[i+1].lastName)
            swap(student[i].lastName, student[i+1].lastName);
   }
}
void sortByGpa(Person student[], int item)
{
    //This for loop will put the items in descending order.
   for(int j=0; j<item-1; j++)
   {
      //will perform the swapping until all items are in descending order.
      for(int i=0; i<item-1; i++)
         //will swap the two items next to each other if the first item is smaller than the next item.
         if(student[i].gpa < student[i+1].gpa)
            swap2(student[i].gpa, student[i+1].gpa);
   }
}
void sortByIncome(Person student[], int item)
{
   //This for loop will put the items in ascending order.
   for(int j=0; j<item-1; j++)
   {
      //will perform the swapping until all items are in descending order.
      for(int i=0; i<item-1; i++)
         //will swap the two items next to each other if the first item is smaller than the next item.
         if(student[i].income < student[i+1].income)
            swap2(student[i].income, student[i+1].income);
   }
}
void getChoice(int choice, Person student[], int item)
{
    cout << "Press 1 to sort by last name. Press 2 to sort by gpa. Press 3 to sort by income.";
    cin >> choice;
    if(choice == 1)
        sortByLastName(student, item);
    else if(choice == 2)
        sortByGpa(student, item);
    else if(choice == 3)
        sortByIncome(student, item);
}
void output(Person student[], int item)
{
   //Displays all of the names to the screen.
   for(int i=0; i<item; i++)
      cout << student[i].lastName << " " << student[i].firstName << " " << student[i].gpa << " " << student[i].income << endl;
}

想想两个学生的简单例子。

从一个学生数组开始:[学生1,学生2]

目标是根据学生的某些属性对数组中的学生进行排序。

假设我们想按gpa给学生排序。

  if (student1.gpa > student2.gpa) 
    put student 1 first
  otherwise
    put student 2 first

由于学生1已经是第一名,所以不需要更改。为了把学生2放在第一位,我们希望数组看起来像这样:[学生2,学生1]

一种方法是:

Person temp = student 1;
array[0] = student 2;
array[1] = student 1;
上面的

泛化了swap函数,可以交换两个指针。这也可以为一个人做。

swapPeople([0]数组,数组[1])

void swapPeople(Person &a, Person &b) {
Person temp = a;
b = a;
a = temp;
}

除非您是在做作业之类的事情,要求您自己做所有的事情,否则您最好使用标准库来帮助完成其中的大部分工作。我会这样写代码:

struct person { 
    std::string first_name;
    std::string last_name;
    double income;
    double gpa;
    friend std::istream &operator>>(std::istream &is, person &p) { 
        return is >> p.first_name >> p.last_name >> p.income >> p.gpa;
    }
    friend std::ostream &operator<<(std::ostream &os, person const &p) { 
        return os << p.first_name << "t" << p.last_name << "t"
                  << p.income << "t" << p.gpa;
    }
};
// read the data from the file:
std::vector<person> people((std::istream_iterator<person>(infile)),
                            std::istream_iterator<person>());
// sort by GPA:
std::sort(people.begin(), people.end(),
          [](person const &a, person const &b) { return a.gpa < b.gpa; });
// print the data:
for (auto &p : people) 
    std::cout << p << "n";

基于按GPA排序的代码,按名、姓或收入排序的代码似乎应该非常明显。