-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNum2Text.cpp
77 lines (66 loc) · 1.15 KB
/
Num2Text.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Give a number, convert that to text. 123 as One hundred and twenty three.
// 1. std solution would be
// std::string s = std::to_string(123);
// 2. Non-std solution:
#include <string>
std::string GetText(int n, int dec) {
std::string str;
if (dec == 0 || dec == 2) {
switch (n) {
case 1: str = "one";
break;
case 2: str = "two";
break;
case 3: str = "three";
break;
case 4: str = "four";
break;
}
}
else if (dec == 1) {
switch (n) {
case 2: str = "twenty";
break;
case 3: str = "thirty";
break;
case 4: str = "fourty";
break;
}
}
else if (dec == -1) {
switch (n) {
case 2: str = "twelve";
break;
case 3: str = "thirteen";
break;
case 4: str = "fourteen";
break;
}
}
return str;
}
std::string Num2Text(int num){
std::string str;
int n = num;
n= num/100;
n %= 10;
str = GetText(n,2);
str += " hundred and ";
n = num / 10;
n %= 10;
if (n != 1)
{
str += GetText(n, 1);
str += " ";
n = num;
n %= 10;
str += GetText(n, 0);
}
else
{
n = num;
n %= 10;
str += GetText(n, -1);
}
return str;
}