-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgennum.go
347 lines (316 loc) · 7.21 KB
/
gennum.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"flag"
"fmt"
"os"
"strconv"
)
/* CONFIG */
var verboseEin = false
var verboseUnd = false
var verboseUseParentheses = false
/* CARDINAL NUMBERS */
func cardinalNumber0to19(i int) string {
numbers := []string{"null", "eins", "zwei", "drei", "vier", "fünf", "sechs",
"sieben", "acht", "neun", "zehn", "elf", "zwölf", "dreizehn", "vierzehn",
"fünfzehn", "sechzehn", "siebzehn", "achtzehn", "neunzehn"}
return numbers[i]
}
func cardinalNumberDecade(i int) string {
numbers := []string{"zwanzig", "dreißig", "vierzig", "fünfzig", "sechzig",
"siebzig", "achtzig", "neunzig"}
return numbers[i/10-2]
}
func cardinalNumber20to99(i int) string {
decade := i / 10 * 10
ones_digit := i % 10
result := ""
if ones_digit == 1 {
result += "ein"
} else if ones_digit > 0 {
result += cardinalNumber0to19(ones_digit)
}
if i-decade > 0 {
result += "und"
}
result += cardinalNumberDecade(decade)
return result
}
func cardinalNumber0to99(i int) string {
last2 := i % 100
if last2 < 20 {
return cardinalNumber0to19(last2)
} else if last2 >= 20 {
return cardinalNumber20to99(last2)
} else {
return ""
}
}
func cardinalNumber100to999(i int) string {
hundred_digit := i / 100
last2 := i % 100
result := ""
if hundred_digit == 1 {
if verboseEin {
if verboseUseParentheses {
result += "(ein)"
} else {
result += "ein"
}
}
} else {
result += cardinalNumber0to19(hundred_digit)
}
if hundred_digit > 0 {
result += "hundert"
}
if last2 > 0 {
if verboseUnd {
if verboseUseParentheses {
result += "(und)"
} else {
result += "und"
}
}
result += cardinalNumber0to99(last2)
}
return result
}
func cardinalNumber0to999(i int) string {
if i < 100 {
return cardinalNumber0to99(i)
} else if i < 1000 {
return cardinalNumber100to999(i)
} else {
return ""
}
}
func cardinalNumber1000to999999(i int) string {
tausends := i / 1000
last3 := i % 1000
result := ""
if tausends == 1 {
if verboseEin {
if verboseUseParentheses {
result += "(ein)"
} else {
result += "ein"
}
}
} else {
result += cardinalNumber0to999(tausends)
}
if tausends > 0 {
result += "tausend"
}
if last3 > 0 {
if verboseUnd {
if verboseUseParentheses {
result += "(und)"
} else {
result += "und"
}
}
result += cardinalNumber0to999(last3)
}
return result
}
func cardinalNumber0to999999(i int) string {
if i < 100 {
return cardinalNumber0to99(i)
} else if i < 1000 {
return cardinalNumber100to999(i)
} else if i < 1000000 {
return cardinalNumber1000to999999(i)
} else {
return ""
}
}
/* ORDINAL NUMBERS */
func ordinalNumber0to19(i int) string {
numbers := []string{"nullte", "erste", "zweite", "dritte", "vierte", "fünfte", "sechste", "siebte",
"achte", "neunte", "zehnte", "elfte", "zwölfte", "dreizehnte", "vierzehnte", "fünfzehnte",
"sechzehnte", "siebzehnte", "achtzehnte", "neunzehnte"}
return numbers[i]
}
func ordinalNumberDecade(i int) string {
numbers := []string{"zwanzigste", "dreißigste", "vierzigste", "fünfzigste", "sechzigste",
"siebzigste", "achtzigste", "neunzigste"}
return numbers[i/10-2]
}
func ordinalNumber20to99(i int) string {
decade := i / 10 * 10
ones_digit := i % 10
result := ""
if ones_digit == 1 {
result += "ein"
} else if ones_digit > 0 {
result += cardinalNumber0to19(ones_digit)
}
if i-decade > 0 {
result += "und"
}
result += ordinalNumberDecade(decade)
return result
}
func ordinalNumber0to99(i int) string {
last2 := i % 100
if last2 < 20 {
return ordinalNumber0to19(last2)
} else if last2 < 100 {
return ordinalNumber20to99(last2)
} else {
return ""
}
}
func ordinalNumber100to999(i int) string {
hundred_digit := i / 100
last2 := i % 100
result := ""
if hundred_digit == 1 {
if verboseEin {
if verboseUseParentheses {
result += "(ein)"
} else {
result += "ein"
}
}
} else {
result += cardinalNumber0to19(hundred_digit)
}
if hundred_digit > 0 {
result += "hundert"
}
if last2 > 0 {
if verboseUnd {
if verboseUseParentheses {
result += "(und)"
} else {
result += "und"
}
}
result += ordinalNumber0to99(last2)
} else {
result += "ste"
}
return result
}
func ordinalNumber0to999(i int) string {
if i < 100 {
return ordinalNumber0to99(i)
} else if i < 1000 {
return ordinalNumber100to999(i)
} else {
return ""
}
}
func ordinalNumber1000to999999(i int) string {
tausends := i / 1000
last3 := i % 1000
result := ""
if tausends == 1 {
if verboseEin {
if verboseUseParentheses {
result += "(ein)"
} else {
result += "ein"
}
}
} else {
result += cardinalNumber0to999(tausends)
}
if tausends > 0 {
result += "tausend"
}
if last3 > 0 {
if verboseUnd {
if verboseUseParentheses {
result += "(und)"
} else {
result += "und"
}
}
result += ordinalNumber0to999(last3)
} else {
result += "ste"
}
return result
}
func ordinalNumber0to999999(i int) string {
if i < 100 {
return ordinalNumber0to99(i)
} else if i < 1000 {
return ordinalNumber100to999(i)
} else if i < 1000000 {
return ordinalNumber1000to999999(i)
} else {
return ""
}
}
func main() {
startPtr := flag.Int("s", 0, "start number from")
endPtr := flag.Int("e", 0, "count until value")
isOrdinalPtr := flag.Bool("o", false, "print ordinal numbers instad of cardinals")
showUsagePtr := flag.Bool("h", false, "show usage help")
verboseEinPtr := flag.Bool("ein", false, "verbose ein in einhundert, eintausend etc.")
verboseUndPtr := flag.Bool("und", false, "verbose und in hundertundeins, tausendundzwei etc.")
verboseUseParenthesesPtr := flag.Bool("p", false, "use parentheses in (ein)hundert, tausend(und)zwei etc.")
verboseDigitsPtr := flag.Bool("d", false, "print number also in digits at the beginning of the line")
flag.Parse()
if *showUsagePtr {
os.Stderr.WriteString("Usage:\n")
flag.PrintDefaults()
os.Exit(0)
}
start := *startPtr
end := *endPtr
isOrdinal := *isOrdinalPtr
verboseEin = *verboseEinPtr
verboseUnd = *verboseUndPtr
verboseUseParentheses = *verboseUseParenthesesPtr
verboseDigits := *verboseDigitsPtr
if end < start {
os.Stderr.WriteString("Warning: Start value must be lower than end!\n")
os.Stderr.WriteString("Setting end value to start value = " + strconv.Itoa(start) + "\n")
end = start
}
if verboseEin || verboseUnd {
os.Stderr.WriteString("Verbose on: ")
if verboseEin {
if verboseUseParentheses {
os.Stderr.WriteString("(ein)")
} else {
os.Stderr.WriteString("ein")
}
}
if verboseUnd {
if verboseUseParentheses {
os.Stderr.WriteString(", (und)")
} else {
os.Stderr.WriteString(", und")
}
}
os.Stderr.WriteString("\n")
}
if !isOrdinal {
os.Stderr.WriteString("Generating 'cardinal' numbers from " +
strconv.Itoa(start) + " to " + strconv.Itoa(end) + "\n")
for i := start; i <= end; i++ {
if verboseDigits {
fmt.Println(i, "=", cardinalNumber0to999999(i))
} else {
fmt.Println(cardinalNumber0to999999(i))
}
}
} else {
os.Stderr.WriteString("Generating 'ordinal' numbers from " +
strconv.Itoa(start) + " to " + strconv.Itoa(end) + "\n")
for i := start; i <= end; i++ {
if verboseDigits {
fmt.Println(i, "=", ordinalNumber0to999999(i))
} else {
fmt.Println(ordinalNumber0to999999(i))
}
}
}
}