2016-07-25 79 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

相关问题