Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • duezenli/magicCards
  • letzgus/magicCards
2 results
Show changes
Commits on Source (14)
<?xml version="1.0" encoding="UTF-8"?>
<module type="CPP_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<module classpath="CMake" type="CPP_MODULE" version="4" />
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -3,4 +3,4 @@ project(magicCards)
set(CMAKE_CXX_STANDARD 20)
add_executable(magicCards main.cpp)
add_executable(magicCards main.cpp levenD.h levenD.cpp Cards.h functions.h functions.cpp)
#pragma once
#include <string>
//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;
std::string cmc;
std::string type;
std::string count;
};
\ No newline at end of file
# magicCards
Programm to repair broken cards given the intact name of the cards.
CLion was used to implement the programm.
Build on Windows.
#include <iostream>
#include "functions.h"
#include <fstream>
#include <stdexcept>
#include <sstream>
#include <vector>
#include "levenD.h"
// Aufgabe 3
//Given a file of cards, return a list consisting of card objects
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();
//return the cards as a list
return CardsList;
}
// Aufgabe 4
//Given a list of cards, save the card objects back into a file
void saveCardsToFile(const std::list<Cards> &cardsList, const std::string &filename) {
//Create new file
std::ofstream file(filename);
//Check if file can be opened
if (!file) {
throw std::runtime_error("Error: Unable to open file.");
}
//Print every card object in CardsList into separate Line, add the vertical bar again
for (const auto &card: cardsList) {
file << card.name << "|" << card.mana << "|" << card.cmc << "|" << card.type << "|" << card.count << std::endl;
}
//Close file
file.close();
}
//Aufgabe 5
std::list<std::string> referenceNames(const std::string &referenceFile) {
std::ifstream file(referenceFile);
//check if file can be opened
if (!file) {
throw std::runtime_error("Error: Unable to open file " + referenceFile);
}
//output
std::list<std::string> namesList;
//temp for names
std::string name;
//read every line
while (std::getline(file, name)) {
namesList.push_back(name);
}
//close file
file.close();
//return list
return namesList;
}
// Aufgabe 6
//Calculate the levenshtein distance for two lists, replace the broken names with the intact names if possible and save
// in a new .txt file
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();
/* On macOS use this loop instead
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;
}
} */
for (const auto &intactName: intactNames) {
//calculate levenD of card for every intactName
int levenDist = levenD(card.name, intactName);
//if distance smaller than 26.75% of the card name length...
if (levenDist < referenceV) {
//replace broken name with intact name
card.name = intactName;
//break and go on to next card
break;
}
}
}
//Aufgabe 7
saveCardsToFile(brokenNames, "repaired.txt");
}
#pragma once
#include <string>
#include <iostream>
#include <list>
#include "Cards.h"
std::list<Cards> getListOfCards(const std::string &filename);
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);
#include "levenD.h"
//Helper function, calculate minimum of three values
int min(int x, int y, int z) {
if (x < y && x < z) {
return x;
} else if (y < z) {
return y;
} else {
return z;
}
}
//Given two strings,calculate the levenshtein distance between them and return it as an integer
int levenD(const std::string &s, const std::string &t) {
//Length of first string
int n = s.length();
//Length of second string
int m = t.length();
int cost = 0;
//Matrix to hold the levenshtein distance (longest string is 174)
int Matrix[175][175];
// Initialize the first row and column of the matrix,[0][0] is empty
for (int i = 0; i <= n; i++) {
Matrix[i][0] = i;
}
for (int j = 0; j <= m; j++) {
Matrix[0][j] = j;
}
// Calculate the Levenshtein distances for all pairs of characters
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i - 1] == t[j - 1])
cost = 0;
else
cost = 1;
//M[i][j] := min(rep, ins, del)
int replace = Matrix[i - 1][j - 1] + cost;
int insert = Matrix[i][j - 1] + 1;
int del = Matrix[i - 1][j] + 1;
Matrix[i][j] = min(replace, insert, del);
}
}
//matrix entry at the bottom right which represents the levenshteinDistance
int LevenDist = Matrix[n][m];
//return the distance
return LevenDist;
}
#pragma once
#include <string>
int min(int x, int y, int z);
int levenD(const std::string& s, const std::string& t);
#include <iostream>
#include "levenD.h"
#include "Cards.h"
#include "functions.h"
int main() {
std::cout << "Hello, World!" << std::endl;
std::list<Cards> scrambledCards;
std::list<std::string> referenceCardNames;
/* on VsCode use this instead
//Take scrambled cards and put them in a list
scrambledCards = getListOfCards("../../scrambled.txt");
//take reference names and put them in a list
referenceCardNames = referenceNames("../../reference.txt");
*/
//Take scrambled cards and put them in a list
scrambledCards = getListOfCards("../scrambled.txt");
//take reference names and put them in a list
referenceCardNames = referenceNames("../reference.txt");
//calculate levenshteinDistance and create new file with intact names (if existing)
levenshteinDistance(referenceCardNames, scrambledCards);
return 0;
}
This diff is collapsed.
This diff is collapsed.