2015-01-04 77 views
0

我想執行選擇排序來排序航班的出發時間,但它不會打印出結果(我不確定它是否正確)。對於這個漫長而愚蠢的問題抱歉,我是編程新手。這裏是我現在做的代碼:使用列表實現選擇排序C++

// Sort class 
class Sort 
{ 
protected: 
    // number of comparisons performed in sort function 
    unsigned long num_cmps; 

public: 
    // main entry point 
    virtual void sort(std::vector<Flight>& data) = 0; 
    // returns false if not sorted true otherwise 
    bool testIfSorted(const std::vector<Flight>& data); 
    // returns number of comparisons 
    unsigned long getNumCmps(); 
    // resets the number of comparisons 
    void resetNumCmps(); 
}; 

// SelectionSort class 
class SelectionSort : public Sort 
{ 
public: 
    // main entry point 
    void sort(std::vector<Flight>& data); 
}; 

// BubbleSort class 
class BubbleSort : public Sort 
{ 
public: 
    // main entry point 
    void sort(std::vector<Flight>& data); 
}; 

#include "Sort.h" 

using namespace std; 


unsigned long Sort::getNumCmps() 
{ 
    return num_cmps; 
} 


void Sort::resetNumCmps() 
{ 
    num_cmps = 0; 
} 

void Menu::selection_sort(){ 
    system("cls"); 
    ifstream in("inputFileExample.txt"); 
    if (!in) 
    { 
     cerr << "ERROR: wrong input file name!"; 
     exit(-1); 
    } 
    SelectionSort(); 
} 

void SelectionSort::sort(std::vector<Flight>& data){ 
    for (int i = 0; i < (num_cmps - 1); i++) 
    { 
    int smallest = i; 
     for(int j = (i+1); j<num_cmps; j++) 
     { 
      if(data[j] < data[smallest]) 
      { 
       smallest = j; 
      } 
     } 
    num_cmps++; 
    cout << num_cmps; 
    } 
} 
+0

哪裏是你的'主' – mangusta 2015-01-04 11:09:40

+1

那麼,*你*打印結果在任何地方? – 2015-01-04 11:10:29

+0

試圖在SelectionSort()中打印它,並且我在Menu :: selection_sort()中調用了Selection排序,但是無論我嘗試什麼,都沒有出現,甚至沒有出現「Hello world」。 主要是菜單,我沒有發佈它,因爲它是無關的代碼工作。 – Lianna 2015-01-04 11:17:13

回答

0

本聲明

SelectionSort(); 

創建SelectionSort類型的臨時對象,僅此而已。

你實際上沒有讀取任何事情從文件中,你沒有一個向量來排序,你不要調用排序函數。

+0

謝謝我讓它工作。 :) – Lianna 2015-01-04 11:33:17