epr24pr1_ojanssen2/main.cpp

74 lines
2.1 KiB
C++

#include "std_lib_inc.h"
const vector<string> first_digit_strings = {
"", "ein", "zwei", "drei", "vier", "fuenf", "sechs", "sieben", "acht", "neun"
};
const 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;
}
int first_digit = input % 10;
int last_two_digits = input % 100;
int middle_digit = ((last_two_digits) - first_digit) / 10;
vector<string> result;
string suffix = "";
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 > 9 /*input_length > 1*/) && (last_two_digits < 11 || last_two_digits > 12))
result.push_back(middle_digit_strings[middle_digit]);
else
result.push_back("");
if (input > 99 /*input_length >= 3*/)
{
int last_digit = ((input % 1000) - middle_digit) / 100;
if (last_digit != 1)
suffix = first_digit_strings[last_digit];
else
suffix = "";
result.push_back(suffix + "hundert");
}
else
result.push_back("");
cout << result[2] << result[0] << result[1] << "\n";
}
cout << "Eingabe beendet.\n";
return 0;
}