将已排序的元素保存在文本文件c ++中

时间:2015-01-14 16:11:46

标签: c++ sorting

我有冒泡排序程序,该程序在升序中对数组进行排序。

如何在TEXT FILE中保存已排序的元素,并在每个其他数字(。-1,0,1,2,3,4)中放入逗号(,)?

文本文件中是已排序的元素。 (-1,0,1,2,3,4)

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

int compare (int, int);
void sort(int[], const int);
int compare(int x, int y){
    return(x > y);
}
void swap(int *x, int *y){
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
void display(int array[], int n){
    for (int i = 0; i<n; i++) {
        cout << array[i]<<" ";
    }
    cout<<endl;
}
void sort(int table[], const int n) {
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n-1; j++) {
            if(compare(table[j],table[j+1]))
                swap(&table[j],&table[j+1]);
        }
    }
}
int main(){
    int quantity;
    int* tab;
    ofstream outfile;
    cout<<"Enter number of element: ";
    cin>>quantity;
    tab = new int [quantity];
    cout<<"Element:\n\n"<<endl;
    for(int i=0; i < quantity; i++){
        int x = i;
        cout<<"#"<<++x<<":";
        cin>>tab[i];
    }
    sort(tab, quantity);
    cout<<"The Sorted Elements are: ";

    display(tab, quantity);
    cout<<endl;
    system ("pause");
    return 0;
}          

2 个答案:

答案 0 :(得分:0)

使用cout writes to a file

而不是filestream

答案 1 :(得分:0)

以下是完整的代码:

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

int compare(int, int);
void sort(int[], const int);
int compare(int x, int y){
    return(x > y);
}
void swap(int *x, int *y){
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
void display(int array[], int n){
    for (int i = 0; i<n; i++) {
        cout << array[i] << " ";
    }
    cout << endl;
}
void writeToFile(int array[], int n){
    ofstream myfile;
    myfile.open("example.txt");
    for (int i = 0; i<n; i++) {
        myfile << array[i];
        if (i != n - 1){
            myfile << ", ";
        }
    }
    myfile.close();
}
void sort(int table[], const int n) {
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n - 1; j++) {
            if (compare(table[j], table[j + 1]))
                swap(&table[j], &table[j + 1]);
        }
    }
}
int main(){
    int quantity;
    int* tab;
    ofstream outfile;
    cout << "Enter number of element: ";
    cin >> quantity;
    tab = new int[quantity];
    cout << "Element:\n\n" << endl;
    for (int i = 0; i < quantity; i++){
        int x = i;
        cout << "#" << ++x << ":";
        cin >> tab[i];
    }
    sort(tab, quantity);
    cout << "The Sorted Elements are: ";

    display(tab, quantity);
    writeToFile(tab, quantity);
    cout << endl;
    getchar();
    getchar();
    //system("pause");
    return 0;
}

我已经定义了写入文件的函数:

void writeToFile(int array[], int n){
    ofstream myfile;
    myfile.open("example.txt");
    for (int i = 0; i<n; i++) {
        myfile << array[i];
        if (i != n - 1){
            myfile << ", ";
        }
    }
    myfile.close();
}

此函数采用数组和数组大小,如果元素不是最后一个元素,则用,分隔连续元素。

main()中,我就像你的display()函数一样调用它:

writeToFile(tab, quantity);