|
发表于 2020-7-22 21:45:01
|
显示全部楼层
其实实现的原理很简单,开始也想错了!
就是以八位为区分,超过八位后只不过增加了个亿罢了,在八位以前都是一样的,所以程序实现就是以八位为节点,先算出每个八位的数值,再不是第一个八位(从右边数),每增加一个八位就增加一个亿字就可以了!
具体实现如下:(以下是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; //最大的编号
COLMAX = 3;
U_NUM: array[0..9] of string = ('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
U_LEV: array[0..ROWMAX, 0..COLMAX] of string = (('', '拾', '佰', '仟'),
('', '', '', '萬'),
('', '', '', '亿')
);
{
其实实现的原理很简单,开始也想错了!
就是以八位为区分,超过八位后只不过增加了个亿罢了,在八位以前都是一样的,
所以程序实现就是以八位为节点,先算出每个八位的数值,再不是第一个八位(从右边数),
每增加一个八位就增加一个亿字就可以了!
具体实现如下:
}
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;//整数部分及小数部分
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]+'亿';
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亿', '亿', [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. |
|