无法按字母顺序对文件内容进行排序
Having trouble sorting a file's contents in alphabetical order
我正在创建一个程序,该程序根据用户希望的方式对文件的内容进行排序。该文件包含学生的姓、名、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排序的代码,按名、姓或收入排序的代码似乎应该非常明显。
相关文章:
- 按价格对文件中的产品进行排序
- 如何对文件中数组中的单个列进行冒泡排序?C++
- 使用C++对一个巨大文件中的文本行进行字典式外部排序
- 如何按升序对输入文件中的数字进行排序,并找到它们的范围和中值
- 如何在一个巨大的文本文件中对整数进行排序
- 在排序链表中插入结构并将其写入二进制文件
- 从文件中读取并将其内容放入对象数组中,然后对它们进行排序
- 对包含整数的文本文件进行排序时,必须逐行进行排序
- 在排序文本文件中实现二叉搜索?
- 如何从输入的文本文件中按姓氏字母顺序对图表进行排序?
- 按文件中列表中行的一部分排序
- C++ 从文件中获取数据时使用 strcpy 和 strcmp 按字母顺序对数组进行排序?
- 如何从文件中读取并按类别对它们进行排序
- 如何在大型文本文件中对数字进行排序
- C++ 从文件中读取字符,计算每个字符并进行排序
- 从文件中读取并按字母顺序排序
- 随机化超大文件内容排序的有效方法是什么?
- C 从文件和输出排序的内容中读取
- 如何在C++的排序文件中插入数据
- 编写和打印一个额外的0来排序文件,并从末尾删除节点(单链表)