-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrojUTekst.cs
158 lines (140 loc) · 4.49 KB
/
BrojUTekst.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BrojUTekst
{
class Program
{
enum Pol { m, ž, s };
static string[] b1 = {"nula", "jedan[jedna]jedno", "dva[dve]dva","tri","četiri","pet","šest","sedam","osam","devet","deset",
"jedanaest","dvanaest","trinaest","četrnaest","petnaest","šesnaest","sedamnaest","osamnaest","devetnaest"}; // moguće m(..), ž(..) i s(..)
static string[] b10 = { "dva", "tri", "četr", "pe", "šez", "sedam", "osam", "deve" }; // + deset
static string[] b100 = { "", "dve", "tri", "četiri", "pet", "šest", "sedam", "osam", "devet" }; // + sto
static string[] b1000 = { "hiljada", "hiljade" };
static string[] b1000000 = { "milion", "miliona" };
static void Main(string[] args)
{
string unos;
long broj;
while (true)
{
unos = Console.ReadLine();
if (long.TryParse(unos, out broj))
{
Console.WriteLine(BrojUString(broj));
}
}
}
static string mžs(string broj, Pol p)
{
switch (p)
{
case Pol.m:
return m(broj);
case Pol.ž:
return ž(broj);
case Pol.s:
return s(broj);
}
throw new ArgumentException("p");
}
static string m(string broj)
{
int p = broj.IndexOf('[');
if (p > 0)
return broj.Substring(0, p);
else
return broj;
}
static string ž(string broj)
{
int p = broj.IndexOf('[');
int q = broj.IndexOf(']');
if (p > 0)
return broj.Substring(p + 1, q - p - 1);
else
return broj;
}
static string s(string broj)
{
int p = broj.IndexOf(']');
if (p > 0)
return broj.Substring(p + 1, broj.Length - p - 1);
else
return broj;
}
static string BrojUString(long broj, Pol p = Pol.m)
{
string prefiks = (broj < 0) ? "minus " : string.Empty;
if (broj == 0)
return b1[0];
else
return prefiks + Br1000000(Math.Abs( broj), p);
}
static string Br1(long broj, Pol p = Pol.m)
{
if (broj != 0)
return mžs(b1[(broj % 20)], p);
else
return string.Empty;
}
static string Br20(long broj, Pol p = Pol.m)
{
if (broj >= 20)
return b10[(broj % 100) / 10 - 2] + "deset " + Br1(broj % 10, p);
else
return Br1(broj, p);
}
static string Br100(long broj, Pol p = Pol.m)
{
if (broj >= 100)
return b100[broj / 100 - 1] + "sto " + Br20(broj % 100, p);
else
return Br20(broj, p);
}
static string Br1000(long broj, Pol p = Pol.m)
{
if (broj >= 1000)
return BrojUString(broj / 1000, Pol.ž).Trim() + " " + Padež(broj / 1000, b1000[1], b1000[0]) + " " + Br100(broj % 1000);
else
return Br100(broj, p);
}
static string Br1000000(long broj, Pol p = Pol.m)
{
if (broj >= 1000000)
return BrojUString(broj / 1000000).Trim() + " " + Padež2(broj / 1000000, b1000000[0], b1000000[1]) + " " + Br1000(broj % 1000000, p);
else
return Br1000(broj, p);
}
static string Padež(long broj, string hiljade, string hiljada)
{
long b;
if ((broj >= 10) && (broj <= 20))
b = 1;
else
b = broj % 10;
switch (b)
{
case 2:
case 3:
case 4:
return hiljade;
default:
return hiljada;
}
}
static string Padež2(long broj, string milion, string miliona)
{
long b;
if ((broj >= 10) && (broj <= 20))
b = 2;
else
b = broj % 10;
if (b == 1)
return milion;
else
return miliona;
}
}
}