feat: started handling special cases

This commit is contained in:
moonleay 2024-11-20 16:37:14 +01:00
parent 5c2ea4c07b
commit ae66fc5a37
Signed by: moonleay
GPG key ID: 82667543CCD715FB

View file

@ -6,34 +6,53 @@ const vector<string> first_digit_strings = {
const vector<string> middle_digit_strings = { const vector<string> middle_digit_strings = {
"", "zehn", "zwanzig", "deissig", "vierzig", "fuenfzig", "sechzig", "siebzig", "achzig", "neunzig" "", "zehn", "zwanzig", "deissig", "vierzig", "fuenfzig", "sechzig", "siebzig", "achzig", "neunzig"
}; }; // TODO: fix typo
int main() int main()
{ {
int input; int input;
// ReSharper disable once CppDFAEndlessLoop // ReSharper disable once CppDFAEndlessLoop
/*
* Use while (true) with break instead of while(cin) to be able to instantly stop the loop in case of an invalid input
*/
while (true) while (true)
{ {
cin >> input; cin >> input;
if (!cin) if (!cin)
break; break; // Stop running loop, if input is wrong
if (input < 1 || input > 999) if (input < 1 || input > 999)
{ {
cout << "Zahl ausserhalb des gueltigen Bereichs.\n"; cout << "Zahl ausserhalb des gueltigen Bereichs.\n";
continue; continue;
} }
// TODO: remove static_cast
int input_length = static_cast<int>(std::to_string(input).length()); int input_length = static_cast<int>(std::to_string(input).length());
int first_digit = input % 10; int first_digit = input % 10;
int middle_digit = ((input % 100) - first_digit) / 10; int middle_digit = ((input % 100) - first_digit) / 10;
int last_two_digits = input % 100;
vector<string> result; vector<string> result;
string suffix = ""; string suffix = "";
if (first_digit == 1) if (last_two_digits < 11 || last_two_digits > 12)
suffix = "s"; {
if (middle_digit != 0 && first_digit != 0) // Handle everything else
suffix = "und"; if (first_digit == 1)
result.push_back(first_digit_strings[first_digit] + suffix); suffix = "s";
if (input_length > 1) if (middle_digit > 1 && first_digit != 0)
suffix = "und";
suffix = first_digit_strings[first_digit] + suffix;
}
else
{
// Handle 11 and 12
if (last_two_digits == 11)
suffix = "elf";
else
suffix = "zwoelf";
}
result.push_back(suffix);
if (input_length > 1 && (middle_digit < 11 || middle_digit > 12))
result.push_back(middle_digit_strings[middle_digit]); result.push_back(middle_digit_strings[middle_digit]);
else else
result.push_back(""); result.push_back("");