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
|
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
const string One[10] = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine" };
const string Teen[10] = {
"", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
const string Ten[10] = {
"", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety" };
const string Hund[10] = {
"", "one_hundred", "two_hundred", "three_hundred", "four_hundred",
"five_hundred", "six_hundred", "seven_hundred", "eight_hundred", "nine_hundred" };
int result = 0;
string word, line;
cout << "Please enter a number: ";
getline(cin, line);
istringstream is(line);
while (is >> word) {
const string* iter;
iter = find(One, One + 10, word);
if (iter != (One + 10)) {
result += (iter - One);
}
iter = find(Teen, Teen + 10, word);
if (iter != (Teen + 10)) {
result += 10 + (iter - Teen);
}
iter = find(Ten, Ten + 10, word);
if (iter != (Ten + 10)) {
result += 10 * (iter - Ten);
}
iter = find(Hund, Hund + 10, word);
if (iter != (Hund + 10)) {
result += 100 * (iter - Hund);
}
}
cout << result << endl;
return 0;
}
|