前些天朋友公司的考试题,他们跟我说了,我没事做了做。简单的实现了一下。里面有的英文单词不对,不想改了,呵呵,反正就是这个意思。代码写的有点冗余,但功能还是实现了。不过只做了四位数的。
namespace CountNum
{
class Program
{
static void Main(string[] args)
{
Dictionary<int, string> dicGeEnglishGeWei = GetGeWei();
Dictionary<int, string> dicGetEnglishShiWei = GetShiWei();
Dictionary<int, string> dicGetDuble = GetDubleRead();
//读取数字文本
string[] textNums = File.ReadAllLines("1.txt", Encoding.UTF8);
string[] saveTestEnglish = new string[textNums.Length];
for (int i = 0; i < textNums.Length; i++)
{
Match match = Regex.Match(textNums[i], @"\d[1-9]");
//int number = int.Parse(textNums[i]);
string number = textNums[i];
if (match.Success)
{
switch (number.Length)
{
case 1:
//个位数的英文读入
int ge = int.Parse(number);
saveTestEnglish[i] = dicGeEnglishGeWei[ge];
break;
case 2:
if (number[0] == '1')
{
int shi = int.Parse(number);
saveTestEnglish[i] = dicGetDuble[shi];
}
else
{
saveTestEnglish[i] = dicGetEnglishShiWei[SubS(number)] + dicGeEnglishGeWei[SubS((int.Parse(number) - 1).ToString())];
}
break;
case 3:
if (number[1] == '1')
{
saveTestEnglish[i] = dicGeEnglishGeWei[SubS(number)] + " hunred and" + dicGetDuble[int.Parse(number.Substring(1))];
}
else
{
saveTestEnglish[i] = dicGeEnglishGeWei[SubS(number)] + " hunred and" + dicGetEnglishShiWei[SubS(number.Substring(1))] + dicGeEnglishGeWei[SubS(number.Substring(2))];
}
break;
case 4:
if (number[2] == '1')
{
saveTestEnglish[i] = dicGeEnglishGeWei[SubS(number)] + " thousand " + dicGeEnglishGeWei[SubS(number.Substring(1))] + " hunred and" + dicGetDuble[int.Parse(number.Substring(2))];
}
else
{
saveTestEnglish[i] = dicGeEnglishGeWei[SubS(number)] + " thousand " + dicGeEnglishGeWei[SubS(number.Substring(1))] + " hunred and" + dicGetEnglishShiWei[SubS(number.Substring(2))] + dicGeEnglishGeWei[SubS(number.Substring(3))];
}
break;
default:
break;
}
}
else
{
saveTestEnglish[i] = "Error";
}
}
for (int j = 0; j < saveTestEnglish.Length; j++)
{
Console.WriteLine(saveTestEnglish[j]);
}
Console.Read();
}
/// <summary>
/// 截取位数上的数
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
static int SubS(string num)
{
int danwei = 0;
danwei = int.Parse(num.Substring(0, 1));
return danwei;
}
/// <summary>
///返回个位数字对应的英文
/// </summary>
/// <returns></returns>
static Dictionary<int, string> GetGeWei()
{
#region 个位数字典
Dictionary<int, string> dicSingle = new Dictionary<int, string>();
dicSingle.Add(1, " one");
dicSingle.Add(2, " two");
dicSingle.Add(3, " three");
dicSingle.Add(4, " four");
dicSingle.Add(5, " five");
dicSingle.Add(6, " six");
dicSingle.Add(7, " seven");
dicSingle.Add(8, " eight");
dicSingle.Add(9, " ine");
dicSingle.Add(0, "");
#endregion
return dicSingle;
}
static Dictionary<int, string> GetShiWei()
{
#region 个位数字典
Dictionary<int, string> dicSingle = new Dictionary<int, string>();
dicSingle.Add(1, " ten");
dicSingle.Add(2, " twenty");
dicSingle.Add(3, " thirty");
dicSingle.Add(4, " forty");
dicSingle.Add(5, " fifty");
dicSingle.Add(6, " sixty");
dicSingle.Add(7, " seventy");
dicSingle.Add(8, " eighty");
dicSingle.Add(9, " ninty");
dicSingle.Add(0, "");
#endregion
return dicSingle;
}
static Dictionary<int, string> GetDubleRead()
{
#region 个位数字典
Dictionary<int, string> dicSingle = new Dictionary<int, string>();
dicSingle.Add(10, " ten");
dicSingle.Add(11, " eleven");
dicSingle.Add(13, " thirsteen");
dicSingle.Add(14, " fourteen");
dicSingle.Add(15, " fifteen");
dicSingle.Add(16, " sixteen");
dicSingle.Add(17, " seventeen");
dicSingle.Add(18, " eighteen");
dicSingle.Add(19, " nineteen");
dicSingle.Add(12, " twelve");
#endregion
return dicSingle;
}
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。