|
发表于 2020-7-20 21:30:01
|
显示全部楼层
利用自动机理论来做。将数分成4位的节(Section).
public class RMBConvert
{
public RMBConvert()
{
}
private static string[] faceVal = new String[] { "零", "壹", "贰", "叁", "肆",
"伍", "陆", "柒", "捌", "玖" };
private static string []unit = new string[]{"仟","佰","拾",""};
private static string[] level = new String[] { "圆", "拾", "佰", "仟", "萬", "亿" };
public string Convert(string rmb)
{
int len = 0;
string ret = "";
string tmpY = "";
string tmpW = "";
string tmpG = "";
bool bFlag = false;
if(rmb.Length > 8)
rmb = rmb.PadLeft(12, '0');
else if(rmb.Length > 4)
rmb = rmb.PadLeft(8, '0');
else rmb = rmb.PadLeft(4, '0');
if(rmb.Length > 8)
{
tmpY = ConvertSection(rmb, 0, 4, false);
if(tmpY != "零")
{
ret += tmpY + "亿";
bFlag = true;
}
tmpW = ConvertSection(rmb, 4, 4, bFlag);
if(tmpW != "零")
{
ret += tmpW + "万";
bFlag = true;
}
tmpG = ConvertSection(rmb, 8, 4, bFlag);
if(tmpG != "零")
ret += tmpG;
else{
if(tmpY == "零" && tmpW == "零" && tmpG == "零")
ret = "零";
}
}
else if(rmb.Length > 4)
{
tmpW = ConvertSection(rmb, 0, 4, false);
if(tmpW != "零")
{
ret += tmpW + "万";
bFlag = true;
}
tmpG = ConvertSection(rmb, 4, 4, bFlag);
if(tmpG != "零")
{
ret += tmpG;
}
else
{
if(tmpW == "零")
ret = "零";
}
}
else{
ret = ConvertSection(rmb, 0, 4, false);
}
return ret;
}
public string ConvertSection(string value, int start, int len, bool headerZero)
{
return ConvertSection(value.Substring(start, len), headerZero);
}
public string ConvertSection(string value)
{
return ConvertSection(value, false);
}
/// <summary>
/// 主要看这个了
/// </summary>
/// <param name="value"></param>
/// <param name="headerZero"></param>
/// <returns></returns>
public string ConvertSection(string value, bool headerZero)
{
string regVal = value.PadLeft(4, '0');
string strOutput = "";
int state = 0;
for(int i=0; i<4;i++)
{
int crr = regVal[i] - 0x30;
switch(state)
{
case 0:
if( crr == 0 )
{
state = 1;
}
else{
state = 2;
strOutput += faceVal[crr] + unit[i];
}
break;
case 1:
if( crr == 0 )
{
state = 3;
}
else
{
state = 5;
if( headerZero )
strOutput += "零";
strOutput += faceVal[crr] + unit[i];
}
break;
case 2:
if( crr == 0 )
{
state = 4;
}
else
{
state = 5;
strOutput += faceVal[crr] + unit[i];
}
break;
case 3:
if( crr == 0 )
{
state = 11;
}
else
{
state = 6;
if( headerZero )
strOutput += "零";
strOutput += faceVal[crr] + unit[i];
}
break;
case 4:
if( crr == 0 )
{
state = 7;
}
else
{
state = 6;
strOutput += "零" + faceVal[crr] + unit[i];
}
break;
case 5:
if( crr == 0 )
{
state = 7;
}
else
{
state = 6;
strOutput += faceVal[crr] + unit[i];
}
break;
case 6:
if( crr == 0 )
{
state = 8;
}
else
{
state = 9;
strOutput += faceVal[crr] + unit[i];
}
break;
case 7:
if( crr == 0 )
{
state = 10;
}
else
{
state = 9;
strOutput += "零" + faceVal[crr] + unit[i];
}
break;
case 11:
if( crr == 0 )
{
state = 12;
strOutput = "零";
}
else
{
state = 13;
if( headerZero )
strOutput += "零";
strOutput += faceVal[crr] + unit[i];
}
break;
case 8:break;
case 9:break;
case 10:break;
case 12:break;
case 13:break;
} // end of switch
} // end of for
return strOutput;
}
} |
|