简体   繁体   中英

How can't I sort by a number in descending order of course code

I am writing a program to manage a list of records. The user first inputs several records, until the string "END" that indicates the end of input. Then input a student id and output all record of the student in descending order of course code

I am trying to use a bubble sort to do this, but I don't know why my program is not working. Can you guys give me any hints?? The change() function is to change the course code to int without A .

Sample input

SEHH2042 19100001A 76.4
SEHH2042 18001234A 85.2
SEHH2042 19000123A 45.5
SEHH3140 18001234A 89.3
SEHH1034 19100001A 45.7
SEHH1034 18001234A 88.4
SEHH2271 18001234A 85.4
END
18001234A 

Sample output

SEHH3140 89.3
SEHH2271 85.4
SEHH2042 85.2
SEHH1034 88.4
#include <iostream>
using namespace::std;

int change(string id) {
    int num;
    string dd(id, 4, 7);
    num = atoi(dd.c_str());
    return num;
}

int main()
{
    string subject[1000], x, tmp1;
    string id[1000], y, search, tmp2;
    double mark[1000], z = 0, sum = 0, tmp3;
    int count = 0, flag = 0, id2[100], p = 0, count2 = 0, tmp4;
    //input
    do {
        cin >> x;
        if (x == "END") {
            flag = 1;
            break;
        }
        cin >> y;
        cin >> z;
        subject[count] = x;
        id[count] = y;
        mark[count] = z;
        p = change(x);
        id2[count] = p;
        count++;
    } while (flag != 1);

    // sort by course code
    for (int i = 0; i < count - 1; i++) {
        for (int k = count - 1; k > i; k--) {
            if (id2[k] > id2[k - 1]) {
                tmp1 = subject[k];
                tmp2 = id[k];
                tmp3 = mark[k];
                tmp4 = id2[k];
                subject[k] = subject[k - 1];
                id[k] = id[k - 1];
                mark[k] = mark[k - 1];
                id2[k] = id2[k - 1];
                subject[k] = tmp1;
                id[k] = tmp2;
                mark[k] = tmp3;
                id2[k] = tmp4;
            }
        }
    }

    cin >> search;
    // search the result of the student
    for (int i = 0; i < count; i++) {
        if (search == id[i]) {
            cout << subject[i] << " " << mark[i] << endl;
            //cout << id2[i]<<endl;
        }
    }
}

Please don't use parallel arrays. Your issue is an example of the complexity of using parallel arrays. Use a collection of structures:

struct Student
{
    std::string subject;
    std::string id;
    double mark;

    friend std::istream& operator>>(std::istream& input, Student& s);
};

std::istream& operator>>(std::istream& input, Student& s)
{
    input >> s.subject;
    input >> s.id;
    input >> s.mark;
    input.ignore(1000, '\n'); // ignore to the end of the line.
    return input;
}

std::vector<Student> database;
Student s;
std::string text_line;
while (std::getline(cin, text_line))
{
    if (text_line == "END")
    {
        break;
    }
    std::istringstream text_stream(text_line);
    text_line >> s;
    database.push_back(s);
}

The above code reads in a record as a structure and appends to the database.

Here's a function to order by subject:

bool Order_By_Subject(const Student& a, const Student& b)
{
    return a.subject < b.subject;
}

The sorting by subject is as simple as:

std::sort(database.begin(), database.end(), Order_By_Subject);

Sorting by other fields (members) can be accomplished by writing addition ordering functions and supplying to std::sort like the above example.

This is a lot simpler than using multiple arrays.

Edit 1: Changing sort order
Ordering descending by subject can be accomplished by changing the comparision statement:

bool Order_Descending_By_Subject(const Student& a, const Student& b)
{
    return a.subject > b.subject;
}

Then calling the sort function:

std::sort(database.begin(), database.end(), Order_Descending_By_Subject);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM