Bir Sayının Okunuşunu Veren Class
Bu örnekte bir sayının türkçeye göre okunuşu bulunmaktadır. Örneğin 12034 girildiğinde üretilen string onikibinotuzdört olmaktadır.
1 package com. godoro. test;
2 public class NumberTest{
3 public static final String NUMBER_1="bir";
4 public static final String NUMBER_2="iki";
5 public static final String NUMBER_3="üç";
6 public static final String NUMBER_4="dört";
7 public static final String NUMBER_5="beş";
8 public static final String NUMBER_6="altı";
9 public static final String NUMBER_7="yedi";
10 public static final String NUMBER_8="sekiz";
11 public static final String NUMBER_9="dokuz";
12 public static final String NUMBER_10="on";
13 public static final String NUMBER_20="yirmi";
14 public static final String NUMBER_30="otuz";
15 public static final String NUMBER_40="kırk";
16 public static final String NUMBER_50="elli";
17 public static final String NUMBER_60="altmış";
18 public static final String NUMBER_70="yetmiş";
19 public static final String NUMBER_80="seksen";
20 public static final String NUMBER_90="doksan";
21 public static final String NUMBER_100="yüz";
22 public static final String NUMBER_1000="bin";
23 public static final String NUMBER_MILLION="milyon";
24 public static final String NUMBER_MILLIARD="milyar";
25 public static final String NUMBER_TRILLION="trilyon";
26 public String read( String number) {
27 int length=number. length( ) ;
28 int level=( int ) ( ( length- 1) / 3) + 1;
29 String result="";
30 for ( int i=0;i<level;i+ + ) {
31 int start=0;
32 if ( i! =( level- 1) ) {
33 start=length- 3* ( i+ 1) ;
34 }
35 int finish=length- 3* ( i) ;
36 String block=number. substring( start, finish) ;
37 result=readNumber( block, i+ 1) + result;
38 }
39 return result;
40 }
41 private String readNumber( String number, int level) {
42 if ( level==1) {
43 return readThreeNumber( number) ;
44 } else if ( level==2) {
45 return readThousand( number) ;
46 } else {
47 return readBigNumber( number, level) ;
48 }
49 }
50 private String readThreeNumber( String number) {
51 String result="";
52 int location=1;
53 for ( int i=number. length( ) - 1;i>=0;i- - ) {
54 String s=number. substring( i, i+ 1) ;
55 if ( location==1) {
56 result=readNumber( s) ;
57 } else if ( location==2) {
58 result=readTheTens( s) + result;
59 } else if ( location==3) {
60 result=readHundred( s) + result;
61 }
62 location+ + ;
63 }
64 return result;
65 }
66 private String readThousand( String number) {
67 String yuzluk=readThreeNumber( number) ;
68 if ( yuzluk. equals( "") ) {
69 return "";
70 } else if ( yuzluk. equals( "bir") ) {
71 return getLevelString( 2) ;
72 } else {
73 return yuzluk+ getLevelString( 2) ;
74 }
75 }
76 private String readBigNumber( String number, int level) {
77 String yuzluk=readThreeNumber( number) ;
78 if ( yuzluk. equals( "") ) {
79 return "";
80 } else {
81 return yuzluk+ getLevelString( level) ;
82 }
83 }
84 private String readHundred( String number) {
85 if ( number. equals( "0") ) {
86 return "";
87 } else if ( number. equals( "1") ) {
88 return NUMBER_100;
89 } else {
90 return readNumber( number) + NUMBER_100;
91 }
92 }
93 private String readTheTens( String number) {
94 switch ( Integer. parseInt( number) ) {
95 case 1:
96 return NUMBER_10;
97 case 2:
98 return NUMBER_20;
99 case 3:
100 return NUMBER_30;
101 case 4:
102 return NUMBER_40;
103 case 5:
104 return NUMBER_50;
105 case 6:
106 return NUMBER_60;
107 case 7:
108 return NUMBER_70;
109 case 8:
110 return NUMBER_80;
111 case 9:
112 return NUMBER_90;
113 default:
114 return "";
115 }
116 }
117 private String readNumber( String number) {
118 switch ( Integer. parseInt( number) ) {
119 case 1:
120 return NUMBER_1;
121 case 2:
122 return NUMBER_2;
123 case 3:
124 return NUMBER_3;
125 case 4:
126 return NUMBER_4;
127 case 5:
128 return NUMBER_5;
129 case 6:
130 return NUMBER_6;
131 case 7:
132 return NUMBER_7;
133 case 8:
134 return NUMBER_8;
135 case 9:
136 return NUMBER_9;
137 default:
138 return "";
139 }
140 }
141 private String getLevelString( int level) {
142 if ( level==2) {
143 return NUMBER_1000;
144 } if ( level==3) {
145 return NUMBER_MILLION;
146 } else if ( level==4) {
147 return NUMBER_MILLIARD;
148 } else if ( level==5) {
149 return NUMBER_TRILLION;
150 } else {
151 return "";
152 }
153 }
154 public static void main( String[ ] args) {
155 NumberTest test=new NumberTest( ) ;
156 String n="4044";
157 String result=test. read( n) ;
158 System. out. println( "result: "+ result) ;
159 }
160 }
read method'u verilen bir sayının okunuşunu döndürmektedir. Algoritma şu şekildedir. Türkçede en önemli yüzlük sayıları okumaktadır. Örneğin 234,534,123 gibi. Eğer bu sayılar doğru bir şekilde okunabilirse tüm sayılar okunabilir demektir. Örneğin 12345 gibi bir sayı önce 12 sayısı sonra 345 sayısının okunuşu ile elde edilir. Tek özellik 12 okunuşun sonuna "bin" kelimesini eklenmesidir (oniki bin üçyüzkırkbeş). Daha büyük rakamlar içinde kural değişmez. Burada bin yerine milyon,milyar,trilyon gibi kelimeler eklenir. Örneğin 123456789 gibi bir sayı yüzyürmiüç milyon dörtyüzellialtı bin yediyüzseksendokuz şeklinde okunur. readThreeNumber method'u 999 ve daha küçük rakamları okuyan method'dur.
Dosya Listesi