求一道简单算法题,大家进来看看! ( 积分: 0 )

  • 主题发起人 贷款娶媳妇
  • 开始时间

贷款娶媳妇

Unregistered / Unconfirmed
GUEST, unregistred user!
题目要求:输入一个数比如说10234567,经过程序处理输出这个数是:壹佰零贰十叁万肆千伍百陆十柒。毕竟是个算法题,要求代码别太长了,尽量优化你的算法。谢谢!!!
 

白河愁

Unregistered / Unconfirmed
GUEST, unregistred user!
没分就算了,还要求优化,太过分了。
 
Z

zzyun

Unregistered / Unconfirmed
GUEST, unregistred user!
借宝地一用,呵呵。
to 白河愁,
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3839845
到这里去瞧瞧,高难度的呀。 呵呵
 
L

lvxq

Unregistered / Unconfirmed
GUEST, unregistred user!
***************************
让ロロロ来帮你!!!
***************************
//下面是上面方法的实现代码,数字输入有以下限制
//不能输入除数字和逗号以外的符号(小数点?当然没有对应了。)
//数值不能太大,因为超过9000亿后我不知道该怎么读了(兆?)
//不能以0打头
public bool ChangeNumber(string sIn, ref string sOut)
{
bool result = false;
string[] out0 = {"零", "壹", "贰","叁", "肆", "伍", "陆", "柒", "捌","玖"};
string[] out1 = { "","拾", "佰", "千", "", "拾", "佰", "千","","拾", "佰", "千" };
string[] out2= {"万", "亿"};
sOut = "";
string sRet = "";
bool numberStarted = false;
bool oldNumberIsZero = false;
//允许输入","
sIn = sIn.Replace(",", "");
int count = sIn.Length;
if (count > 12)
{
//太大的数字(超过9000亿)
return result;
}
if ("0".Equals(sIn.Substring(0, 1)))
{
//0打头的数字
return result;
}
for (int i = 0;
i < sIn.Length;
i++)
{
//取得字符
string sBit = sIn.Substring(sIn.Length - i-1, 1);
//检查有效性
if ("0".CompareTo(sBit) > 0 || "9".CompareTo(sBit) < 0)
{
return result;
}
if ("0".Equals(sBit))
{
if (!numberStarted)
{
continue;
}
else
if (!oldNumberIsZero &amp;&amp;
i!=4 &amp;&amp;
i!=8)
{
sRet = out0[0] + sRet;
}
oldNumberIsZero = true;
if (i == 4)
{
sRet = out2[0] + sRet;
}
else
if (i == 8)
{
sRet = out2[1] + sRet;
}
}
else

{
numberStarted = true;
sRet = out0[int.Parse(sBit) ] + out1 + sRet;
if(i==4)
{
sRet = sRet.Substring(0, 1) + out2[0] + sRet.Substring(1, sRet.Length -1);

}
else
if (i==8)
{
sRet = sRet.Substring(0, 1) + out2[1] + sRet.Substring(1, sRet.Length -1);

}
oldNumberIsZero = false;
}

}
sOut = sRet;
return true;
}
//以下时调用方法
string sout = "";
bool changeok = ChangeNumber("10234567", ref sout);
if(changeok)
{
//
return value was setted in sout
}

***************************
有问题么?让ロロロ来帮你吧
***************************
 

贷款娶媳妇

Unregistered / Unconfirmed
GUEST, unregistred user!
非常感谢lvxq,有一点不明白,就是string[] out1 ={ "","拾", "佰", "千", "", "拾", "佰", "千","","拾", "佰", "千" };解释一下好吗?
 
L

lmxiori

Unregistered / Unconfirmed
GUEST, unregistred user!
function TForm1.ConverToCapitalization(Num:do
uble): String;
var
CapStr, UnitStr, IntStr, FractionStr, StrTemp, NumStr : String;
IntLen, FractionLen, I, IntTemp, Position : Integer;
IntFlag, FrontZeroFlag, AllZeroFlag : Boolean;
begin
//初始化参数
NumStr := FloatToStr(Num);
CapStr := '零壹贰叁肆伍陆柒捌玖拾';
UnitStr := '十百千万亿兆春夏秋冬风火雷电';
FractionLen := 0;
IntLen := 0;
IntLen := Pos('.',NumStr) - 1;
if IntLen = -1 then
begin
IntFlag := True;
IntLen := Length(NumStr);
end
else
IntFlag := False;
if not IntFlag then
begin
FractionLen := Length(NumStr) - IntLen -1;
FractionStr := Copy(NumStr,IntLen +2,FractionLen);
end;

IntStr := '';
For I := IntLendo
wnto 1do
IntStr := IntStr + Copy(NumStr,I,1);
//开始转换整部分
if (IntLen = 1) and (IntStr = '0') then
Result := '零'
else
begin
FrontZeroFlag := False;
AllZeroFlag := True;
For I := IntLendo
wnto 1do
begin
Position := I mod 4;
IntTemp := StrToInt(Copy(IntStr,I,1));
if IntTemp = 0 then
begin
if Position <> 1 then
if FrontZeroFlag then
Continue
else
begin
FrontZeroFlag := True;
Continue;
end;
end
else
begin
if FrontZeroFlag then
Result := Result +'零';
AllZeroFlag := False;
FrontZeroFlag := False;
StrTemp := Copy(CapStr,IntTemp*2+1,2);
Result := Result + StrTemp;
end;

if (not AllZeroFlag) and ((I div 4) > 0) and (Position = 1) then
Result := Result + Copy(UnitStr,((I div 4)-1)*2+1+6,2)
else
if (IntTemp <> 0) and (Position <> 1) then
if Position = 0 then
Result := Result + Copy(UnitStr,5,2)
else
Result := Result + Copy(UnitStr,(Position - 2)*2+1,2);
if Position = 1 then
AllZeroFlag := true;
end;
end;
//开始转换整部分
if FractionLen <> 0 then
begin
Result := Result + '点';
For I := 1 To FractionLendo
Result := Result + Copy(CapStr,StrToInt(Copy(FractionStr,I,1))*2+1,2);
end;
end;
 
J

jjfeng66

Unregistered / Unconfirmed
GUEST, unregistred user!
简单的数字转换成大写的子程序,随便一搜比比皆是,会用检索吗?[8D]
 
B

B_Man

Unregistered / Unconfirmed
GUEST, unregistred user!
这是我用的一条大写金额函数,自己修改一下吧.
function VarToRMB(const n: extended): string;
const
M_CHR = '分角圆拾佰仟万拾佰仟亿拾佰仟';
M_NUM = '零壹贰叁肆伍陆柒捌玖';
var
M_val: string;
M_CHRID, i, wz, s_len: integer;
begin
result := '';
M_CHRID := 0;
M_VAL := Format('%n', [Abs(N)]);
s_len := Length(m_val);
for i := s_lendo
wnto 1do
begin
if (Copy(M_val, i, 1) = '.') or (Copy(M_val, i, 1) = ',') then
Continue;
wz := strtoint(copy(m_val, i, 1));
result := copy(m_num, wz * 2 + 1, 2) + copy(m_chr, m_chrid * 2 + 1, 2) + result;
inc(m_chrid);
end;
if n > 0 then
result := result + '整'
else
result := '负(' + result + '整)';
end;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
703
import
I
I
回复
0
查看
614
import
I
顶部