From 6e97f8d08319138c3443a8ed7ec731ebb6bafb1a Mon Sep 17 00:00:00 2001 From: Eraser <erayduezenli@googlemail.com> Date: Thu, 13 Apr 2023 15:01:57 +0200 Subject: [PATCH] Aufgabe 3 done --- CMakeLists.txt | 2 +- Cards.h | 3 ++- functions.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ functions.h | 14 +++++++++++ main.cpp | 5 ++++ 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 functions.cpp create mode 100644 functions.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3db357b..124269e 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 0f01b5e..76795bb 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 0000000..b7853f3 --- /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 0000000..4d512b2 --- /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 d97758c..8409859 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; } -- GitLab