日期:2014-05-20  浏览次数:20680 次

请教大家一个OOP编程问题,谢谢各位
建立一个C++ Console, 做一个篮球队员的信息系统,虽然是C++,但我的问题Java也可以回答


Number Position Name
---------- ---------- ----------
5 Center ABC
1 Guard DEF
9 Forward GHI


需要有 AddPlayer(), DeletePlayer(), Display(),
我创建了2个类,一个是class PlayerDataBase ,一个是class Player

以上我都完成了,就是在遇到如何排序的时候遇到了问题,共有3种排序要求
SortByNumber(), SortByPosition(), SortByName(), 每一种都会按照不同的
要求输出相应的内容
现在我的思路是:
把球员信息存入vector中 然后push_back进入PlayerDataBase中,可是还是感觉很迷惑。

请问我该用什么方法,数据结构?谢谢大家

------解决方案--------------------
sort by xxx 是一个动作
如果存在一个Vector中,每次sort后顺序都改变了,也就是说每次都需要重新排序。
建议你保存三个排好序的结果在三个Vecotr(或List)中,这样排序结果可重用。
相应的,三个方法为:getListOrderByNumber(),getListOrderByPosition(),getListOrderByName()。
为保证排序结果一直有效,在插入和删除队员数据的时候,同时更新维护这三个列表




------解决方案--------------------
用一个list足矣,Collection容器有一个静态方法:sort。你实现三种不同的Comparator接口就行,分别对应你的Number、Position、Name。下面是一个实例:
http://www.blogjava.net/zygcs/archive/2008/01/17/176032.html
------解决方案--------------------
楼主的重点应该不是对数据结构的选择,而是不知排序算法的实现;这在Java中,都有类库的支持,很方便;

楼主说了By name, position, Numner来排序,不知对 国际化、排序的强度、分解 等有要求;
如果只要求按某种 字符集 的字典顺序来排序,在Java中, 四楼的做法就可以了(Java采用的是Unicode字符顺序);
如果排序需要支持 国际化、排序的强度、分解,在Java中,Collator类提供了支持;

所以楼主只需实现或找个已有实现的C++排序器即可;C++中没有排序器?

Java 示例:
Java code

import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.Locale;
import java.util.Vector;

public class PlayerUtil {

    static Collator collator = Collator.getInstance(Locale.CHINA);
    static{
        collator.setStrength(Collator.SECONDARY);
        collator.setDecomposition(Collator.NO_DECOMPOSITION);
    }
    
    /**
     * 根据球队队员Name排序,对大小不敏感
     * 
     * @param players
     */
    public static void sortByName(Vector<Player> players){
        if(players == null || players.isEmpty()) return;
        Collections.sort(players, new Comparator<Player>() {
            @Override
            public int compare(Player player1, Player player2) {
                return collator.compare(player1.getName(), player2.getName());
            }
        });
    }
    
    /**
     * 根据球队队员Number排序,按字典顺序排序
     * 
     * @param players
     */
    public static void sortByNumber(Vector<Player> players){
        if(players == null || players.isEmpty()) return;
        Collections.sort(players, new Comparator<Player>() {
            @Override
            public int compare(Player player1, Player player2) {
                return player1.getNumber().compareTo(player2.getNumber());
            }
        });
    }
    
    /**
     * 根据球队队员Position排序,对大小不敏感
     * 
     * @param players
     */
    public static void sortByPosition(Vector<Player> players){
        if(players == null || players.isEmpty()) return;
        Collections.sort(players, new Comparator<Player>() {
            @Override
            public int compare(Player player1, Player player2) {
                return collator.compare(player1.getPosition(), player2.getPosition());
            }
        });
    }
    
    /**
     * 打印结果
     * 
     * @param players
     */
    public static void display(Vector<Player> players){
        if(players == null || players.isEmpty()) return;
        for(Player player : players){
            System.out.println(player);
        }
    }
    
    
    public static void main(String[] args) {
        Vector<Player> players = new Vector<Player>();
        players.add(new Player(5,"Center","ABC"));
        players.add(new Player(1,"Guard","DEF"));
        players.add(new Player(9,"Forward","GHI"));
        players.add(new Player(6,"guard","abc"));
        players.add(new Player(4,"FORWARD","Defcf"));
        players.add(new Player(2,"Guard","ghif"));
        
        System.out.println("-------------------Sort by Number : ");
        PlayerUtil.sortByNumber(players);
        PlayerUtil.display(players);
        System.out.println("-------------------End");
        
        System.out.println("-------------------Sort by Name : ");
        PlayerUtil.sortByName(players);
        PlayerUtil.display(players);
        System.out.println("-------------------End");
        
        System.out.println("-------------------Sort by Position : ");
        PlayerUtil.sortByPosition(players);
        PlayerUtil.display(players);
        System.out.println("-------------------End");
    }
}

class Player{
    private Integer number;
    private String name;
    private String position;
    
    public Player(Integer number, String name, String position){
        this.number = number;
        this.name = name;
        this.position = position;
    }

    public Integer getNumber() {
        return number;
    }

    public String getName() {
        return name;
    }

    public String getPosition() {
        return position;
    }

    @Override
    public String toString() {
        return "Player [number=" + number + ", name=" + name + ", position="
                + position + "]";
    }
}