diff --git a/CMakeLists.txt b/CMakeLists.txt index 3db357b5c1bf0347a06f69273eb6f1ed28e5cb5f..124269e55072b79398a9a328cda68618007aea5f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ project(magicCards) set(CMAKE_CXX_STANDARD 20) -add_executable(magicCards main.cpp aufgabe1.h aufgabe1.cpp Cards.h) +add_executable(magicCards main.cpp aufgabe1.h aufgabe1.cpp Cards.h functions.h functions.cpp) diff --git a/Cards.h b/Cards.h index 0f01b5e27181134943a93911096640eee183f4a0..76795bbe2579fc10599320b47c5e2157c524f4a1 100644 --- a/Cards.h +++ b/Cards.h @@ -8,10 +8,11 @@ //Aufgabe Nr.2 struct Cards { + //cmc and count are also strings to make it easier for the getListOfCards function public: std::string name; std::string mana; - int cmc; + std::string cmc; std::string type; std::string count; diff --git a/functions.cpp b/functions.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b7853f309dfd94cdd12714cdf563e34524c2978d --- /dev/null +++ b/functions.cpp @@ -0,0 +1,67 @@ +// +// Created by Eray düzenli on 13.04.23. +// +#include <iostream> +#include "functions.h" +#include <fstream> +#include <stdexcept> +#include <sstream> +#include <vector> + +//std::list<Cards> getListOfCards(const std::string& filename) +std::list<Cards> getListOfCards(const std::string& filename) { + std::ifstream file(filename); + if(!file) { + throw std::runtime_error("Error: Unable to open file " + filename); + } + //temp string to save line + std::string temp; + //Output + std::list<Cards> CardsList; + + //get every line of scrambled.txt + while (std::getline(file, temp)) { + std::stringstream streamData(temp); + //"cut" the string at the vertical bars(|) + char delim ='|'; + //string vector to hold the cut parts + std::vector<std::string> stringVector; + //temp to store Card attributes + std::string attr; + + //go over every line, cut line accordingly and push it at the back of the vector + while (getline(streamData, attr, delim)) { + stringVector.push_back(attr); + } + //assign every cut out part to the according attribute + std::string name = stringVector[0]; + std::string mana = stringVector[1]; + std::string cmc = stringVector[2]; + std::string type = stringVector[3]; + std::string count = stringVector[4]; + + //create card object and assign attributes to it + Cards card; + card.cmc = cmc; + card.mana = mana; + card.name = name; + card.type = type; + card.count = count; + + //push Card object at the end of the List + CardsList.push_back(card); + } + + //close .txt file + file.close(); + + + /* just for testing + for (const auto& card : CardsList) { + std::cout << card.name << ", " << card.mana << ", " << card.cmc << ", " << card.type << ", " << card.count << std::endl; + } + */ + + //return the cards as a list + return CardsList; +} \ No newline at end of file diff --git a/functions.h b/functions.h new file mode 100644 index 0000000000000000000000000000000000000000..4d512b22b580f4954272a00e7ec3eea7fc1288a6 --- /dev/null +++ b/functions.h @@ -0,0 +1,14 @@ +// +// Created by Eray düzenli on 13.04.23. +// +#pragma once +#include <string> +#include <iostream> +#include <list> +#include "Cards.h" + + +std::list<Cards> getListOfCards(const std::string& filename); + + + diff --git a/main.cpp b/main.cpp index d97758cf327fbd3de7c5b5d7283e0f074868079f..8409859b6ec9a9249f8c81f333f8e67cbc717ae4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,10 @@ #include <iostream> #include "aufgabe1.h" #include "Cards.h" +#include "functions.h" int main() { + /* std::cout << "Info 3 Projekt Nr.1" << std::endl; levenshteinDistance("kitten", "sitting"); levenshteinDistance("industry", "interests"); @@ -16,6 +18,9 @@ int main() { s.name = "peter"; s.type = "uwu"; std::cout << s.name << s.count << s.type << s.cmc << std::endl; + //To-Do, only absolute path works + */ + getListOfCards("/Users/erayduzenli/CLionProjects/magicCards/scrambled.txt"); return 0; }