您好,登錄后才能下訂單哦!
前些天朋友公司的考試題,他們跟我說了,我沒事做了做。簡單的實現了一下。里面有的英文單詞不對,不想改了,呵呵,反正就是這個意思。代碼寫的有點冗余,但功能還是實現了。不過只做了四位數的。
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。