上一篇我们使用递归的方式将数字自动翻译(转化为中文)
这次我们使用循环迭代的方式。
思路(除基取余迭代法):
以数字4567为例,首先除以1000,获取商为4,以及余数567,拼接 四千,
此时处理余数567,除以100,获取商为5,余数为67,拼接 五百,
此时处理余数67,除以10,获取商为6,余数为7,拼接 六十,
此时处理余数7,直接加上个位 七即可。
新建关键处理类TranslateUseLoopUtil.cs,
类TranslateUseLoopUtil源程序如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberToChineseDemo
{
/// <summary>
/// 使用循环【迭代】来进行转化
/// </summary>
public class TranslateUseLoopUtil
{
/// <summary>
/// 【个位的十个数字】中文字符描述集合
/// </summary>
static readonly char[] ChineseCollection = new char[] { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九' };
/// <summary>
/// 数字【倍率】与中文字符的映射字典
/// </summary>
static readonly Dictionary<int, char> DictMultiple = new Dictionary<int, char>() {
{ 10, '十' }, { 100, '百' }, { 1000, '千' }, { 10000, '万' }, { 100000000, '亿' } };
/// <summary>
/// 数字转中文
/// 考虑负数 和 正数
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static string NumberToChineseEx(int number)
{
if (number < 0)
{
return "负" + NumberToChinese(number);
}
return NumberToChinese(number);
}
/// <summary>
/// 数字转中文,使用【循环迭代】的方式
/// 按照【个、十、百、千、万、亿】
/// 只考虑正整数和零
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static string NumberToChinese(int number)
{
string chinese = string.Empty;
//商和余数
int quotient = 0;//商
int remainder = 0;//余数
number = Math.Abs(number);
if (number < 10)
{
//0~9 注意这个倍率multiple一定要小于10
return GetChinese(1, number);
}
if (number < 100)
{
//10~99之间
return GetChinese(10, number);
}
if (number < 1000)
{
//100~999
return GetChinese(100, number);
}
if (number < 10000)
{
//1000~9999
return GetChinese(1000, number);
}
if (number < 100000000)
{
//10000~9999 9999,以万为轴,分割为两部分
quotient = number / 10000;
remainder = number % 10000;
if (remainder == 0)
{
//比如580000
return NumberToChinese(quotient) + DictMultiple[10000];
}
string handleRemainder = "";
if (remainder > 0 && remainder < 1000)
{
handleRemainder = ChineseCollection[0].ToString();//加上零 如 502
}
return NumberToChinese(quotient) + DictMultiple[10000] + handleRemainder + NumberToChinese(remainder);
}
else
{
//一亿或以上,以亿为轴,分割为两部分
quotient = number / 100000000;
remainder = number % 100000000;
if (remainder == 0)
{
//比如20亿
return NumberToChinese(quotient) + DictMultiple[100000000];
}
string handleRemainder = "";
if (remainder > 0 && remainder < 1000)
{
handleRemainder = ChineseCollection[0].ToString();//加上零 如 502
}
return NumberToChinese(quotient) + DictMultiple[100000000] + handleRemainder + NumberToChinese(remainder);
}
}
/// <summary>
/// 对【0000~9999】之间的数字进行迭代解析成中文
/// 类似于除基取余法
/// </summary>
/// <param name="multiple">倍率</param>
/// <param name="number">需要解析的数字</param>
/// <returns></returns>
private static string GetChinese(int multiple, int number)
{
if (number == 0)
{
return ChineseCollection[0].ToString();
}
string chinese = string.Empty;
int quotient = 0;//商
int remainder = number;//余数
bool existZero = false;//是否已存在“零”
while (remainder > 0 && multiple >= 10)
{
quotient = remainder / multiple;
remainder = remainder % multiple;
if (quotient == 0)
{
if (!existZero)
{
existZero = true;//处理3008 转化为 三千零零八 多了个零 的问题
chinese += ChineseCollection[quotient].ToString();
}
}
else
{
chinese += ChineseCollection[quotient].ToString() + DictMultiple[multiple];
}
//处理后 依次是 千、百、十
multiple = multiple / 10;
}
if (remainder > 0) //处理个数
{
chinese += ChineseCollection[remainder].ToString();
}
return chinese;
}
}
}
FormNumberToChinese窗体增加下拉框cboMode,整体FormNumberToChinese设计器如下:
FormNumberToChinese测试程序如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NumberToChineseDemo
{
public partial class FormNumberToChinese : Form
{
public FormNumberToChinese()
{
InitializeComponent();
cboMode.Items.AddRange(new object[] { "递归法", "循环迭代法" });
cboMode.SelectedIndex = 0;
}
private void btnNumberToChinese_Click(object sender, EventArgs e)
{
txbDestChinese.Clear();
if (txbSrcNumber.Text.Trim().Length == 0)
{
txbSrcNumber.Focus();
MessageBox.Show($"请输入数字", "出错");
return;
}
int number;
if (!int.TryParse(txbSrcNumber.Text, out number))
{
txbSrcNumber.Focus();
MessageBox.Show($"请输入整数", "出错");
return;
}
try
{
if (cboMode.SelectedIndex == 0)
{
txbDestChinese.Text = NumberChineseTranslateUtil.NumberToChineseEx(number);
}
else
{
txbDestChinese.Text = TranslateUseLoopUtil.NumberToChineseEx(number);
}
}
catch (Exception ex)
{
MessageBox.Show($"出现异常:【{ex.Message}】", "转化出错");
}
}
private void btnChineseToNumber_Click(object sender, EventArgs e)
{
txbDestNumber.Clear();
string chinese = txbSrcChinese.Text.Trim();
if (chinese.Length == 0)
{
txbSrcChinese.Focus();
MessageBox.Show($"请输入中文", "出错");
return;
}
try
{
txbDestNumber.Text = NumberChineseTranslateUtil.ChineseToNumberEx(chinese).ToString();
}
catch (Exception ex)
{
MessageBox.Show($"出现异常:【{ex.Message}】", "转化出错");
}
}
}
}
程序运行如图(切换为【循环迭代法】):