到目前为止,这是我的代码。我想对从文本文件加载的患者记录进行排序。我正在努力理解如何访问存储在结构体中的信息,以便重新排列它。例如,如何定义一个函数,将交换年龄,同时保持附加到年龄的信息(id,性别等)。
#include <iostream>
#include <cstring> // required for string manipulation
#include <iomanip> // required for tabular output
#include <fstream>
using namespace std;
const int NUM_PATIENTS = 10; // 10 patients will be included, this value cannot be modified
const int MAX_NAME_LENGTH = 10; // no names longer than 10 characters, this value cannot be modified
const int SIZE=10;
const int SIZE2=10;
int x;
int main()
{
string category;
do{
cout << "Please select a sorting category"<< endl;
cin >> category;
if (category=="Age")
{
cout << "Case 1, sorting by age" << endl;
x=1;
}
else if (category=="ID")
{
cout << "Case 2, sorting by ID" <<endl;
x=2;
}
else if (category=="Sex")
{
cout << "Case 3, sorting by sex" << endl;
x=3;
}
else if (category=="Last")
{
cout << "Case 4, sorting by last name"<< endl;
x=4;
}
else if (category=="First")
{
cout << "Case 5, sorting by first name" << endl;
x=5;
}
else
{
cout << "invalid input" << endl;
x=6;
}
}
while (x==6);
struct records
{
string firstName;
string lastName;
string sex;
int age;
int id;
} record [SIZE];
ifstream in ("patientInfo.txt");
if (!(in))
{
cout << "ERROR OPENING FILE" << endl;
}
else
{
for (int i=0; i <SIZE; i++)
{
in >> record[i].age>> record[i].id>> record[i].firstName >> record[i].lastName >> record[i].sex;
}
for (int j=0;j<SIZE; j++)
{
if (x==1)
{
if (record[j].age>record[j+1].age)
{
//rearrange
}
}
if (x==2)
{
//sort id
}
if (x==3)
{
//sort first name
}
if (x==4)
{
//sort lastname
}
if (x==5)
{
//sort sex
}
}
}
for (int i=0; i<1; i++)
{
cout << record[0].age << setw(10) <<record[1].age << setw(10) << record[2].age << setw(10) << record[3].age << setw(10) << record[4].age << setw(10) << record[5].age << setw(10) << record[6].age << setw(10) << record[7].age << setw(10) << record[8].age << setw(10) << record[9].age << endl;
cout << record[0].id << setw(10) << record[1].id << setw(10) << record[2].id << setw(10) << record[3].id<< setw(10) << record[4].id << setw(10) << record[5].id<< setw(10) << record[6].id << setw(10) << record[7].id << setw(10) << record[8].id << setw(10) << record[9].id << endl;
cout << record[0].firstName << setw(10) << record[1].firstName << setw(10) << record[2].firstName << setw(10) << record[3].firstName<< setw(10) << record[4].firstName<< setw(10) << record[5].firstName<< setw(10) << record[6].firstName<< setw(10) << record[7].firstName << setw(10) << record[8].firstName<< setw(10) << record[9].firstName << endl;
cout << record[0].lastName << setw(10) << record[1].lastName << setw(10) << record[2].lastName << setw(10) << record[3].lastName<< setw(10) << record[4].lastName<< setw(10) << record[5].lastName<< setw(10) << record[6].lastName<< setw(10) << record[7].lastName << setw(10) << record[8].lastName<< setw(10) << record[9].lastName << endl;
cout << record[0].sex << setw(10) << record[1].sex << setw(10) << record[2].sex << setw(10) << record[3].sex<< setw(10) << record[4].sex<< setw(10) << record[5].sex << setw(10) << record[6].sex << setw(10) << record[7].sex << setw(10) << record[8].sex << setw(10) << record[9].sex << endl;
}
return 0;
}
字符串
谢谢你的帮助;
1条答案
按热度按时间dauxcl2d1#
在C++中处理需要排序的数据看起来更像这样:
字符串