Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- using namespace std;
- int takeInt(int min, int max) {
- int number = 0;
- bool isIncorrect;
- do {
- isIncorrect = false;
- cin >> number;
- if (!cin.good()){
- isIncorrect = true;
- cout << "Incorrect input!!!\n";
- while (cin.get() != '\n') {
- cin.clear();
- }
- }
- if ((!isIncorrect) && (number < min || number > max)){
- isIncorrect = true;
- cout << "Incorrect input!!!\n";
- }
- } while (isIncorrect);
- return number;
- }
- char takeSource() {
- char choice;
- cin >> choice;
- while ((choice != '1') && (choice != '2')) {
- cout << "Incorrect input!!!\nSelect the source:\n1:Console\n2:File\nEnter 1 or 2: ";
- cin >> choice;
- }
- return choice;
- }
- string takeInPath() {
- string path;
- bool isIncorrect;
- cout << "Enter file path: ";
- do {
- isIncorrect = true;
- cin >> path;
- ifstream fin(path);
- if (fin.is_open()) {
- isIncorrect = false;
- fin.close();
- }
- else {
- cout << "File is not found\nEnter file path: ";
- }
- if (!isIncorrect && ((path.length() < 4) || (!(path.substr(path.length() - 4) == ".txt")))) {
- cout << "File is found, but it is not \".txt\" type file\nEnter file path : ";
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- string takeOutPath() {
- string path;
- bool isIncorrect;
- cout << "Enter file path: ";
- do{
- isIncorrect = false;
- cin >> path;
- if ((path.length() < 4) || (!(path.substr(path.length() - 4) == ".txt"))) {
- cout << "It should be a \".txt\" file\nEnter file path: ";
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- vector<int> takeArrayFromConsole() {
- const int MIN_ELEMENT = -1000;
- const int MAX_ELEMENT = 1000;
- const int MIN_SIZE = 2;
- const int MAX_SIZE = 100;
- cout << "Enter array size (in range " << MIN_SIZE << ", " << MAX_SIZE << "): ";
- int size = takeInt(MIN_SIZE, MAX_SIZE);
- vector <int> array(size);
- for (int i = 0; i < size; i++) {
- cout << "Enter element " << (i + 1) << ": ";
- array[i] = takeInt(MIN_ELEMENT, MAX_ELEMENT);
- }
- return array;
- }
- vector <int> takeArrayFromFile(const string path) {
- ifstream fin(path);
- int size;
- fin >> size;
- vector <int> array(size);
- for (int i = 0; i < size; i++) {
- fin >> array[i];
- }
- fin.close();
- return array;
- }
- vector <int> takeArray(const char source) {
- string inPath;
- vector <int> array;
- if (source == '1') {
- array = takeArrayFromConsole();
- }
- else {
- inPath = takeInPath();
- array = takeArrayFromFile(inPath);
- }
- return array;
- }
- vector<vector<int>> findStagesOfCocktailSort(vector<int> array){
- int size = array.size();
- vector<vector<int>>answer(size);
- for (int i = 0; i < size; i++)
- answer[i].resize(size);
- for (int k = 0; k < size; k++) {
- answer[0][k] = array[k];
- }
- int counter = 0;
- int leftBorder = 0, rightBorder = size - 1;
- bool wasThereSwap = true;
- while ((leftBorder < rightBorder) && wasThereSwap) {
- wasThereSwap = false;
- for (int i = leftBorder; i < rightBorder; i++) {
- if (array[i] > array[i + 1]) {
- swap(array[i], array[i + 1]);
- wasThereSwap = true;
- }
- }
- if (wasThereSwap) {
- for (int k = 0; k < size; k++) {
- answer[counter + 1][k] = array[k];
- }
- }
- counter++;
- wasThereSwap = false;
- rightBorder--;
- for (int i = rightBorder; i > leftBorder; i--) {
- if (array[i - 1] > array[i]) {
- swap(array[i - 1], array[i]);
- wasThereSwap = true;
- }
- }
- leftBorder++;
- if (wasThereSwap) {
- for (int k = 0; k < size; k++) {
- answer[counter + 1][k] = array[k];
- }
- }
- counter++;
- }
- vector<vector<int>> allStages(counter + 1);
- for (int i = 0; i < counter + 1; i++) {
- allStages[i] = answer[i];
- }
- return allStages;
- }
- static void outputToConsole(vector<vector<int>> answer) {
- int numberOfAnswers = answer.size();
- int size = answer[0].size();
- for (int i = 0; i < numberOfAnswers; i++) {
- for (int j = 0; j < size; j++)
- cout << answer[i][j] << " ";
- cout << "\n";
- }
- }
- void outputToFile(string path, vector<vector<int>> answer) {
- ofstream fout;
- fout.open(path);
- int size = answer.size();
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++)
- fout << answer[i][j] << " ";
- fout << "\n";
- }
- fout.close();
- cout << "Done";
- }
- void output(const char source, vector<vector<int>> answer) {
- string outPath;
- if (source == '1') {
- outputToConsole(answer);
- }
- else {
- outPath = takeOutPath();
- outputToFile(outPath, answer);
- }
- }
- int main(){
- cout << "Welcome to the program that will sort an array using cocktail sort\nSelect the source for entering the array:\n1:Console\n2:File\nEnter 1 or 2: ";
- char inputSource = takeSource();
- vector<int> array = takeArray(inputSource);
- vector<vector<int>> answer = findStagesOfCocktailSort(array);
- cout << "Select the source for output:\n1:Console\n2:File\nEnter 1 or 2: ";
- char outputSource = takeSource();
- output(outputSource, answer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement