簡體   English   中英

在C ++中使用冒泡排序對結構數組的成員進行排序

[英]Sorting a member of a structure array using bubble sort in C++

我正在嘗試使用冒泡排序從最小到最大對我的數據類型Student數組student_database及其成員ID數組進行排序。 我從C ++教科書中復制了一個示例。 該程序可以編譯,但是對數組不執行任何操作。 我需要使用指針嗎? 我很困惑,謝謝。

using namespace std;
#include <iostream>
#include <string>
#include <fstream>

struct Student
{
    int ID;
    int examscore[3];
    int examtotal;
    string lettergrade;
};

void inputinfo(Student[], int&);
void lettergrade(Student[], int);
void countgrades(Student[], int);
void IDsort(Student[], int);
void sorthi(Student[], int);
void sortlow();
void maxexam(Student[], int, int);
void outputall(Student[], int);
void outputstudent(Student[]);
ifstream infile;
ofstream outfile;

int main()
{
    string filename;
    int numofstudents = 0;
    int operation;
    int user;
    cout << "Enter the filename: ";
    cin >> filename;
    infile.open(filename);
    if (infile.fail()) // checks to see if file is opened correctly
    {
        cout << "FILE_OPEN_FAILURE > CHECK_TO_SEE_IF_THE_FILE_IS_IN_THE_SAME_FOLDER \n_AS_YOUR_PROGRAM_FILE \nCHECK_YOUR_SPELLING_AS_WELL" << endl;
    }

    Student student_database[300];
    inputinfo(student_database, numofstudents);
    outputall(student_database, numofstudents);
    cout << endl << "Class size of: " << numofstudents << endl;

    do {
        cout << "Welcome. Enter a number from the menu to display the requested information." << endl << "---------------------------------------------------------" << endl;
        cout << "1. Student with the highest score on exam 1" << endl;
        cout << "2. Student with the highest score on exam 2" << endl;
        cout << "3. Student with the highest score on exam 3" << endl;
        cout << "4. Students ID in ascending numerical order" << endl;
        cout << "5. Sort total exam scores from least to greatest" << endl;
        cout << "6. Sort total exam scores from greatest to least" << endl;
        cout << "7. Total number grade results for the entire class" << endl;
        cin >> operation;
        cout << endl;

        switch (operation)
        {
        case 1:
            maxexam(student_database, numofstudents, operation);
            break;
        case 2:
            maxexam(student_database, numofstudents, operation);
            break;
        case 3:
            maxexam(student_database, numofstudents, operation);
            break;
        case 4:
            IDsort(student_database, numofstudents);
            break;
        case 5:
            sorthi(student_database, numofstudents);
            break;
        case 6:
            //sortlow();
            break;
        case 7:
            countgrades(student_database, numofstudents);
            break;
        default:
            break;
        }
        cout << "Would you like to request more information?";
        cout << endl << "1. Yes?   0. No?" << endl;
        cout << "Answer: ";
        cin >> user;
        cout << endl << endl;
    } while (user == 1);
}

void IDsort(Student student_database[], int numofstudents)
{
    bool swap = false;
    Student temp;
    while (!swap)
    {
        swap = true;
        for (int i = 0; i < (numofstudents - 1); i++)
        {
            if (student_database[i].ID > student_database[i + 1].ID)
            {
                temp = student_database[i];
                student_database[i] = student_database[i + 1];
                student_database[i] = temp;
                swap = false;
            }
        }
    }
}

此交換是錯誤的:

temp = student_database[i];
student_database[i] = student_database[i + 1];
student_database[i] = temp;

您更改了student_database[i]但隨后將其重新分配為其先前的值。 您應該更新student_database[i + 1]

student_database[i + 1] = temp;

最好還是使用std::swap()

std::swap(student_database[i], student_database[i + 1]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM