epr24pr1_ojanssen2/main.cpp

74 lines
2.2 KiB
C++
Raw Normal View History

2024-11-13 11:08:17 +00:00
#include "std_lib_inc.h"
2024-11-18 16:08:16 +00:00
const vector<string> first_digit_strings = {
"", "ein", "zwei", "drei", "vier", "fuenf", "sechs", "sieben", "acht", "neun"
};
const vector<string> middle_digit_strings = {
2024-11-20 15:51:07 +00:00
"", "zehn", "zwanzig", "dreissig", "vierzig", "fuenfzig", "sechzig", "siebzig", "achzig", "neunzig"
};
2024-11-18 16:08:16 +00:00
2024-11-13 11:08:17 +00:00
int main()
{
2024-11-18 16:08:16 +00:00
int input;
// ReSharper disable once CppDFAEndlessLoop
2024-11-20 15:37:14 +00:00
/*
* Use while (true) with break instead of while(cin) to be able to instantly stop the loop in case of an invalid input
*/
2024-11-18 16:08:16 +00:00
while (true)
{
cin >> input;
if (!cin)
2024-11-20 15:37:14 +00:00
break; // Stop running loop, if input is wrong
2024-11-18 16:08:16 +00:00
if (input < 1 || input > 999)
{
cout << "Zahl ausserhalb des gueltigen Bereichs.\n";
continue;
}
2024-11-20 15:37:14 +00:00
// TODO: remove static_cast
2024-11-18 16:08:16 +00:00
int input_length = static_cast<int>(std::to_string(input).length());
int first_digit = input % 10;
2024-11-20 15:37:14 +00:00
int last_two_digits = input % 100;
2024-11-20 15:42:41 +00:00
int middle_digit = ((last_two_digits) - first_digit) / 10;
2024-11-18 16:27:55 +00:00
vector<string> result;
string suffix = "";
2024-11-20 15:37:14 +00:00
if (last_two_digits < 11 || last_two_digits > 12)
{
// Handle everything else
if (first_digit == 1)
suffix = "s";
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 && (last_two_digits < 11 || last_two_digits > 12))
2024-11-18 16:27:55 +00:00
result.push_back(middle_digit_strings[middle_digit]);
else
result.push_back("");
2024-11-18 16:08:16 +00:00
if (input_length == 3)
{
int last_digit = ((input % 1000) - middle_digit) / 100;
2024-11-18 16:27:55 +00:00
string suffix = "";
result.push_back(first_digit_strings[last_digit] + "hundert" + suffix);
2024-11-18 16:08:16 +00:00
}
2024-11-18 16:27:55 +00:00
else
result.push_back("");
2024-11-18 16:08:16 +00:00
2024-11-18 16:27:55 +00:00
cout << result[2] << result[0] << result[1] << "\n";
2024-11-18 16:08:16 +00:00
}
cout << "Eingabe beendet.\n";
2024-11-13 11:08:17 +00:00
return 0;
2024-11-18 16:08:16 +00:00
}