1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
# include <iostream>
# include <fstream>
# include <string>
# include <cstdlib>
using namespace std;
// Prototypes
void Instrucciones(void);
bool CheckFile(string filename);
int ReadFile();
float *ResizeArray(float [], int, int);
bool InsertElement(float [], int &, ifstream &);
void SelectionSort(float [], int);
int main(){
// Declaring variables
int opcion;
string filename;
int size;
int defaultsize = 15;
float array[15];
bool verify;
string nombreArchivo;
string prepend("sorted-");
// Display instructions
Instrucciones();
cout << "Enter the name of the file: " << endl;
cin >> filename;
if (CheckFile(filename)==true){
ifstream ListNumbers;
size = 0;
int numbers;
ListNumbers.open(filename.c_str(),fstream::in);
while (!ListNumbers.eof()){
verify = InsertElement(array, size, ListNumbers);
}
cout << "The amount of numbers in the file is: " << size << endl;
if (verify == true){
ResizeArray(array, size, defaultsize);
}
cout << "The file " << filename << " exists. " << endl;
ListNumbers.clear();
ListNumbers.close();
nombreArchivo = prepend + filename;
ofstream SortedFile(nombreArchivo.c_str());
SelectionSort(array, size);
for (int i = 0; i < size; i++)
SortedFile << array[i] << endl;
}
else {
cout << "The file doesn't exist." << endl;
}
system("pause");
} // End main
float *ResizeArray(float array[], int size, int defaultsize){
float *arrayptr;
int resize;
resize = size + defaultsize;
arrayptr = new float[resize];
for (int i = 0; i < size; i++){
arrayptr[i] = array[i];
}
delete [] array;
array = arrayptr;
return arrayptr;
}
bool InsertElement(float array[], int & size, ifstream & ListNumbers){
float numbers;
ListNumbers >> numbers;
if (ListNumbers){
array[size] = numbers;
cout << "The numbers are: " << array[size] << endl;
size++;
}
if (size >= 15)
return true;
else
return false;
}
|