-
-
Save kodieGlosser/85286d4325a5501568e2 to your computer and use it in GitHub Desktop.
Updates files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // InsertAlgorithm.cpp : Defines the entry point for the console application. | |
| // | |
| #include "stdafx.h" | |
| #include "InsertAlgorithm.h" | |
| using namespace std; | |
| #define sourcefilepath "unsorted.txt" | |
| #define destinationfilepath "sorted.txt" | |
| int main(int argc, char* argv[]) { | |
| InsertAlgorithm insertAlgorithm; | |
| for (int i = 1; i < argc - 1; i++){ | |
| if (argv[i + 1] == string("-d")) | |
| { | |
| insertAlgorithm.m_duplicates = true; | |
| } | |
| if (argv[i] == string("-i")) { | |
| insertAlgorithm.Insertsort(argv[argc-1]); | |
| } | |
| else | |
| cout << "Please enter a valid command" << '\n'; | |
| } | |
| return 0; | |
| } | |
| void InsertAlgorithm::WriteToFile(std::vector<int> myVector) | |
| { | |
| ofstream myFile("sorted.txt"); | |
| for (int j = 0; j < myVector.size(); j++) | |
| { | |
| myFile << myVector[j] << '\n'; | |
| } | |
| myFile.close(); | |
| return; | |
| } | |
| void InsertAlgorithm::Insertsort(char* filePath) | |
| { | |
| std::vector<int> unsortedVector = openFile(filePath); | |
| if (!unsortedVector.empty()) | |
| { | |
| int item = 0; int steps = 0; | |
| for (int i = 0; i < unsortedVector.size(); i++) | |
| { | |
| item = unsortedVector[i]; | |
| steps = i - 1; | |
| while (steps >= 0 && (unsortedVector[steps] > item)) | |
| { | |
| unsortedVector[steps + 1] = unsortedVector[steps]; | |
| steps--; | |
| } | |
| unsortedVector[steps + 1] = item; | |
| } | |
| WriteToFile(unsortedVector); | |
| } | |
| } | |
| std::vector<int> InsertAlgorithm::openFile(char* file) | |
| { | |
| std::vector<int> unsortedVector; | |
| ifstream myFile; | |
| myFile.open (file); | |
| string line; | |
| bool isThereDuplicates = false; | |
| if (myFile.is_open()) | |
| { | |
| try | |
| { | |
| while (getline(myFile, line)){ | |
| if (m_duplicates) | |
| isThereDuplicates = FindDuplicatesInVector(line, unsortedVector); | |
| if (!isThereDuplicates) | |
| unsortedVector.push_back(atoi(line.c_str())); | |
| isThereDuplicates = false; | |
| } | |
| } | |
| catch (...) | |
| { | |
| cout << "Error reading file. \n Make sure that it contains only integers." << endl; | |
| unsortedVector.clear(); | |
| return unsortedVector; | |
| } | |
| } | |
| else | |
| { | |
| cout << "File does not exist" << endl; | |
| return unsortedVector; | |
| } | |
| myFile.close(); | |
| return unsortedVector; | |
| } | |
| bool InsertAlgorithm::FindDuplicatesInVector(std::string line, std::vector<int> unsortedvector) | |
| { | |
| for (int i = 0; i < unsortedvector.size(); i++) | |
| { | |
| if (unsortedvector[i] == atoi(line.c_str())) | |
| return true; | |
| } | |
| return false; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "stdafx.h" | |
| #include <iostream> | |
| #include <string> | |
| #include <fstream> | |
| #include <vector> | |
| #include <sstream> | |
| class InsertAlgorithm | |
| { | |
| public: | |
| InsertAlgorithm() { m_duplicates = false; }; // constructor | |
| ~InsertAlgorithm() {}; // de constructor | |
| void WriteToFile(std::vector<int> myVector); | |
| void Insertsort(char*); | |
| std::vector<int> openFile(char* file); | |
| bool FindDuplicatesInVector(std::string line, std::vector<int> unsortedvector); | |
| bool m_duplicates; | |
| private: | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment