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

时间:2013-05-25 06:22:29

标签: c++ visual-c++ sorting bubble-sort

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

例如,我有4个名字,从左到右显示姓氏,名字,gpa和家庭收入。

Hernandez Joshua 3.40 65000

Su Harry 3.33 60000

唐爱德华4.00 100000

Guan Jessica 3.20 50000

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

Guan Joshua 3.40 65000

Hernandez Harry 3.33 60000

Su Edward 4.00 100000

Tang Jessica 3.20 50000

这是我的班级人员

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;
}

2 个答案:

答案 0 :(得分:0)

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

您从一系列学生开始: [学生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;

上面你概括了交换函数,以便交换两个指针。这也可以为一个人完成。

swapPeople(array [0],array [1])

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

答案 1 :(得分:0)

除非你在家庭作业之类的地方做这件事,你需要自己做所有事情,否则你最好不要使用标准库来帮助解决这个问题。我写的代码是这样的:

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排序的代码,按名字,姓氏或收入排序的代码似乎应该非常明显。