diff --git a/.idea/magicCards.iml b/.idea/magicCards.iml index bc2cd87409057301f546d83bd548111b9a241cb1..f08604bb65b25149b195f9e9f282f9683a428592 100644 --- a/.idea/magicCards.iml +++ b/.idea/magicCards.iml @@ -1,8 +1,2 @@ <?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 diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 68991b23fc738212dee7640b824eb0d9c6b4caba..91af74722b4b2829a18f0a91562e459d4764b358 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ project(magicCards) set(CMAKE_CXX_STANDARD 20) -add_executable(magicCards main.cpp) +add_executable(magicCards main.cpp aufgabe1.h aufgabe1.cpp) diff --git a/aufgabe1.cpp b/aufgabe1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49c3fa8f711574368dc137635eb2fe3c9f9de4c1 --- /dev/null +++ b/aufgabe1.cpp @@ -0,0 +1,57 @@ +#include <iostream> +#include "aufgabe1.h" + +//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; + } +} + + +int levenshteinDistance(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 + int Matrix[n + 1][m + 1]; + + // 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[i][j] = min((Matrix[i-1][j-1] + cost), (Matrix[i][j-1]+1),(Matrix[i-1][j]+1)); + + } + } + int LevenDist = Matrix[n][m]; + std::cout << "The Levenshtein distance between " << s << " and " << t << " is " << LevenDist << std::endl; + return 0; +} + diff --git a/aufgabe1.h b/aufgabe1.h new file mode 100644 index 0000000000000000000000000000000000000000..1b1e5b1b74a3adf51a18988ea8a724f0b19ed50a --- /dev/null +++ b/aufgabe1.h @@ -0,0 +1,8 @@ +// +// Created by erayd on 11.04.2023. +// +#pragma once +#include <string> + +int min(int x, int y, int z); +int levenshteinDistance(const std::string& s, const std::string& t); diff --git a/main.cpp b/main.cpp index 48139b9a19f461276e7b5e5baf5dd6f89802c91b..6639000e4bcdcf637c19660343f89eb7fe9c4553 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,13 @@ #include <iostream> +#include "aufgabe1.h" int main() { std::cout << "Info 3 Projekt Nr.1" << std::endl; + levenshteinDistance("kitten", "sitting"); + levenshteinDistance("industry", "interests"); + levenshteinDistance("abcdef", "ghijkl"); + levenshteinDistance("hello", "hallo"); + levenshteinDistance("s", ""); + levenshteinDistance("", "s"); return 0; }