我正在制作一个程序,用文本读取数字方程式的用户输入,例如:(thirty_three + forty_two)。 该程序需要能够读取高达999的输入并输出高达999。 我的输出数字最高为999的代码可以正常工作
string* write_value(int n, string* first) {
int th = (n % 10000) / 1000;
int h = (n % 1000) / 100;
int t = (n % 100) / 10;
int u = (n % 10);
if (th > 0) {
*first++ = units[h] + "_thousand";
}
if (h > 0) {
*first++ = units[h] + "_hundred";
}
if (t == 1) { // deal with the pesky teens
t -= 1;
u += 10;
}
if (t > 0) {
*first++ = tens[t];
}
if (u > 0) {
*first++ = units[u];
}
return first;
}
但我的read_value函数不接受“数百”文本作为输入并跳过它。我将如何将其实现到我的read_value函数中?
string* read_digit(int& value, string* first, string* last, string* digits, int count) {
if (first < last) {
for (int i = 1; i < count; ++i) {
if (*first == digits[i]) {
value = i;
return first + 1;
}
}
}
return first;
}
int read_value(string* first, string* last) {
int th = 0;
int h = 0;
int t = 0;
int u = 0;
first = read_digit(t, first, last, tens, 10);
first = read_digit(u, first, last, units, 20);
first = read_digit(h, first, last, units, 10);
first = read_digit(th, first, last, units, 10);
return (th * 1000) + (h * 100) + (t * 10) + u;
}
int Wordnum::read_number(string n) {
string words[20]; // enough for up to 999,999,999
string* last = split(n, words, '_');
if (last == words || words[0] == "zero") {
return 0;
}
else if (words[0] == "negative") {
return -read_value(words + 1, last);
}
else {
return read_value(words, last);
}
}