2015-01-14 6 views
-4

У меня есть bubble sort программа, которая сортирует массив в по возрастанию.сохранить отсортированные элементы в текстовом файле C++

Как сохранить в TEXT FILE отсортированные элементы и поместить запятые (,) все другие числа ex. (- 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; 
}   
+0

проводки весь код не было необходимости. Вы можете просто спросить, как сохранить массив 'int' для файла и показать, что вы пробовали до сих пор. – CoryKramer

+2

Это углубление ... пожалуйста, исправьте его – Borgleader

+1

Поток - это поток - это поток. Все текстовые потоки работают одинаково, независимо от того, является ли это 'std :: cout' или некоторым файловым потоком. –

ответ

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); 
Смежные вопросы