|
In fact, the principle of realization is very simple, and I was wrong at the beginning!
It is distinguished by eight bits. After more than eight bits, it will only increase by 100 million. It is the same before eight bits. Therefore, the program implementation is to use eight bits as the node, and calculate the value of each eight bits first. An eight-digit number (counting from the right), each additional eight-digit number adds one billion words!
The specific implementation is as follows: (The following is the implementation of DELPHI)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls;
type
TForm1 = class(TForm)
LedtNum: TLabeledEdit;
BitBtn1: TBitBtn;
ComboBox1: TComboBox;
memo: TMemo;
procedure BitBtn1Click(Sender: TObject);
private
{Private declarations}
procedure ConvertNumber(value: string);
public
{Public declarations}
end;
const
ROWMAX = 2; //The largest number
COLMAX = 3;
U_NUM: array[0..9] of string = ('zero','一','贰','三', '4','Wu','Lu','柒','捌', '玖');
U_LEV: array[0..ROWMAX, 0..COLMAX] of string = (('','lift','百','千'),
('','','', '10,000'),
('','','','billion')
);
{
In fact, the principle of realization is very simple, and I was wrong at the beginning!
It is divided into eight digits. After surpassing eight digits, it only adds one hundred million. It is the same before eight digits.
So the program implementation is to use eight bits as the node, first calculate the value of each eight bits, and then not the first eight bits (counting from the right),
For every eight bits added, one hundred million words will be added!
The specific implementation is as follows:
}
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
ConvertNumber(LedtNum.Text);
end;
procedure TForm1.ConvertNumber(value: string);
var
i, i2, i3: integer;
tmpD, tmpM: integer;//Integer part and decimal part
tmpStrs: array of string;
tmpLst: TStringList;
tmpStr, tmpStr1: string;
function getStr(aValue: string): string;
var
tmpS2: string;
begin
result :='';
i := length(aValue);
while i>0 do
begin
i2 := length(aValue)-i+1;
if aValue[i] <>'0' then
begin
tmpD := (i2-1) div 4;
tmpM := (i2-1) mod 4;
tmpS2 :='';
if tmpD> 0 then
begin
if (tmpD>0) and (tmpD<=2) then
begin
if AnsiPos(U_LEV[tmpD, COLMAX], result)>0 then
tmpS2 := U_LEV[0, tmpM]
else
tmpS2 := U_LEV[0, tmpM]+U_LEV[tmpD, COLMAX];
end;
result := aValue[i]+tmpS2+result;
end else begin
result := aValue[i]+U_LEV[tmpD, tmpM]+result;
end;
end else result := aValue[i]+result;
dec(i);
end;
end;
begin
tmpLst := TStringList.Create;
try
while (length(value)> 0) do
begin
if length(value)< 8 then
begin
tmpLst.Add(value);
break;
end else begin
tmplst.Add(copy(value, length(value)-8+1, 8));
value := copy(value, 1, length(value)-8);
end;
end;
setLength(tmpStrs, tmpLst.Count);
for i3 := 0 to tmpLst.Count-1 do
begin
tmpStrs[i3] := getStr(tmpLst.Strings[i3]);
for i := 1 to i3 do
tmpStrs[i3] := tmpStrs[i3]+'billion';
end;
for i3 := high(tmpStrs) downto low(tmpStrs) do
tmpStr := tmpStr+tmpStrs[i3];
while (AnsiPos('00', tmpStr)>0) do
begin
tmpStr := StringReplace(tmpStr, '00', '0', [rfReplaceAll]);
end;
tmpStr := StringReplace(tmpStr, '0 billion', '100 million', [rfReplaceAll]);
tmpStr1 := tmpStr;
for i3 := 1 to length(tmpStr) do
begin
if tmpStr[i3] in ['0'..'9'] then
begin
tmpStr1 := StringReplace(tmpStr1, tmpStr[i3], U_NUM[strToInt(tmpStr[i3])], [rfReplaceAll]);
end;
end;
memo.Text := tmpStr1;
combobox1.Items := tmpLst;
finally
tmpLst.Free;
end;
end;
end. |
|