从文件中读取任意数字并对它们进行冒泡排序

时间:2014-04-04 05:15:30

标签: c++ bubble-sort

我试图编写一个程序,将文本文件(21 12 44 21 -5 63 0)中的数字读取到一个数组,并按降序对它们进行冒泡排序,并仅打印出正数。我已经尝试了一段时间,但显示的并不是我所期待的。

文本文件的内容是:

21 12 44 21 -5 63 0

完整代码:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>

using namespace std;
class bubble
{
public:

unsigned* arr;
int n;

//Constructor
bubble(const int& size) : n(size) { this->arr = new unsigned[n]; }



//function to read from file
void inputvf(istream &f)
{
    //check if file is open

    if (f.fail()) {
        cout << "\n Error in openning file!!!";
        exit(1);
    }
    for (int i = 0; i < n; i++)

        f >> arr[i];
        delete[] arr;
    //close file
    //f.close();
}



//Bubble sort function
void bubblesort()
{
    for (int i = 1; i<n; i++)//for n-1 passes
    {

        for (int j = 0; j<n - 1; j++)
        {
            if (arr[j] < arr[j + 1])
            {
                int temp;
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}
void display()
{
    cout << endl;
    cout << "----------------------\n";
    cout << "Sorted array elements \n";
    cout << "----------------------\n";
    if (arr >= 0){
        for (int j = 0; j < n; j++)
            cout << arr[j] << endl;
    }
}
};


int _tmain(int argc, _TCHAR* argv[])
{
bubble list(7);
ifstream file("Text.txt");
list.inputvf(file);
list.bubblesort();
list.display();

_getch();
return 0;
运行代码后

结果:

4277075694
1
4261281277
2880154539
2880154539
4277075694
0

我做错了什么?请帮忙

这是新代码(如下):

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>

using namespace std;
class bubble
{
public:
//Array of integers to hold values
int* arr;

//Number of elements in array 
int n;

//Constructor
bubble(const int& size) : n(size) { this->arr = new int[n]; }



//function to read from file
void inputvf(istream &f)
{
    //check if file is open

    if (f.fail()) {
        cout << "\n Error in openning file!!!";
        exit(1);
    }
    for (int i = 0; i < n; i++)

        f >> arr[i];
        //delete[] arr;
    //close file
    //f.close();
}



//Bubble sort function
void bubblesort()
{
    for (int i = 1; i<n; i++)//for n-1 passes
    {
        for (int j = 0; j<n - 1; j++)
        {
            if (arr[j] < arr[j + 1])
            {
                int temp;
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}
void display()
{
    cout << endl;
    cout << "----------------------\n";
    cout << "Sorted array elements \n";
    cout << "----------------------\n";
    if (arr >= 0){
        for (int j = 0; j < n; j++)
            cout << arr[j] << endl;
    }
}
};


int _tmain(int argc, _TCHAR* argv[])
{
bubble list(7);
ifstream file("Text.txt");
list.inputvf(file);
list.bubblesort();
list.display();

_getch();
return 0;
}

1 个答案:

答案 0 :(得分:2)

两个问题:

inputvf

delete[] arr;

此时你不应该删除数组 - 你还没有开始使用它。

此声明:

unsigned* arr;

表示您的所有输入都是unsigned,这意味着-1被读取为4294967291,因此将被视为一个大数字。将数组更改为普通int,然后使用if测试在输出时忽略负数。

相关问题