簡體   English   中英

類對象的選擇排序數組

[英]selection sorting array of class objects

我有一個部分起作用的選擇排序算法,我用它按年齡對類對象數組進行排序時,它對某些內容進行了正確排序,但未能對第一個元素進行排序。 還有一種方法可以使用該類的賦值運算符使此操作更簡單。

謝謝

下面是我的完整代碼:

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

void storeinfo() ;
void showinfo() ;
void menu() ;
void deleteinfo() ;
void displayallinfo() ;
void selectionSort() ;
int linsearch(string val) ;


class user 
{
    string firstname, lastname, currentteam, position, status ;
    int age ;
public:
    user() {};
    user(string fname, string lname, string cteam, string pos, string stat, int age) 
    {
        setFirstName(fname);
        setLastName(lname);
        setCurrentTeam(cteam);
        setPosition(pos);
        setStatus(stat);
        setAge(age);
    } ;



    user& operator = (const user& source)
    {
        firstname = source.firstname;
        lastname = source.lastname ;
        currentteam = source.currentteam ;
        position = source.position ;
        status = source.status ;
        age = source.age ;
    }


    void setFirstName(string fname)
        {firstname = fname;}
    void setLastName(string lname)
        {lastname = lname;}
    void setCurrentTeam(string cteam)
        {currentteam = cteam;}
    void setPosition(string pos)
        {position = pos;}
    void setStatus(string stat)
        {status = stat;}
    void setAge(int _age)
        {age = _age;}

    string getFirstName()
        {return firstname ;}
    string getLastName()
        {return lastname ;}
    string getCurrentTeam()
        {return currentteam ;}
    string getPosition()
        {return position ;}
    string getStatus()
        {return status ;}
    int getAge()
        {return age ;}
};

user player[20] ;
int arrlength = 3 ;

int main()
{
    menu() ;

    cin.get() ;
    return 0 ;
}

void storeinfo()
{
    string firstname ;
    string lastname ;
    string currentteam ;
    string position;
    string status ;
    int age ;

    for (int i=0; i < 3; i++)
    {
        cout << "\n\n Enter First Name : " ; 
        cin >> firstname ;
        player[i].setFirstName(firstname) ;
        cout << "Enter Last Name : " ; 
        cin >> lastname ;
        player[i].setLastName(lastname) ;
        cout << "Enter Player's Age : " ; 
        cin >> age;
        player[i].setAge(age) ;
        cout << "Enter Current Team : " ; 
        cin >> currentteam ;
        player[i].setCurrentTeam(currentteam) ;
        cout << "Enter Position : " ; 
        cin >> position ;
        player[i].setPosition(position) ;
        cout << "Enter Status : " ; 
        cin >> status ;
        player[i].setStatus(status) ;

        cout << "\n\n\n" ;
    }

    /*cout << string(50, '\n');*/

    menu() ;

}

void showinfo()
{
    string search;
    int found ;


    cout << "Please Enter The Player's Last Name : " ;
    cin >> search ;

    found=linsearch(search);

    if (found==-1)
    {
        cout << "\n There is no player called " << search ;
    }
    else
    {
        cout << "\n First Name : " << player[found].getFirstName() << "\n" << "Last Name : " << player[found].getLastName() <<
            "\n" << "Age : " << player[found].getAge() << "\n" << "Current Team : " << player[found].getCurrentTeam() << 
            "\n" << "Position : " << player[found].getPosition() << "\n" << "Status :  " << player[found].getStatus()  << "\n\n";
    }

    cin.get() ;

    menu() ;

}

void deleteinfo()
{
    int arrlength = 3 ;
    string search ;
    int found ;

    cout << "\n Delete A Player's Information \n\n" ;
    cout << "Please Enter The Player's Last Name : " ;
    cin >> search ;

        found=linsearch(search);

    if (found==-1)
    {
        cout << "\n There is no player called " << search ;
    }
    else
    {
        for (int i=found + 1; i < arrlength; ++i)
        {
            player[i-1].setFirstName(player[i].getFirstName()) ;
            player[i-1].setLastName(player[i].getLastName()) ;
            player[i-1].setAge(player[i].getAge()) ;
            player[i-1].setCurrentTeam(player[i].getCurrentTeam()) ;
            player[i-1].setPosition(player[i].getPosition()) ;
            player[i-1].setStatus(player[i].getStatus()) ;
        }

        --arrlength ;

        cout << "\n Player has been deleted." ;

        player[arrlength].setAge(0) ;
        player[arrlength].setCurrentTeam("") ;
        player[arrlength].setFirstName("") ;
        player[arrlength].setLastName("") ;
        player[arrlength].setPosition("") ;
        player[arrlength].setStatus("");
    }

    cin.get() ;

    menu() ;
}

void displayallinfo()
{

    selectionSort();

    for (int i=0; i < 3; i++)
    {
        cout << "\n First Name : " << player[i].getFirstName() << "\n" << "Last Name : " << player[i].getLastName() <<
            "\n" << "Age : " << player[i].getAge() << "\n" << "Current Team : " << player[i].getCurrentTeam() << 
            "\n" << "Position : " << player[i].getPosition() << "\n" << "Status :  " << player[i].getStatus()  << "\n\n";
    }

    cin.get() ;

    menu() ;
}

void menu()
{
    cout << "\n\n MENU" << "\n" ;
    cout << "\n A. Store Player Information" ;
    cout << "\n B. Show Player Informaton" ;
    cout << "\n C. Delete Player Information" ;
    cout << "\n D. Display All Players Sorted By Age";
    cout << "\n Z. Exit \n\n" ;

    string x =  "";
    cin >> x ;

    if (x=="a" | x=="A")
    { 
        storeinfo() ;
    }
    else if (x=="b" | x=="B")
    {
        showinfo() ;
    }
    else if (x=="c" | x=="C")
    {
        deleteinfo() ;
    }
    else if (x=="d" | x=="D")
    {
        displayallinfo() ;
    }
    else if (x=="z" | x=="Z")
    {
        exit(0) ;
    }
    else
    {
        cout << "Invalid Choice" ;
        menu() ;
    }
}

int linsearch(string val)
{
    for (int j=0; j <= 3; j++)
    {
        if  (player[j].getLastName()==val)
         return j ;         
    }
        return -1 ;
}

void selectionSort()
{
    int i, minIndex, minValue;
    for (i = 0; i < (arrlength - 1); i++)
    {
        minIndex = i ;
        minValue = player[i].getAge() ;
        for (int index = i + 1; index < arrlength; index++)
        {
            if (player[index].getAge() < minValue)
            {
                minValue = player[index].getAge();
                minIndex = index;

            }
        }

        player[minIndex].setAge(player[i].getAge());
        player[i].getAge() == minValue;

    }



}

您的“用戶”類非常簡單,因此不需要重載的賦值運算符,因為它的所有成員都是純舊數據類型或已經擁有自己的賦值運算符(例如字符串)。 我建議從此類中刪除operator =方法,這樣您在添加新成員時就不會維護operator =方法。 如果需要,C ++會自動為您的類生成一個賦值運算符(進行成員賦值)。 當您擁有作為指針的成員時,您實際上只需要一個重載的賦值運算符,但是我離題了。

您的選擇排序功能實際上並不是對玩家進行排序。 只是在重新排列每個玩家的“年齡”值。 我認為這是您實際上要對“玩家”數組進行“排序”的操作。 請注意,這比“ C ++”更像“ C”,但請忍受 )。

void SimpleButSlowSort()
{
    bool fSorted = false;

    if (arrLength <= 1)
    {
       return;
    }

    while (fSorted)
    {
        fSorted = true;
        for (int index = 0; index < arrLength-1; index++)
        {
            if (player[index].getAge() > player[index+1].getAge())
            {
                user temp;
                temp = player[index];
                player[index] = player[index+1];
                player[index+1] = temp;
                fSorted = false;   
            }   
        }
    }
}

但是,如果您希望使用更C ++標准的方式進行排序,則可以為此使用標准庫。 但這要求玩家采用可以迭代的類型。

bool MyPlayerCompare(user& u1, user& u2) { return (u1.getAge() < u2.getAge()); }
std::vector<user> players;
void FasterSort()
{
    std::sort(players.begin(), players.end(), MyPlayerCompare);

}

暫無
暫無

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

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