C_CPP/07_STD/MP/grundgeruest.cpp

105 lines
4.4 KiB
C++
Raw Normal View History

2018-03-09 09:47:53 +01:00
// based on example from http://en.cppreference.com/w/cpp/numeric/random/rand
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <random>
#include <list>
#include <algorithm>
#include <unordered_map>
#include "grundgeruest.hpp"
2018-03-09 09:47:53 +01:00
struct BinaryOctet {
bool evenParity;
char bitsAsDigits[8];
BinaryOctet(int i) {
}
2018-03-09 09:47:53 +01:00
};
int main() {
int array[41];
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(0, 23);
for(int i = 0; i <= 41; i++) {
int rnd = distribution(generator);
array[i] = rnd;
}
unittest_stringSimilarity();
}
int stringSimilarity(std::string string1, std::string string2) {
//wenn gleicher String, gleich 100 Punkte geben, spart den Rest
if(string1 == string2) return 100;
int points = 0;
//gleiche Länge +10 Punkte
if(string1.length() == string2.length()) points += 10;
//Anzahl der verschiedenen Zeichen zählen
std::unordered_map<char, int> string1map;
std::unordered_map<char, int> string2map;
std::unordered_map<char, int> characterCount;
for(char c : string1) {
string1map[c]++;
characterCount[c]++;
}
for(char c : string2) {
string2map[c]++;
characterCount[c]++;
}
//gleiche Anzahl an verschiedenen Zeichen +10 Punkte
if(string1map.size() == string2map.size()) points += 10;
//auf gleiche Zeichenanzahl testen
//gleiche Anzahl an gleichen Zeichen
int equalCharCount = 0;
int equalChars = 0;
for(std::pair<char, int> count : characterCount) {
if(string1map[count.first] == string2map[count.second]) equalCharCount++;
if(string1map[count.first] != 0 && string2map[count.first] != 0) equalChars++;
}
if(equalChars > 0) {
points += characterCount.size() / equalChars * 10;
}
if(equalCharCount > 0) {
points += characterCount.size() / equalCharCount;
}
//TODO: Punkte für gleiche Buchstaben und gleiche Buchstaben Anzahl vergeben
return points;
2018-03-09 09:47:53 +01:00
}
void unittest_stringSimilarity() {
std::cout << "Hallo Welt | Hallo Welt ---> " << stringSimilarity("Hallo Welt", "Hallo Welt") << std::endl;
std::cout << "Hallo Welt | Hallo Baum --->" << stringSimilarity("Halllo Welt", "Hallo Baum") << std::endl;
std::cout << "Hello World | Hallo Welt ---> " << stringSimilarity("Hello World", "Hallo Welt") << std::endl;
std::cout << "bla | ping ---> " << stringSimilarity("bla", "ping") << std::endl;
std::cout << "Lorem ipsum..... | Lorem ipsum ---> " << stringSimilarity("Lorem ipsum dolor sit amet, "
"consetetur sadipscing elitr, "
"sed diam nonumy eirmod tempor"
"invidunt ut labore et dolore "
"magna aliquyam erat, sed diam voluptua. "
"At vero eos et accusam et justo duo dolores "
"et ea rebum. Stet clita kasd gubergren, "
"no sea takimata sanctus est Lorem "
"ipsum dolor sit amet. "
"Lorem ipsum dolor sit amet, "
"consetetur sadipscing elitr, "
"sed diam nonumy eirmod tempor "
"invidunt ut labore et dolore magna "
"aliquyam erat, sed diam voluptua. "
"At vero eos et accusam et justo duo dolores "
"et ea rebum. Stet clita kasd gubergren, "
"no sea takimata sanctus est Lorem ipsum "
"dolor sit amet.",
"Lorem ipsum") << std::endl;
}