如何访问和重新排列(冒泡排序)结构中存储的信息?

问题描述 投票:0回答:1

这是我到目前为止的代码。我想对从文本文件加载的患者记录进行排序。我正在努力了解如何访问结构中存储的信息以重新排列它。例如,我如何定义一个函数来交换年龄,同时保留附加到年龄的信息(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;
}

感谢您的帮助;

c++ bubble-sort
1个回答
0
投票

在 C++ 中处理需要排序的数据看起来更像这样:

#include <vector>
#include <string>
#include <algorithm>
#include <iostream>

// using namespace std; <== no unlearn this

enum class gender_v
{
    male,
    female
};

// note your original declaration of record was in "C" style
struct record_t
{
    std::string firstName;
    std::string lastName;
    // string sex;
    gender_v gender; // <== C++ is typesafe so use that 
    unsigned int age; // ages cannot be smaller than 0 so unsigned it is
    int id;
};


bool sort_by_ascending_age(const record_t& lhs, const record_t& rhs)
{
    return lhs.age < rhs.age;
}

bool sort_by_last_name(const record_t& lhs, const record_t& rhs)
{
    return lhs.lastName < rhs.lastName;
}

// etc for compare functions

std::ostream& operator<<(std::ostream& os, const record_t& record)
{
    os << record.firstName << " " << record.lastName << ", age = " << record.age;
    return os;
}

int main()
{
    std::vector<record_t> records
    {
        {"Alice","Anders", gender_v::female, 32, 1},
        {"Bob","Bowman", gender_v::male, 12, 2},
        {"Charly", "Chaplin", gender_v::male, 51, 3},
        {"Denise", "Dapper", gender_v::male, 21, 4}
    };

    std::sort(records.begin(), records.end(), sort_by_ascending_age);

    for (const auto& record : records)
    {
        std::cout << record << "\n";
    }

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.