Skip to content
Snippets Groups Projects
Commit 92678ca9 authored by Eraser's avatar Eraser
Browse files

Aufgabe 6&7 done

parent 7ab6a749
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Created by Eray düzenli on 13.04.23. // Created by Eray düzenli on 13.04.23.
// //
#pragma once #pragma once
#include <string> #include <string>
...@@ -15,5 +16,4 @@ public: ...@@ -15,5 +16,4 @@ public:
std::string cmc; std::string cmc;
std::string type; std::string type;
std::string count; std::string count;
}; };
\ No newline at end of file
#include <iostream> #include <iostream>
#include "aufgabe1.h" #include "aufgabe1.h"
//calculate minimum of three values //Helper function, calculate minimum of three values
int min(int x, int y, int z) { int min(int x, int y, int z) {
if (x < y && x < z) { if (x < y && x < z) {
return x; return x;
...@@ -13,7 +13,7 @@ int min(int x, int y, int z) { ...@@ -13,7 +13,7 @@ int min(int x, int y, int z) {
} }
int levenshteinDistance(const std::string& s, const std::string& t) { int levenD(const std::string &s, const std::string &t) {
//Length of first string //Length of first string
int n = s.length(); int n = s.length();
//Length of second string //Length of second string
...@@ -35,23 +35,24 @@ int levenshteinDistance(const std::string& s, const std::string& t) { ...@@ -35,23 +35,24 @@ int levenshteinDistance(const std::string& s, const std::string& t) {
for (int i = 1; i <= n; i++) { for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) { for (int j = 1; j <= m; j++) {
if (s[i-1] == t[j-1]) if (s[i - 1] == t[j - 1])
cost = 0; cost = 0;
else else
cost = 1; cost = 1;
//M[i][j] := min(rep, ins, del) //M[i][j] := min(rep, ins, del)
int replace = Matrix[i-1][j-1] + cost; int replace = Matrix[i - 1][j - 1] + cost;
int insert = Matrix[i][j-1]+1; int insert = Matrix[i][j - 1] + 1;
int del = Matrix[i-1][j]+1; int del = Matrix[i - 1][j] + 1;
Matrix[i][j] = min(replace, insert,del); Matrix[i][j] = min(replace, insert, del);
//Matrix[i][j] = min((Matrix[i-1][j-1] + cost), (Matrix[i][j-1]+1),(Matrix[i-1][j]+1));
}
} }
}
//matrix entry at the bottom right which represents the levenshteinDistance
int LevenDist = Matrix[n][m]; int LevenDist = Matrix[n][m];
std::cout << "The Levenshtein distance between " << s << " and " << t << " is " << LevenDist << std::endl;
return 0; //return the distance
return LevenDist;
} }
...@@ -5,4 +5,4 @@ ...@@ -5,4 +5,4 @@
#include <string> #include <string>
int min(int x, int y, int z); int min(int x, int y, int z);
int levenshteinDistance(const std::string& s, const std::string& t); int levenD(const std::string& s, const std::string& t);
...@@ -7,11 +7,12 @@ ...@@ -7,11 +7,12 @@
#include <stdexcept> #include <stdexcept>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include "aufgabe1.h"
// Aufgabe 3 // Aufgabe 3
std::list<Cards> getListOfCards(const std::string& filename) { std::list<Cards> getListOfCards(const std::string &filename) {
std::ifstream file(filename); std::ifstream file(filename);
if(!file) { if (!file) {
throw std::runtime_error("Error: Unable to open file " + filename); throw std::runtime_error("Error: Unable to open file " + filename);
} }
//temp string to save line //temp string to save line
...@@ -23,7 +24,7 @@ std::list<Cards> getListOfCards(const std::string& filename) { ...@@ -23,7 +24,7 @@ std::list<Cards> getListOfCards(const std::string& filename) {
while (std::getline(file, temp)) { while (std::getline(file, temp)) {
std::stringstream streamData(temp); std::stringstream streamData(temp);
//"cut" the string at the vertical bars(|) //"cut" the string at the vertical bars(|)
char delim ='|'; char delim = '|';
//string vector to hold the cut parts //string vector to hold the cut parts
std::vector<std::string> stringVector; std::vector<std::string> stringVector;
//temp to store Card attributes //temp to store Card attributes
...@@ -56,12 +57,6 @@ std::list<Cards> getListOfCards(const std::string& filename) { ...@@ -56,12 +57,6 @@ std::list<Cards> getListOfCards(const std::string& filename) {
file.close(); 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 the cards as a list
return CardsList; return CardsList;
} }
...@@ -69,16 +64,16 @@ std::list<Cards> getListOfCards(const std::string& filename) { ...@@ -69,16 +64,16 @@ std::list<Cards> getListOfCards(const std::string& filename) {
// Aufgabe 4 // Aufgabe 4
void saveCardsToFile(const std::list<Cards>& cardsList) { void saveCardsToFile(const std::list<Cards> &cardsList, const std::string &filename) {
//Create new file //Create new file
std::ofstream file("scrambledNew.txt"); std::ofstream file(filename);
//Check if file can be opened //Check if file can be opened
if(!file) { if (!file) {
throw std::runtime_error("Error: Unable to open file."); throw std::runtime_error("Error: Unable to open file.");
} }
//Print every card object in CardsList into separate Line, add the vertical bar again //Print every card object in CardsList into separate Line, add the vertical bar again
for (const auto& card : cardsList) { for (const auto &card: cardsList) {
file << card.name << "|" << card.mana << "|" << card.cmc << "|" << card.type << "|" << card.count << std::endl; file << card.name << "|" << card.mana << "|" << card.cmc << "|" << card.type << "|" << card.count << std::endl;
} }
...@@ -87,7 +82,7 @@ void saveCardsToFile(const std::list<Cards>& cardsList) { ...@@ -87,7 +82,7 @@ void saveCardsToFile(const std::list<Cards>& cardsList) {
} }
//Aufgabe 5 //Aufgabe 5
std::list<std::string> referenceNames(const std::string& referenceFile) { std::list<std::string> referenceNames(const std::string &referenceFile) {
std::ifstream file(referenceFile); std::ifstream file(referenceFile);
//check if file can be opened //check if file can be opened
if (!file) { if (!file) {
...@@ -105,12 +100,37 @@ std::list<std::string> referenceNames(const std::string& referenceFile) { ...@@ -105,12 +100,37 @@ std::list<std::string> referenceNames(const std::string& referenceFile) {
//close file //close file
file.close(); file.close();
//for testing reasons //return list
std::cout <<namesList.front() << std::endl; return namesList;
std::cout <<namesList.back() << std::endl; }
return namesList; // Aufgabe 6
void levenshteinDistance(const std::list<std::string> &intactNames, std::list<Cards> &brokenNames) {
for (auto &card: brokenNames) {
//reference value
double referenceV = 0.2675 * card.name.length();
//bool for iterating
for (const auto &intactName: intactNames) {
//Following two lines are to remove the /r at the end of the string
std::string newIntact = intactName;
newIntact.erase(newIntact.size() - 1);
//calculate levenD of card for every intactName
int levenDist = levenD(card.name, newIntact);
//if distance smaller than 26.75% of the card name length...
if (levenDist < referenceV) {
//replace broken name with intact name
card.name = newIntact;
//break and go on to next card
break;
}
}
}
//Aufgabe 7
saveCardsToFile(brokenNames, "repaired.txt");
} }
...@@ -2,15 +2,20 @@ ...@@ -2,15 +2,20 @@
// Created by Eray düzenli on 13.04.23. // Created by Eray düzenli on 13.04.23.
// //
#pragma once #pragma once
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <list> #include <list>
#include "Cards.h" #include "Cards.h"
std::list<Cards> getListOfCards(const std::string& filename); std::list<Cards> getListOfCards(const std::string &filename);
void saveCardsToFile(const std::list<Cards>& CardsList);
std::list<std::string> referenceNames(const std::string& referenceFile); void saveCardsToFile(const std::list<Cards> &CardsList, const std::string &filename);
std::list<std::string> referenceNames(const std::string &referenceFile);
void levenshteinDistance(const std::list<std::string> &intactNames, std::list<Cards> &brokenNames);
......
...@@ -4,28 +4,21 @@ ...@@ -4,28 +4,21 @@
#include "functions.h" #include "functions.h"
int main() { int main() {
/*
std::cout << "Info 3 Projekt Nr.1" << std::endl; std::list<Cards> scrambledCards;
levenshteinDistance("kitten", "sitting"); std::list<std::string> referenceCardNames;
levenshteinDistance("industry", "interests");
levenshteinDistance("abcdef", "ghijkl");
levenshteinDistance("hello", "hallo");
levenshteinDistance("s", "");
levenshteinDistance("", "s");
Cards s;
s.cmc = 210;
s.count = "sf";
s.name = "peter";
s.type = "uwu";
std::cout << s.name << s.count << s.type << s.cmc << std::endl;
*/
std::list<Cards> test;
//TODO relative path doesnt work //TODO relative path doesnt work
test = getListOfCards("/Users/erayduzenli/CLionProjects/magicCards/scrambled.txt"); //Take scrambled cards and put them in a list
scrambledCards = getListOfCards("/Users/erayduzenli/CLionProjects/magicCards/scrambled.txt");
//TODO why does it save the file in the debug folder //TODO why does it save the file in the debug folder
saveCardsToFile(test); //take reference names and put them in a list
referenceNames("/Users/erayduzenli/CLionProjects/magicCards/reference.txt"); referenceCardNames = referenceNames("/Users/erayduzenli/CLionProjects/magicCards/reference.txt");
//calculate levenshteinDistance and create new file with intact names (if existing)
levenshteinDistance(referenceCardNames, scrambledCards);
return 0; return 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment