对文本文件中的数据进行排序

问题描述:

我需要编写一个程序,该程序从文本文件中读取未知数量的数字数据,然后使用排序参数对数字进行排序。我一直在尝试了几个小时,当你结束阅读文件的末尾,我不能让程序工作对文本文件中的数据进行排序

void sort(double &, double arr[], int s); 

int main() 
{ 
    fstream myfile; 
    int size, i = 0; 
    const int n = 1000; 
    double x[n]; 

    //reading data 
    myfile.open("data.txt"); 
    if (myfile.fail()) 
    { 
     cout << "Error Opening File" << endl; 
     exit(1); 
    } 
    while (!myfile.eof()) 
    { 
     myfile >> x[i]; 
     cout << x[i] << endl; 
     i++; 
    } 
    size = i - 1; 
    cout << size << " number of values in file" << endl; 
    myfile.close(); 

    i = 0; 
    while (size > i) 
    { 
     cout << x[i] << endl; 
     i++; 
    } 
    //sorting 
    sort(x[n], x[n], size); 
    i = 0; 
    while (size > i) 
    { 
     cout << x[i] << endl; 
     i++; 
    } 
    return 0; 
} 

void sort(double &, double arr[], int s) 
{ 
    bool swapped = true; 
    int j = 0; 
    int tmp; 
    while (swapped) 
    { 
     swapped = false; 
     j++; 
     for (int i = 0; i < s - j; i++) 
     { 
      if (arr[i] > arr[i + 1]) 
      { 
       tmp = arr[i]; 
       arr[i] = arr[i + 1]; 
       &[i + 1] = &tmp; 
       swapped = true; 
      } 
     } 
    } 
} 
+0

欢迎来到*!我建议在编程语言的答案中添加一个标签,以吸引该领域专家的答案。 – philtune 2014-12-01 22:50:19

+0

@philtune我只是为他做的。另外斯蒂芬,目前的代码问题是什么? – Emz 2014-12-01 22:51:19

+0

我要建议'&[i + 1] =&tmp'不会做太多好事。 'std :: swap'会比那三个普通代码更好,老实说,std :: sort'也会比那个冒泡排序更好。而这:'while(!myfile.eof())'[**是错误的。**](http://*.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-认为是错误的) – WhozCraig 2014-12-01 22:54:36

while (!myfile.eof()) 

几乎总是错的, Why is iostream::eof inside a loop condition considered wrong? 。改用

while (myfile >> x[i]) 

在你的情况,你可以声明x作为std::vector,像

std::vector<double> x; 

读取内容到载体中,然后使用

std::sort(x.begin(), x.end()); 

没有更加简单。

而对于一个完整的C++标准库的解决方案,你可以使用迭代器和排序算法,如

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

int main() 
{ 
    std::fstream myfile("data.txt"); // should also test if it's open correctly 
    std::vector<double> x(std::istream_iterator<double>(myfile), {}); 
    std::sort(x.begin(), x.end()); 

    for(const auto& elem: x) 
     std::cout << elem << " "; 
} 

你可以很容易的代码地图上方的功能。我不知道您的排序参数是什么意思。如果您需要能够进行排序上升/下降,您可以使用std::lessstd::greater(需要#include <functional>),作为std::sort第三个参数,如

std::sort(x.begin(), x.end(), std::greater<double>()); // sorts in descending order 

或编写自己的比较仿函数/λ功能。

+0

我将我的代码更改为这个 – 2014-12-01 23:20:30

+0

fstream myfile; \t int size,i = 0; \t const int n = 1000; \t std :: vector x; \t //读取数据 \t myfile.open(“data.txt”); \t if(myfile.fail()){ \t \t cout > X [I]; \t \t cout i) \t { \t \t cout I) \t { \t \t COUT 2014-12-01 23:21:11

+0

你知道你可以使用istream迭代器'std :: vector x((std :: istream_iterator (myfile)),std :: istream_iterator ());'并且跳过复制步骤来初始化该向量。 – WhozCraig 2014-12-01 23:21:28

我的C++很生疏,我没有编译器,如果遇到任何错误,我会更新它。

#include <algorithm> 
#include <iostream> 
#include <vector> 

using namespace std; 

static bool sort_function (double p, double q); 

int main() 
{ 
    fstream myfile; 

    // opening file 
    myfile.open("data.txt"); 
    if (myfile.fail()) 
    { 
     cout << "Error Opening File" << endl; 
     exit(1); 
    } 

    // reading data and storing data 
    vector<double> x; 
    double tmp; 
    while (myfile >> tmp) 
    { 
     x.push_back(tmp); 
    } 
    myfile.close(); 
    cout << x.size() << " number of values in file" << endl; 

    // unsorted print data from vector 
    for (vector<double>::iterator it = x.begin(); it != x.end(); ++i) 
    { 
     cout << *it << endl; 
    } 

    // sorted print data from vector 
    sort(x.begin(), x.end(), sort_function); 
    for (vector<double>::iterator it = x.begin(); it != x.end(); ++i) 
    { 
     cout << *it << endl; 
    } 

    return 0; 
} 

static bool sort_function (double p, double q) 
{ 
    return p > q; 
} 

与您的代码有一些差异。

  • 它使用std::vector而不是数组。它是动态的,并具有一些令人愉快的功能,如动态长度,尺寸计数器和排序。
  • 我把它分成四个代码块,打开,存储,未排序和排序。

所以它做什么

  1. 如其名之前,它会尝试加载data.txt,如果失败了它的存在。
  2. 它读取数据,将其存储在我们的临时变量tmp中,然后将其推入我们的向量x。然后继续关闭文件并打印数值x.size()
  3. 它打印未分类的矢量,就像它在代码中一样。
  4. 最后利用std::sort(begin, end, function)对向量进行排序。