2013-01-24 111 views
0

我有一个部分工作的选择排序算法,我使用它按年龄对类对象进行排序,当它这样做时,它会正确排序某些内容,但无法排序第一个元素。还有一种方法可以使用该类的赋值运算符来使其更容易一些。选择排序类对象数组

感谢

下面

是我的全码:

#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; 

    } 



} 
+0

为什么我会得到至少差评发表评论,如果您查看的问题告诉我什么,我做错了或点我在正确的方向 – tarantino

+0

即使我没有我会很乐意发表评论downvote。 SO是针对特定的个人编程问题的家。我无法辨别你的一个问题是什么。当我猜测你的一个问题是什么时,我不觉得你正在努力具体。形式问题“这是我的整个计划,你找出问题所在”似乎在这里得到了低估。尝试用传达问题的最小示例代码提出一个明确的问题。这将表明你也想努力获得答案。 –

回答

1

你的“用户”类是足以使,因为它的所有成员都是纯OLD-它不需要重载赋值运算符简单数据类型或已经有自己的赋值运算符(如字符串)。我建议从这个类中删除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); 

} 
+0

谢谢我采取了你的C战略,并实施它,它的工作,再次感谢 – tarantino