epr24pr1_ojanssen2/main.cpp

74 lines
2.3 KiB
C++

#include "std_lib_inc.h"
constexpr vector<string> first_digit_strings = {
"", "ein", "zwei", "drei", "vier", "fuenf", "sechs", "sieben", "acht", "neun"
};
constexpr vector<string> middle_digit_strings = {
"", "zehn", "zwanzig", "dreissig", "vierzig", "fuenfzig", "sechzig", "siebzig", "achzig", "neunzig"
};
int main()
{
int input;
/*
* 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)
{
cin >> input;
if (!cin)
break; // Stop running loop, if input is wrong
if (input < 1 || input > 999)
{
cout << "Zahl ausserhalb des gueltigen Bereichs.\n";
continue;
}
const int right_digit = input % 10;
const int middle_and_right_digit = input % 100;
const int middle_digit = ((middle_and_right_digit) - right_digit) / 10;
vector<string> result;
string number_constructor = "";
if (middle_and_right_digit < 11 || middle_and_right_digit > 12)
{
// Handle everything else
if (right_digit == 1)
number_constructor = "s";
if (middle_digit > 1 && right_digit != 0)
number_constructor = "und";
number_constructor = first_digit_strings[right_digit] + number_constructor;
}
else
{
// Handle 11 and 12
if (middle_and_right_digit == 11)
number_constructor = "elf";
else
number_constructor = "zwoelf";
}
result.push_back(number_constructor);
if ((input > 9 /*input_length > 1*/) && (middle_and_right_digit < 11 || middle_and_right_digit > 12))
result.push_back(middle_digit_strings[middle_digit]);
else
result.push_back("");
if (input > 99 /*input_length >= 3*/)
{
int left_digit = ((input % 1000) - middle_digit) / 100;
if (left_digit != 1)
number_constructor = first_digit_strings[left_digit];
else
number_constructor = "";
result.push_back(number_constructor + "hundert");
}
else
result.push_back("");
cout << result[2] << result[0] << result[1] << "\n";
}
cout << "Eingabe beendet.\n";
return 0;
}