一道java笔试题,以为很简单,结果半天都没写对!请大虾给出个正确解答

fanz2000 2007-01-04 01:43:11
给一个阿拉伯数字
例如输入:123456789
输出:壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖

我以为这个功能很简单,但是发现如果数字中间出现多个0的情况,就要有问题

贴一下我的代码,比较差劲
public static String daxie(int sum)
{
String [] n=new String[10];
n[0]="零";
n[1]="壹";
n[2]="贰";
n[3]="叁";
n[4]="肆";
n[5]="伍";
n[6]="陆";
n[7]="柒";
n[8]="捌";
n[9]="玖";
String [] d=new String[10];
d[0]="";
d[1]="";
d[2]="拾";
d[3]="佰";
d[4]="仟";
d[5]="萬";
d[6]="拾";
d[7]="佰";
d[8]="仟";
d[9]="亿";
//计算数字的位数
int wei=(int)Math.floor
(
Math.log10((double)sum)
)+1;
System.out.println(wei);
String str="";
int digit=0;
for (int i=wei;i>0;i--)
{
digit=sum/
(int)Math.pow(10.0, (double)(i-1));
str+=n[digit].toString()+d[i].toString();
sum=sum%
(int)Math.pow(10.0, (double)(i-1));
}

str= str.replace("拾零", "拾");
str= str.replace("零拾", "零");
str= str.replace("零佰", "零");
str= str.replace("零仟", "零");
str= str.replace("零萬", "萬");
for (int i=1;i<=6;i++)
str= str.replace("零零", "零");
str=str.replace("零萬", "零");
str=str.replace("零亿", "亿");
str= str.replace("零零", "零");
if (str.endsWith("零"))
{
str=str.substring(0, str.length()-1);
}
System.out.println(str);
return str;
}
...全文
10818 93 打赏 收藏 转发到动态 举报
写回复
用AI写文章
93 条回复
切换为时间正序
请发表友善的回复…
发表回复
ybyb14 2008-03-12
  • 打赏
  • 举报
回复
找了半天,没看到一个好算法,我记得以前有个用树来求的,代码很简洁!
yigui2001 2007-01-10
  • 打赏
  • 举报
回复
mark&up
minani 2007-01-09
  • 打赏
  • 举报
回复
package num;

public class NumReader {

public static String[] n = new String[10];

public static String[] d = new String[6];

static {
n[0] = "零";
n[1] = "壹";
n[2] = "贰";
n[3] = "叁";
n[4] = "肆";
n[5] = "伍";
n[6] = "陆";
n[7] = "柒";
n[8] = "捌";
n[9] = "玖";

d[0] = "";
d[1] = "拾";
d[2] = "佰";
d[3] = "仟";
d[4] = "萬";
d[5] = "亿";
}

public static String readNum(String num) {
if(!checkParam(num)){
System.out.println(num + " is not a valid number");
}
StringBuffer sb = new StringBuffer();

int len = num.length();
String[] numbers = new String[len];
String[] units = new String[len];
boolean[] numFlags = new boolean[len];
boolean[] unitFlags = new boolean[len];

//caculate every pos
for(int i=0;i<len;i++){
Result rs = read(num,i);
numbers[i] = rs.number;
units[i] = rs.unit;
numFlags[i] = rs.numFlag;
unitFlags[i] = rs.unitFlag;

// join string
if(numFlags[i]){
sb.append(numbers[i]);
}
if(unitFlags[i]){
sb.append(units[i]);
}
}

return sb.toString();
}

public static boolean checkParam(String num) {
try {
Double.parseDouble(num);
} catch (NumberFormatException e) {
return false;
}
return true;
}

public static Result read(String num, int pos) {
Result rs = new Result();
rs.number = readNum(num,pos);
rs.unit = readUnit(num,pos);
rs.numFlag = readNumFlag(num,pos);
rs.unitFlag = readUnitFlag(num,pos);
return rs;
}

public static String readNum(String num, int pos) {
int value = Integer.parseInt(num.substring(pos,pos+1));
return n[value];
}

public static String readUnit(String num, int pos) {
int len = num.length();
int count = len - pos - 1;

int unit = count % 4;
String unitStr = d[unit];

int rate = count / 4;
if( count >= 4 && count % 4 == 0){
if(rate % 2 == 0){
unitStr += d[5];
} else {
unitStr += d[4];
}
}
return unitStr;
}

public static boolean readNumFlag(String num, int pos) {
int len = num.length();
int value = Integer.parseInt(num.substring(pos,pos+1));
int count = len - pos - 1;
if( len %4 == 2 && value == 1 && pos == 0){
return false;
}
if(value != 0){
return true;
}

if ( count % 4 == 0 ){
return false;
}
int nextValue = Integer.parseInt(num.substring(pos + 1,pos+2));
if(nextValue != 0){
return true;
}
return false;
}

public static boolean readUnitFlag(String num, int pos) {
int value = Integer.parseInt(num.substring(pos,pos+1));
if(value != 0){
return true;
}
int len = num.length();
int count = len - pos - 1;
if ( count % 4 == 0 ){
return true;
}
return false;
}

public static class Result{
String number = null;
String unit = null;
boolean numFlag = false;
boolean unitFlag = false;
}

public static void main(String[] args) {
String testNum = "123456789012345678";
String str = readNum(testNum);
System.out.println(testNum);
System.out.println(str);
}
}
lzp5115 2007-01-09
  • 打赏
  • 举报
回复
昨天用vc做的
void CNumDlg::AddStr(CString sStr, CString *sNum, TCHAR tcUnit)
{
int t = sStr.GetLength();
CString sTemp = _T("");
for (int i=0; i<t; i++)
{
TCHAR tcTemp = sStr.GetAt(t-1-i);
switch (tcTemp)
{
case _T('0'):
if (sTemp == _T("") || sTemp.GetAt(0) == _T('零'))
{
break;
}
else
{
sTemp.Insert(0, _T("零"));
}
break;
case _T('1'):
sTemp.Insert(0, _T("壹"));
break;
case _T('2'):
sTemp.Insert(0, _T("贰"));
break;
case _T('3'):
sTemp.Insert(0, _T("叁"));
break;
case _T('4'):
sTemp.Insert(0, _T("肆"));
break;
case _T('5'):
sTemp.Insert(0, _T("伍"));
break;
case _T('6'):
sTemp.Insert(0, _T("陆"));
break;
case _T('7'):
sTemp.Insert(0, _T("柒"));
break;
case _T('8'):
sTemp.Insert(0, _T("捌"));
break;
case _T('9'):
sTemp.Insert(0, _T("玖"));
break;
default:
break;
}
switch(i)
{
case 0:
break;
case 1:
if (sTemp != _T("") && sTemp.GetAt(0) != _T('零'))
{
sTemp.Insert(1, _T("拾"));
}
break;
case 2:
if (sTemp != _T("") && sTemp.GetAt(0) != _T('零'))
{
sTemp.Insert(1, _T("佰"));
}
break;
case 3:
if (sTemp != _T("") && sTemp.GetAt(0) != _T('零'))
{
sTemp.Insert(1, _T("仟"));
}
break;
default:
break;
}
}
if (tcUnit != (TCHAR)0 && sTemp!= _T(""))
{
sNum->Insert(0,tcUnit);
}
else
{
if (tcUnit == _T('亿'))
{
sNum->Insert(0,tcUnit);
}
}
sNum->Insert(0,sTemp);
}

void CNumDlg::OnRead()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CString sNum = _T("");
int t = m_sNum.GetLength();
while (true)
{
if (t>0 && m_sNum.GetAt(0) == _T('0'))
{
m_sNum.Delete(0);
t--;
continue;
}
break;
}
if (t<=0)
{
MessageBox(_T("您的输入错误!"));
return;
}
for (int i = 0; i<t; i++)
{
TCHAR tc = m_sNum.GetAt(i);

if (!_istdigit(tc))
{
MessageBox(_T("请输入数字!"));
return;
}
}
if (t<=4)
{
AddStr (m_sNum, &sNum, (TCHAR)0);
}
if (t>4 && t<=8)
{
CString sTh;
sTh = m_sNum.Right(4);
AddStr (sTh, &sNum, (TCHAR)0);
CString sM;
sM = m_sNum.Left(m_sNum.GetLength() - 4);
AddStr (sM, &sNum, _T('万'));
}
if (t>8 && t<=12)
{
CString sTh;
sTh = m_sNum.Right(4);
AddStr (sTh, &sNum, (TCHAR)0);
CString sM;
sM = m_sNum.Mid(m_sNum.GetLength()-8, 4);
AddStr (sM, &sNum, _T('万'));
CString hM;
hM = m_sNum.Left(m_sNum.GetLength() - 8);
AddStr (hM, &sNum, _T('亿'));
}
if (t>12 && t<=16)
{
CString sTh;
sTh = m_sNum.Right(4);
AddStr (sTh, &sNum, (TCHAR)0);
CString sM;
sM = m_sNum.Mid(m_sNum.GetLength()-8, 4);
AddStr (sM, &sNum, _T('万'));
CString hM;
hM = m_sNum.Mid(m_sNum.GetLength()-12, 4);
AddStr (hM, &sNum, _T('亿'));
CString mHM;
mHM = m_sNum.Left(m_sNum.GetLength() - 12);
AddStr (mHM, &sNum, _T('万'));
}
if (sNum.GetAt(0)==_T('零'))
{
sNum.Delete(0);
}
m_sRead = sNum;
UpdateData(FALSE);
}
angelleecash 2007-01-08
  • 打赏
  • 举报
回复
import java.util.Hashtable;
import java.util.Vector;
class Converter {
// 中国的计数是四位一个单位的处理方式 例如 1 5487 4584 对应的单位是 亿 万 默认
// 每四位的一个单元中,处理方式是一样的,当前能够处理的最大绝对值位数是UNIT_NAME.length * 4
private final static String[] UNIT_NAME = {"亿", "萬", ""};
private final static String[] SUB_UNIT_NAME = {"仟", "佰", "拾"};
private final static String[] NUMBER_NAME = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
private static Hashtable<Integer, String> nums = new Hashtable<Integer, String>();
static {
for (int i = 0; i < NUMBER_NAME.length; i++) {
nums.put(i, NUMBER_NAME[i]);
}
}
public static String convert(long value) throws TooBigNumberException {
boolean isMinus = value < 0;
if (isMinus) {
value = Math.abs(value);
}
String result = "";
String valueString = String.valueOf(value);
char[] chars = valueString.toCharArray();
if (chars.length > UNIT_NAME.length * 4) {
throw new TooBigNumberException();
} else {
// 当前可以处理,进行四段分割,每部分进行相同的处理,再添加各自段的单位
String[] segments = splitValue2Segments(chars, 4);
for (int i = 0, j = UNIT_NAME.length - segments.length; i < segments.length; i++, j++) {
if (segments.length > 1 && i == segments.length - 1) {
if (Integer.parseInt(segments[i]) < 100) {
result += "零";
}
}
if (Integer.parseInt(segments[i]) > 0) {
result += translate(segments[i]) + UNIT_NAME[j];
}

}
}
if (isMinus) {
result = "负" + result;
}
return result;
}
/**
* 将一个最多四个数的片段转换为自然语言表示方式 每一段由最多四为组成,最大数为9999
*
* @param value
* @return
*/
private static String translate(String value) {
String result = "";
int intValue = Integer.parseInt(value);
int thousandBit = intValue / 1000;
int hundredBit = (intValue - thousandBit * 1000) / 100;
int tenthBit = (intValue - thousandBit * 1000 - hundredBit * 100) / 10;
int oneBit = intValue - thousandBit * 1000 - hundredBit * 100 - tenthBit * 10;
if (thousandBit > 0) {
result += nums.get(thousandBit) + "仟";
if (hundredBit > 0) {
result += nums.get(hundredBit) + "佰";
if (tenthBit > 0) {
if (tenthBit > 1) {
result += nums.get(tenthBit) + "拾";
} else {
result += "拾";
}

if (oneBit > 0) {
result += nums.get(oneBit);
}
} else {
if (oneBit > 0) {
result += "零";
result += nums.get(oneBit);
}
}
} else {
if (tenthBit > 0) {
result += "零";
if (tenthBit > 1) {
result += nums.get(tenthBit) + "拾";
} else {
result += "拾";
}

if (oneBit > 0) {
result += nums.get(oneBit);
}
} else {
if (oneBit > 0) {
result += nums.get(oneBit);
}

}

}
} else {
if (hundredBit > 0) {
result += nums.get(hundredBit) + "佰";
if (tenthBit > 0) {
if (tenthBit > 1) {
result += nums.get(tenthBit) + "拾";
} else {
result += "拾";
}

if (oneBit > 0) {
result += nums.get(oneBit);
}
} else {
if (oneBit > 0) {
result += "零";
result += nums.get(oneBit);
}
}
} else {
if (tenthBit > 0) {
if (tenthBit > 1) {
result += nums.get(tenthBit) + "拾";
} else {
result += "拾";
}
if (oneBit > 0) {
result += nums.get(oneBit);
}
} else {
if (oneBit > 0) {
result += nums.get(oneBit);
}
}
}
}
// System.out.println("Translating from " + value + " To :" + result);
return result;
}
public static void main(String[] args) {
try {
System.out.println(convert(1000000009));
} catch (TooBigNumberException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 把字符数组分割为指定段数
*
* @param chars
* @return
*/
public static String[] splitValue2Segments(char[] chars, int segments) {
int count = 0;
Vector<String> v = new Vector<String>();
StringBuffer sb = new StringBuffer();
for (int i = chars.length - 1; i >= 0; i--) {
if (count >= segments) {
v.add(sb.toString());
sb = new StringBuffer();
count = 0;
}
sb.insert(0, chars[i]);
count++;
}
if (sb.length() > 0) {
v.add(sb.toString());
}
String[] result = new String[v.size()];
for (int i = v.size() - 1, j = 0; i >= 0; i--) {
result[j++] = v.elementAt(i);
System.out.println(v.elementAt(i));
}
return result;
}
}
class TooBigNumberException extends Exception {
public String toString() {
return "数值过大,当前系统无法处理。";
}
}
健之 2007-01-08
  • 打赏
  • 举报
回复
上面的程序掉了一句,去除尾零的句子:
……
Cvalue=Cvalue.replaceFirst("亿万","亿");//去除亿万连接的非法情况
if(Cvalue.endsWith("零")) Cvalue=Cvalue.substring(0,Cvalue.length()-1);//去除尾零
System.out.println(Cvalue);
……
一个例子为:
1002003090:壹拾亿零贰佰零万叁仟零玖拾
angelleecash 2007-01-08
  • 打赏
  • 举报
回复
import java.util.Hashtable;
import java.util.Vector;
class Converter {
// 中国的计数是四位一个单位的处理方式 例如 1 5487 4584 对应的单位是 亿 万 默认
// 每四位的一个单元中,处理方式是一样的,当前能够处理的最大绝对值位数是UNIT_NAME.length * 4
private final static String[] UNIT_NAME = {"亿", "萬", ""};
private final static String[] SUB_UNIT_NAME = {"仟", "佰", "拾"};
private final static String[] NUMBER_NAME = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
private static Hashtable<Integer, String> nums = new Hashtable<Integer, String>();
static {
for (int i = 0; i < NUMBER_NAME.length; i++) {
nums.put(i, NUMBER_NAME[i]);
}
}
public static String convert(long value) throws TooBigNumberException {
boolean isMinus = value < 0;
if (isMinus) {
value = Math.abs(value);
}
String result = "";
String valueString = String.valueOf(value);
char[] chars = valueString.toCharArray();
if (chars.length > UNIT_NAME.length * 4) {
throw new TooBigNumberException();
} else {
// 当前可以处理,进行四段分割,每部分进行相同的处理,再添加各自段的单位
String[] segments = splitValue2Segments(chars, 4);
for (int i = 0, j = UNIT_NAME.length - segments.length; i < segments.length; i++) {
if (segments.length > 1 && i == segments.length - 1) {
if (Integer.parseInt(segments[i]) < 100) {
result += "零";
}
}
result += translate(segments[i]) + UNIT_NAME[j++];
}
}
if (isMinus) {
result = "负" + result;
}
return result;
}
/**
* 将一个最多四个数的片段转换为自然语言表示方式 每一段由最多四为组成,最大数为9999
*
* @param value
* @return
*/
private static String translate(String value) {
String result = "";
int intValue = Integer.parseInt(value);
int thousandBit = intValue / 1000;
int hundredBit = (intValue - thousandBit * 1000) / 100;
int tenthBit = (intValue - thousandBit * 1000 - hundredBit * 100) / 10;
int oneBit = intValue - thousandBit * 1000 - hundredBit * 100 - tenthBit * 10;
if (thousandBit > 0) {
result += nums.get(thousandBit) + "仟";
if (hundredBit > 0) {
result += nums.get(hundredBit) + "佰";
if (tenthBit > 0) {
result += nums.get(tenthBit) + "拾";
if (oneBit > 0) {
result += nums.get(oneBit);
}
} else {
if (oneBit > 0) {
result += "零";
result += nums.get(oneBit);
}
}
} else {
if (tenthBit > 0) {
result += "零";
result += nums.get(tenthBit) + "拾";
if (oneBit > 0) {
result += nums.get(oneBit);
}
} else {
if (oneBit > 0) {
result += nums.get(oneBit);
}

}

}
} else {
if (hundredBit > 0) {
result += nums.get(hundredBit) + "佰";
if (tenthBit > 0) {
result += nums.get(tenthBit) + "拾";
if (oneBit > 0) {
result += nums.get(oneBit);
}
} else {
if (oneBit > 0) {
result += "零";
result += nums.get(oneBit);
}
}
} else {
if (tenthBit > 0) {
result += nums.get(tenthBit) + "拾";
if (oneBit > 0) {
result += nums.get(oneBit);
}
} else {
if (oneBit > 0) {
result += nums.get(oneBit);
}
}
}
}
// System.out.println("Translating from " + value + " To :" + result);
return result;
}
public static void main(String[] args) {
try {
System.out.println(convert(8888683));
} catch (TooBigNumberException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 把字符数组分割为指定段数
*
* @param chars
* @return
*/
public static String[] splitValue2Segments(char[] chars, int segments) {
int count = 0;
Vector<String> v = new Vector<String>();
StringBuffer sb = new StringBuffer();
for (int i = chars.length - 1; i >= 0; i--) {
if (count >= segments) {
v.add(sb.toString());
sb = new StringBuffer();
count = 0;
}
sb.insert(0, chars[i]);
count++;
}
if (sb.length() > 0) {
v.add(sb.toString());
}
String[] result = new String[v.size()];
for (int i = v.size() - 1, j = 0; i >= 0; i--) {
result[j++] = v.elementAt(i);
System.out.println(v.elementAt(i));
}
return result;
}
}
class TooBigNumberException extends Exception {
public String toString() {
return "数值过大,当前系统无法处理。";
}
}
angelleecash 2007-01-08
  • 打赏
  • 举报
回复
import java.util.Hashtable;
import java.util.Vector;
class Converter {
// 中国的计数是四位一个单位的处理方式 例如 1 5487 4584 对应的单位是 亿 万 默认
// 每四位的一个单元中,处理方式是一样的,当前能够处理的最大绝对值位数是UNIT_NAME.length * 4
private final static String[] UNIT_NAME = {"亿", "萬", ""};
private final static String[] SUB_UNIT_NAME = {"仟", "佰", "拾"};
private final static String[] NUMBER_NAME = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
private static Hashtable<Integer, String> nums = new Hashtable<Integer, String>();
static {
for (int i = 0; i < NUMBER_NAME.length; i++) {
nums.put(i, NUMBER_NAME[i]);
}
}
public static String convert(long value) throws TooBigNumberException {
boolean isMinus = value < 0;
if (isMinus) {
value = Math.abs(value);
}
String result = "";
String valueString = String.valueOf(value);
char[] chars = valueString.toCharArray();
if (chars.length > UNIT_NAME.length * 4) {
throw new TooBigNumberException();
} else {
// 当前可以处理,进行四段分割,每部分进行相同的处理,再添加各自段的单位
String[] segments = splitValue2Segments(chars, 4);
for (int i = 0, j = UNIT_NAME.length - segments.length; i < segments.length; i++) {
if (segments.length > 1 && i == segments.length - 1) {
result += "零";
}
result += translate(segments[i]) + UNIT_NAME[j++];
}
}
if (isMinus) {
result = "负" + result;
}
return result;
}
/**
* 将一个最多四个数的片段转换为自然语言表示方式 每一段由最多四为组成,最大数为9999
*
* @param value
* @return
*/
private static String translate(String value) {
String result = "";
int intValue = Integer.parseInt(value);
int thousandBit = intValue / 1000;
int hundredBit = (intValue - thousandBit * 1000) / 100;
int tenthBit = (intValue - thousandBit * 1000 - hundredBit * 100) / 10;
int oneBit = intValue - thousandBit * 1000 - hundredBit * 100 - tenthBit * 10;
if (thousandBit > 0) {
result += nums.get(thousandBit) + "仟";
if (hundredBit > 0) {
result += nums.get(hundredBit) + "佰";
if (tenthBit > 0) {
result += nums.get(tenthBit) + "拾";
if (oneBit > 0) {
result += nums.get(oneBit);
}
} else {
if (oneBit > 0) {
result += "零";
result += nums.get(oneBit);
}
}
} else {
if (tenthBit > 0) {
result += "零";
result += nums.get(tenthBit) + "拾";
if (oneBit > 0) {
result += nums.get(oneBit);
}
} else {
if (oneBit > 0) {
result += nums.get(oneBit);
}

}

}
} else {
if (hundredBit > 0) {
result += nums.get(hundredBit) + "佰";
if (tenthBit > 0) {
result += nums.get(tenthBit) + "拾";
if (oneBit > 0) {
result += nums.get(oneBit);
}
} else {
if (oneBit > 0) {
result += "零";
result += nums.get(oneBit);
}
}
} else {
if (tenthBit > 0) {
result += nums.get(tenthBit) + "拾";
if (oneBit > 0) {
result += nums.get(oneBit);
}
} else {
if (oneBit > 0) {
result += nums.get(oneBit);
}
}
}
}
// System.out.println("Translating from " + value + " To :" + result);
return result;
}
public static void main(String[] args) {
try {
System.out.println(convert(10000004));
} catch (TooBigNumberException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 把字符数组分割为指定段数
*
* @param chars
* @return
*/
public static String[] splitValue2Segments(char[] chars, int segments) {
int count = 0;
Vector<String> v = new Vector<String>();
StringBuffer sb = new StringBuffer();
for (int i = chars.length - 1; i >= 0; i--) {
if (count >= segments) {
v.add(sb.toString());
sb = new StringBuffer();
count = 0;
}
sb.insert(0, chars[i]);
count++;
}
if (sb.length() > 0) {
v.add(sb.toString());
}
String[] result = new String[v.size()];
for (int i = v.size() - 1, j = 0; i >= 0; i--) {
result[j++] = v.elementAt(i);
}
return result;
}
}
class TooBigNumberException extends Exception {
public String toString() {
return "数值过大,当前系统无法处理。";
}
}
健之 2007-01-08
  • 打赏
  • 举报
回复
还在讨论呀。其实主要就是有几点要注意:
1)一般情况下,正确地对应中文和数字的叫法,这主要是根据“拾佰仟万”以后应该再叫“拾佰仟”万,“亿”以后应该重复“拾佰仟万”亿来确定,所以,我们给出完整的单位序列应该是“拾佰仟万拾佰仟亿拾佰仟万”(由于整形的位数限制,到这里就够了);
2)出现0的情况,习惯上就应该有一个,而且只有一个零就够了,比如:1003应该叫做一千零三
3)当在万和亿以上的数字,如果出现0,应该加上万和亿的单位,如:10030叫一万零三叁拾,1003000040叫做一拾亿零叁佰万零四拾
4)特殊的,如果有1000000001应叫一拾亿零一,中间不要有万的情况出现
所以,个人觉得前面我给出的程序是对的,基本解决了这几个问题,而且程序也不太复杂,大家讨论。这了再贴一次:
public static void main(String[] arg) throws IOException {
String d="拾佰仟万拾佰仟亿拾佰仟万";
String s="零壹贰叁肆伍陆柒捌玖";
boolean zero=false;
long value=1000000009;
int count=0;
String Cvalue="";
while(value>0) {
int b=(int)(value % 10);
if(!zero || b!=0) {
String t="";
if(b!=0) {
t=s.substring(b,b+1);
if(count>0) t+=d.substring(count-1,count);
zero=false;
}
else if(count!=4 && count!=8){
t="零";
zero=true;
}
Cvalue=t+Cvalue;
}
if(count==4 && !Cvalue.contains("万")) Cvalue="万"+Cvalue;
if(count==8 && !Cvalue.contains("亿")) Cvalue="亿"+Cvalue;
count++;
value/=10;
}
Cvalue=Cvalue.replaceFirst("亿万","亿");
System.out.println(Cvalue);
}
以下是几个结果:
1000000009:壹拾亿零玖
1023000009:壹拾亿贰仟叁佰万零玖
123000809:壹亿贰仟叁佰万零捌佰零玖
1230809:壹佰贰拾叁万零捌佰零玖
18009:壹万捌仟零玖
8092:捌仟零玖拾贰
dcms 2007-01-08
  • 打赏
  • 举报
回复
我的搜索引擎继7月份第二次开放测试后,现在进行第三次开放测试,请大家多批评指正。
无为搜索引擎开始测试了啊
(第三次开放测试啊) 网站运行后的效果请参见:
http://219.233.38.213/Search/WuWei.aspx
代码下载地址 :http://www.ofile.cn/se.rar
前两次用 CGI+html做了一个版本还做了一个 Java版本 (这次比较忙,所以没来的及更新,有需要的跟我说一声啊)
这次用 C# ASP.NET做一个 玩玩
做的不好,请大家做指教啊!
这次还实现了计算器功能啊
可以搜索一下 sin(1+2) 看看
另外 sh=1&sf=1&ua=1&sa=1&st=1&wh=1&ph=1 把这里面的 1改成 0看看 每个部分都是可以控制的:)
至于为什么要把客户端网站部分开源,请参考我第二次开放测试时即兴写的文章 : 最近我研究搜索引擎 七 (长长中国人的志气篇)
http://www.baidu.com/s?wd=%D7%EE%BD%FC%CE%D2%D1%D0%BE%BF%CB%D1%CB%F7%D2%FD%C7%E6%C6%DF&cl=3
承盟广大网友看的起 该文目前已经被全国 2000多网站转载
先说一下技术指标: 本次我收录了全国30多万个一、二级域名, 网页采集用了三周时间,目前已经采集了 12万网站,平均每个网站收录 30篇网页,索引进行了 10天,
目前已经索引了 100多万网页 。 搜索核心部分采用这次采用 C 语言开发。 客户端提供三个版本一个CGI+HTML版本,一个 Java版本,一个 ASP.NET版本。

代码下载地址 :http://www.ofile.cn/se.rar
搜索网站客户端(.NET版本)整体解决方案(源代码)
(第三次开放测试啊) 网站运行后的效果请参见:
http://219.233.38.213/Search/WuWei.aspx
angelleecash 2007-01-08
  • 打赏
  • 举报
回复
import java.util.Hashtable;
import java.util.Vector;
class Converter {
// 中国的计数是四位一个单位的处理方式 例如 1 5487 4584 对应的单位是 亿 万 默认
// 每四位的一个单元中,处理方式是一样的,当前能够处理的最大绝对值位数是UNIT_NAME.length * 4
private final static String[] UNIT_NAME = {"亿", "萬", ""};
private final static String[] SUB_UNIT_NAME = {"仟", "佰", "拾"};
private final static String[] NUMBER_NAME = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
private static Hashtable<Integer, String> nums = new Hashtable<Integer, String>();
static {
for (int i = 0; i < NUMBER_NAME.length; i++) {
nums.put(i, NUMBER_NAME[i]);
}
}
public static String convert(long value) throws TooBigNumberException {
boolean isMinus = value < 0;
if (isMinus) {
value = Math.abs(value);
}
String result = "";
String valueString = String.valueOf(value);
char[] chars = valueString.toCharArray();
if (chars.length > UNIT_NAME.length * 4) {
throw new TooBigNumberException();
} else {
// 当前可以处理,进行四段分割,每部分进行相同的处理,再添加各自段的单位
String[] segments = splitValue2Segments(chars, 4);
for (int i = 0; i < segments.length; i++) {
System.out.println("Segment " + i + " :" + segments[i]);
}
for (int i = 0, j = UNIT_NAME.length - segments.length; i < segments.length; i++) {
result += translate(segments[i]) + UNIT_NAME[j++];
}
}
if (isMinus) {
result = "负" + result;
}
return result;
}
/**
* 将一个最多四个数的片段转换为自然语言表示方式 每一段由最多四为组成,最大数为9999
*
* @param value
* @return
*/
private static String translate(String value) {
String result = "";
int intValue = Integer.parseInt(value);
int thousandBit = intValue / 1000;
int hundredBit = (intValue - thousandBit * 1000) / 100;
int tenthBit = (intValue - thousandBit * 1000 - hundredBit * 100) / 10;
int oneBit = intValue - thousandBit * 1000 - hundredBit * 100 - tenthBit * 10;
if (thousandBit > 0) {
result += nums.get(thousandBit) + "仟";
if (hundredBit > 0) {
result += nums.get(hundredBit) + "佰";
result += nums.get(tenthBit) + "拾";
result += nums.get(oneBit);
} else {
result += "零";
result += nums.get(tenthBit) + "拾";
result += nums.get(oneBit);
}
} else {
if (hundredBit > 0) {
result += nums.get(hundredBit) + "佰";
result += nums.get(tenthBit) + "拾";
result += nums.get(oneBit);
} else {
if (tenthBit > 0) {
result += nums.get(tenthBit) + "拾";
result += nums.get(oneBit);
} else {
result += nums.get(oneBit);
}
}
}
// System.out.println("Translate " + value + " To :" + result);
return result;
}
public static void main(String[] args) {
try {
System.out.println(convert(-3423555));
} catch (TooBigNumberException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 把字符数组分割为指定段数
*
* @param chars
* @return
*/
public static String[] splitValue2Segments(char[] chars, int segments) {
int count = 0;
Vector<String> v = new Vector<String>();
StringBuffer sb = new StringBuffer();
for (int i = chars.length - 1; i >= 0; i--) {
if (count >= segments) {
v.add(sb.toString());
sb = new StringBuffer();
count = 0;
}
sb.insert(0, chars[i]);
count++;
}
if (sb.length() > 0) {
v.add(sb.toString());
}
String[] result = new String[v.size()];
for (int i = v.size() - 1, j = 0; i >= 0; i--) {
result[j++] = v.elementAt(i);
}
return result;
}
}
class TooBigNumberException extends Exception {
public String toString() {
return "数值过大,当前系统无法处理。";
}
}
kaipingk 2007-01-08
  • 打赏
  • 举报
回复
欢迎测试并指正错误!


public class DigitToChinese
{
public static final String CHINESE_NUMBER="零壹贰叁肆伍陆柒捌玖";
private static final String CARRY_UNITS[]={"","拾","佰","仟"};
private static final String CARRY_UNITS_BIG[]={"","万","亿","兆","硕","京"};
public String convert(String digit){
return convert(toIntArray(digit));

}

private int [] toIntArray(String digit){
char [] chars = digit.toCharArray();
int [] toReturn = new int[digit.length()];
for(int i=0;i<toReturn.length;i++){
toReturn[i] = (chars[i]-'0');
if(toReturn[i]<0||(chars[i]-'0')>9)
throw new NumberFormatException("数字字符串格式不正确");
}
return toReturn;
}

private String convert(int [] digit){
StringBuffer out = new StringBuffer();
int maxLen=CARRY_UNITS.length*(CARRY_UNITS_BIG.length-1);
int offset = 0;
boolean outZero = false;
for(int i=0;i<maxLen&&(i+offset)<digit.length;i++){
int curDigit = digit.length-(i+offset+1);
if(i%CARRY_UNITS.length==0&&shouldOutBigUnit(digit,curDigit)) {
out.append(CARRY_UNITS_BIG[i/CARRY_UNITS.length]);
outZero=false;
}
if(digit[digit.length-(i+offset+1)]!=0){
out.append(CARRY_UNITS[i%CARRY_UNITS.length]);
out.append(CHINESE_NUMBER.charAt(digit[curDigit]));
outZero = true;
}else{
if(outZero){
out.append(CHINESE_NUMBER.charAt(0));
outZero=false;
}
}
if(i==maxLen-1&&digit.length>maxLen){
i=-1;
offset+=maxLen;
if(shouldOutBigUnit(digit,digit.length-(offset+1)))
out.append(CARRY_UNITS_BIG[CARRY_UNITS_BIG.length-1]);
}
}
return reverse(out).toString();
}

public boolean shouldOutBigUnit(int [] digit,int p){
for(int i=0;i<CARRY_UNITS.length&&(p-i)>=0;i++){
if(digit[p-i]!=0)
return true;
}
return false;
}

private StringBuffer reverse(StringBuffer sb){
int len = sb.length();
for(int i=0;i<len/2;i++){
char a = sb.charAt(i);
sb.setCharAt(i, sb.charAt(len-i-1));
sb.setCharAt(len-i-1, a);
}
return sb;
}

public static void main(String[] args) {
DigitToChinese dtc = new DigitToChinese();
dtc.convert("100564100515140122");
}
}
bluebilly 2007-01-08
  • 打赏
  • 举报
回复
还有负数一样,预处理一下就可以了,呵呵!
程序中没有处理,请楼主自行处理吧!
bluebilly 2007-01-08
  • 打赏
  • 举报
回复
回wyz0311310213:
你所说的不是主要问题,只要在处理时对输入的数字预处理一下就可以了!
bluebilly 2007-01-08
  • 打赏
  • 举报
回复
其实实现的原理很简单,开始也想错了!
就是以八位为区分,超过八位后只不过增加了个亿罢了,在八位以前都是一样的,所以程序实现就是以八位为节点,先算出每个八位的数值,再不是第一个八位(从右边数),每增加一个八位就增加一个亿字就可以了!
具体实现如下:(以下是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.
amwanfwbx 2007-01-08
  • 打赏
  • 举报
回复
搞笑,ACCP软工培训S1试题
HAPPY冰石 2007-01-08
  • 打赏
  • 举报
回复
累~
不管那么多了~看你们那么辛苦~收下了
Liszt_yp 2007-01-08
  • 打赏
  • 举报
回复
用C++实现了一下,感觉主要还是在零的处理上:
多个零相邻的情况有好几种,最重要的是要处理好这几个零在万位和亿位周围的情况。
emin_lee 2007-01-08
  • 打赏
  • 举报
回复
hehe:)
String money = "100000000010.12645";
改成
String money = "100000000010.12";
注释了
没法保留二位小数然后四舍五入了:)、
emin_lee 2007-01-08
  • 打赏
  • 举报
回复
偶也凑凑热闹:)
-----------------------------------------
public final class RMB{
private RMB(){
}

/**
* 将给定的阿拉伯数字转化为中文大写钱款
*
* @param input 给定的阿拉伯数字
* @return 中文大写钱款
*/
public static String toRMB(String input) {

if (input == null || input.equals("")) {
return "零";
}

// 判断输入是否为数字(方法不贴了,先注释了)
//if (!isNumber(input)) {
// throw new IllegalArgumentException(
// "The money input must be a number!");
//}

// 判断是否含有小数点(将给定字符串四舍五入。方法不贴了,先注释了)
//input = format(round(toDouble(input).doubleValue()));
int dotIndex = input.indexOf(".");
if (dotIndex > 0) {
if (dotIndex == input.length() - 2) {
input = input + "0";
}
else if (dotIndex == input.length() - 1) {
input = input + "00";
}
}
else {
input = input + ".00";
}

// 最多位数:15位数字
if (dotIndex > 14) {
throw new IllegalArgumentException(
"The money input is too large to convert!");
}

final String[] numbers = new String[] {"零", "壹", "贰", "叁", "肆", "伍",
"陆", "柒", "捌", "玖"};
final String[] units = new String[] {"分", "角", "元", "拾", "佰", "仟", "万",
"拾", "佰", "仟", "亿", "拾", "佰", "仟", "万"};
final String[] invalids = new String[] {"拾零元", "零[仟佰拾元角分]", "零+", "零亿",
"零万", "零元", "亿零*+万"};
final String[] valids = new String[] {"拾元", "零", "零", "亿", "万", "元",
"亿"};

// 反转给定数字
StringBuffer inputReversed = new StringBuffer(input).reverse();
inputReversed.deleteCharAt(inputReversed.indexOf("."));

// 按位转化并添加单位
StringBuffer resultBuffer = new StringBuffer();
int numberLength = inputReversed.length();
for (int index = 0; index < numberLength; index++) {
resultBuffer.append(units[index]);
resultBuffer.append(numbers[Integer.parseInt(inputReversed.charAt(index)
+ "")]);
}

// 替换非法表达方式
String result = resultBuffer.reverse().toString();
for (int i = 0; i < invalids.length; i++) {
result = result.replaceAll(invalids[i], valids[i]);
}

// 如果以零开头,则去掉零
if (result.startsWith("零")) {
result = result.substring(1, result.length());
}
// 如果以零结尾,则去掉零
if (result.endsWith("零")) {
result = result.substring(0, result.length() - 1);
}
// 如果没有角分,则添加整字
if (result.indexOf("角") < 0 && result.indexOf("分") < 0) {
result = result + "整";
}

return result;
}

public static void main(String[] args) {
String money = "100000000010.12645";
System.out.println(RMB.toRMB(money));

String moneyString = "10.01";
System.out.println(RMB.toRMB(moneyString));
}
}
加载更多回复(73)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧