-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoedelGenerator.cpp
123 lines (97 loc) · 4.24 KB
/
GoedelGenerator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <regex>
#include "GoedelGenerator.h"
GoedelGenerator::GoedelGenerator(bool newprimes) {
formula = "";
goedelnumber = BigUnsignInt();
primes = Primes(newprimes);
}
std::string GoedelGenerator::getFormulaFromUser(bool &debugflag) {
std::string formulaToCheck;
do {
std::cout << "Geben Sie die Formel ein deren Goedelnummer berechnet werden soll:\n"
"(Hinweise zum Format auf der Hilfeseite. Abbrechen mit Enter ohne Eingabe)" << std::endl;
std::getline(std::cin, formulaToCheck);
if (formulaToCheck.empty()) { break; }
} while (!isCorrectFormula(formulaToCheck));
formula = formulaToCheck;
return formula;
}
BigUnsignInt GoedelGenerator::calculateGoedelNumber(bool &debugflag) {
std::vector<unsigned short int> encodedFormula = encodeFormula(formula, debugflag);
if (encodedFormula.empty()) {
if (debugflag) {
std::cout << "ERROR: Couldn't calculate a Goedelnumber because encodedFormula was empty" << std::endl;
}
return goedelnumber;
} else {
goedelnumber = BigUnsignInt(1);
for (int i = 0; i < encodedFormula.size(); i++) {
goedelnumber *= (intPow(primes.getPrime(i, debugflag), encodedFormula[i]));
}
return goedelnumber;
}
}
bool GoedelGenerator::printCalculatedNumberToScreen(bool &debugflag) {
if (goedelnumber.isEmpty()) {
if (debugflag) {
std::cout << "ERROR: Couldn't print Goedelnumber because it was empty" << std::endl;
}
return false;
} else {
std::cout << "Die Goedelnummer der Formel \"" << formula << "\" ist: " << goedelnumber << std::endl;
}
return true;
}
bool GoedelGenerator::isCorrectFormula(const std::string &formulaToCheck) {
for (const char i: formulaToCheck) {
if (i == '+' || i == '-' || i == '*' || i == '/' || i == '=' || i == 's' || i == '0' || i == 'a' || i == 'b' ||
i == 'c' || i == 'd' || i == 'e' || i == 'f') {
continue;
} else {
std::cout << "Bei der Eingabe wurden nicht zugelassene Zeichen verwendet.\n" << std::endl;
return false;
}
}
std::regex meaninglessFormula(R"(0[abcdef]|[abcdef]{2}|[\+\-\*\/]{2}|00|s[\+\-\*\/\=abcdef]|^[\+\-\*\/\=]|[\+\-\*\/\=s]$)");
if (std::regex_search(formulaToCheck, meaninglessFormula)) {
std::cout << "Die Formel ergibt semantisch keinen Sinn, enthaelt zB aufeinanderfolgende Nullen.\n" << std::endl;
return false;
}
return true;
}
std::vector<unsigned short int> GoedelGenerator::encodeFormula(std::string &formulaToEncode, bool &debugflag) {
std::vector<unsigned short int> encodedFormula(formulaToEncode.size());
if (formulaToEncode.empty()) {
if (debugflag) {
std::cout << "ERROR: Unable to encode, formula was empty." << std::endl;
}
return encodedFormula;
} else {
for (int i = 0; i < formulaToEncode.size(); i++) {
if (formulaToEncode[i] == '+') { encodedFormula[i] = 1; }
else if (formulaToEncode[i] == '-') { encodedFormula[i] = 2; }
else if (formulaToEncode[i] == '*') { encodedFormula[i] = 3; }
else if (formulaToEncode[i] == '/') { encodedFormula[i] = 4; }
else if (formulaToEncode[i] == '=') { encodedFormula[i] = 5; }
else if (formulaToEncode[i] == '0') { encodedFormula[i] = 6; }
else if (formulaToEncode[i] == 's') { encodedFormula[i] = 7; }
else if (formulaToEncode[i] == 'a') { encodedFormula[i] = 8; }
else if (formulaToEncode[i] == 'b') { encodedFormula[i] = 9; }
else if (formulaToEncode[i] == 'c') { encodedFormula[i] = 10; }
else if (formulaToEncode[i] == 'd') { encodedFormula[i] = 11; }
else if (formulaToEncode[i] == 'e') { encodedFormula[i] = 12; }
else if (formulaToEncode[i] == 'f') { encodedFormula[i] = 13; }
}
}
return encodedFormula;
}
unsigned long long int GoedelGenerator::intPow(unsigned long long int base, unsigned short int exp) {
unsigned long long int result = base;
if (exp == 1) {
return result;
} else {
result *= intPow(base, exp - 1);
}
return result;
}