2016-07-25 65 views
2

基本上,我需要讀取一個文本文件並將它們插入到列表中的ascending order上。如何對整數進行排序並將它們放在列表中

該文本文件爲,

4 1 9 11 0 15 23 2 7 8 17 21. 

和我需要把他們的列表上像

問題說

  • 將第一項插入空白列表中。 //哪個將是4

  • 對於每一個連續的文件:

    If it is smaller than the first item, insert it at position 0.

    Otherwise, scan the list from the beginning, looking for the first

    item whose value is greater than the current value, and insert

    the new item before that one.

我認爲我需要通過比較對這些問題進行排序,但我不能提出清晰的想法。你可以幫我嗎 ?

(因爲教授是用他自己的IList.hIList.cpp文件,我可以使用所有的功能是插入和刪除。)

=================== ================================================== ====

我試過了,

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

int main(int argc, char* argv[]) 
{ 


ifstream inf(argv[1]); 
IList t; 
int i1; 
int i2; 
int i3; 
int i4; 
int i5; 
int i6; 
int i7; 
int i8; 
int i9; 
int i10; 
int i11; 
int i12; 

//It is reading each integers from the text file and name it i1, i2, i3.. 
// The text file is.. 
// 4 1 9 11 0 15 23 2 7 8 17 21. 
// i1 is going to be 4 i1 = 4 

inf >> i1 >> i2 >> i3 >> i4 >> i5 >> i6 >> i7 >> i8 >> i9 >> i10 >> i11 
>> i12; 



//I inserted the first value which is 4 
t.insert(i1, 0); 

// comparison will start from here.. 

// when i2 is smaller than i1, we are putting them on the left. 

if ((i2 < i1)) 
{ 
    t.insert(i2, 0); 
} 

// when i2 is greater than i1, we are putting them on the right. 

if ((i2 > i1)) 
{ 
    t.insert(i2, 1); 
} 
+2

使用數組('的std :: array',甚至C數組)將解決*所有*您的問題:) – Rakete1111

+2

[或使用'的std :: list']( http://en.cppreference.com/w/cpp/container/list)的說明似乎建議。 – user4581301

+0

使用列表的任何具體原因?你應該使用'std :: set ' – cplusplusrat

回答

1

您可以使用類似以下內容:

#include <fstream>  
#include <iostream>  
#include <iterator> 
#include <list> 
#include <algorithm> 

using namespace std; 

int main(int argc, char *argv[]) 
{ 
    ifstream f(argv[1]); 
    istream_iterator<int> b(f); 
    istream_iterator<int> e; 

    list<int> l; 
    copy(b, e, back_inserter(l)); 

    l.sort(); 
    for(auto v: l) 
     cout << v << endl; 
} 

說明

ifstream f(argv[1]); 
    istream_iterator<int> b(f); 
    istream_iterator<int> e; 

    list<int> l; 
    copy(b, e, back_inserter(l)); 

副本從文件整數到使用istream_iteratorscopy算法的列表。

然後

l.sort() 

排序使用list的方法的項。

+0

如果@sungch這樣做,我相信他可能會問他是如何得出答案的...... –

+0

哦,這是作業嗎?沒有看到。 –

+0

我認爲「問題所在」和「因爲教授」把它拿走了:-) –

0

您應該實現的算法稱爲「插入排序」。查看維基百科文章:https://en.wikipedia.org/wiki/Insertion_sort瞭解更多詳情。

它的基本功能是將第一個元素放入列表中,然後逐個遍歷列表,直到找到它所屬的位置。

我會做的第一件事就是創建一個列表,而不是使用所有這些interger字段(這使得您的代碼可用於多於或少於12個元素的列表)。

// ... 
// read values from file 
IList unsorted; 
while (!inf.eof()) { // checks if end of file is reached 
    unsorted.insert((int)inf.getline()); 
} 
inf.close(); 

接下來,創建一個新的列表,它包含排序後的列表。

IList sorted; 
sorted.insert(unsorted.get(0), 0); // insert first element 

你的任務說,遍歷未排序列表中的所有元素。這顯然要求for循環。你的代碼表明你可能對此沒有經驗。請檢查控制結構。這些是絕對必要的。但讓我幫你解決這個問題。

// for each successive item 
for (int i = 1; i < unsorted.length(); i++) { // maybe there is another method to check unsorted's length ?! 
    int nextValue = unsorted.get(i); 
    j = i; 

    // as long as the current unsorted element is smaller then the previous element in the sorted list, swap these elements 
    while() { 
     int temp = sorted.get(j-1); 
     sorted.insert(temp, j); 
     j--; 
     sorted.insert(nextValue); 
    } 
} 

我還沒有檢查代碼是否編譯或產生任何運行時錯誤。但這更多的是關於這個概念。希望這可以幫助!

最佳, 托馬斯

+0

非常感謝你的先生。 – sungch

+0

也許將問題標記爲answerd然後? ;) – Nopeman

相關問題