Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- ifstream fin;
- ofstream fout;
- int inputData() {
- int n = 0;
- bool isIncorrect;;
- do {
- cin >> n;
- isIncorrect = false;
- if (cin.fail()) {
- cout << "Please, enter an integer number:" << endl;
- isIncorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isIncorrect);
- return n;
- }
- bool choose() {
- int inputNumber;
- bool isIncorrect;
- const int MIN_NUM = 0;
- const int MAX_NUM = 1;
- do {
- inputNumber = inputData();
- isIncorrect = false;
- if (cin.fail()) {
- isIncorrect = true;
- cout << "Please, enter a number." << endl;
- cin.clear();
- while (cin.get() != '\n');
- }
- if (!isIncorrect && (inputNumber < MIN_NUM || inputNumber > MAX_NUM)) {
- cout << "You are out of input range!" << endl;
- isIncorrect = true;
- }
- } while (isIncorrect);
- if (inputNumber == 1)
- return true;
- else
- return false;
- }
- int inputArraySize() {
- bool isIncorrect;
- int num = 0;
- const int MIN_SIZE = 2;
- do {
- num = inputData();
- isIncorrect = false;
- if (num < MIN_SIZE) {
- cout << "Please, enter a number > 2:" << endl;
- isIncorrect = true;
- cin.clear();
- while (cin.get() != '\n');
- }
- } while (isIncorrect);
- return num;
- }
- int* inputArray(int num) {
- int* arr = new int[num];
- for (int i = 0; i < num; i++)
- arr[i] = inputData();
- return arr;
- }
- int* sortArrayBySelectionSort(int* arr, int num) {
- cout << "Sort stages:" << endl;
- int min;
- for (int i = 0; i < num - 1; i++) {
- for (int j = i + 1; j < num; j++)
- if (arr[j] < arr[i]) {
- min = arr[j];
- arr[j] = arr[i];
- arr[i] = min;
- }
- cout << "Stage " << (i + 1) << ": ";
- for (int j = 0; j < num; j++)
- cout << arr[j] << " ";
- cout << endl;
- }
- return arr;
- }
- void consoleOutput(int* arr, int num) {
- for (int i = 0; i < num; i++)
- cout << arr[i] << ' ';
- cout << endl;
- }
- string inputFilePath() {
- const int EXTENSION_SIZE = 4;
- string path;
- string ext;
- bool isIncorrect;
- do {
- isIncorrect = false;
- cout << "Input path to file: " << endl;
- cin >> path;
- fin.open(path);
- if (path.size() > EXTENSION_SIZE) {
- ext = path.substr(path.size() - EXTENSION_SIZE, EXTENSION_SIZE);
- }
- else {
- cout << "Incorrect file name." << endl;
- isIncorrect = true;
- } if (!isIncorrect && ext != ".txt") {
- cout << "Must have .txt!" << endl;
- isIncorrect = true;
- }
- else if (!isIncorrect && !fin.is_open()) {
- cout << "Wrong way to file." << endl;
- isIncorrect = true;
- }
- } while (isIncorrect);
- fin.close();
- return path;
- }
- int inputSizeOfArrayFromFile(string path) {
- int num;
- bool isIncorrect;
- const int MIN = 2;
- do{
- isIncorrect = false;
- fin.open(path);
- fin >> num;
- if (num < MIN) {
- isIncorrect = true;
- cout << "Matrix size should be at least 2" << endl;
- path = inputFilePath();
- }
- } while (isIncorrect);
- return num;
- }
- int* inputArrayFile (string path, int num){
- ifstream fileIn(path);
- bool isIncorrect = false;
- int* arr = new int [num];
- for (int i = 0; i < num; i++) {
- do {
- isIncorrect = false;
- fin >> arr[i];
- if (fin.fail()) {
- cout << "Reading of array elements failed." << endl;
- isIncorrect = true;
- path = inputFilePath();
- }
- } while (isIncorrect);
- }
- return arr;
- }
- void fileOutput(int* arr, string path, int num) {
- fout.open(path);
- fout << "Sorted array: " << endl;
- for (int i = 0; i < num; i++)
- fout << arr[i] << " ";
- cout << "Successful output in file." << endl;
- fout.close();
- }
- /* в c++ main отличается от остальных языков, так как нет функций input и output(в отличие от delphi и java).
- Это из-за отсутствия функции, возвращающей длину массива(High(массив) или массив.length в других языках). */
- int main() {
- cout << "Selection sort. Demonstration." << endl;;
- cout << "Enter type of input: " << '\n' << "1 is console input, 0 is file input." << endl;
- int num;
- int* arr;
- string path;
- bool chose = choose();
- if (chose) {
- cout << "Input size of array:" << endl;
- num = inputArraySize();
- cout << "Input array elements:" << endl;
- arr = inputArray(num);
- cout << "Unsorted array:" << endl;
- consoleOutput(arr, num);
- }
- else {
- path = inputFilePath();
- num = inputSizeOfArrayFromFile(path);
- arr = inputArrayFile(path, num);
- }
- arr = sortArrayBySelectionSort(arr, num);
- cout << "Enter type of output: " << '\n' << "1 is console output, 0 is file output." << endl;
- chose = choose();
- if (chose) {
- cout << "Sorted array:" << endl;
- consoleOutput(arr, num);
- }
- else {
- path = inputFilePath();
- fileOutput(arr, path, num);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement