说明:
1、输入文件格式为input.txt,输出文件格式为output.txt;
2、数字为正整数,长度不超过十位,不考虑小数,转化结果为英文小写 ;
3、输出格式为100=one hundred (参考out.txt );
4、非法数据请返回“error”,不存在空行;
5、关键词提示:billion ,million, thousand , hundred 。
ps:
input.txt的文本信息为:
0
01
10
11
99
100
101
199
999
1001
9999
10000
100000
100001
1000000
10000000
100000001
1020300401
102a300401
是个体
output文件
0=zero
01=one
10=ten
11=eleven
99=ninety nine
100=one hundred
101=one hundred and one
199=one hundred and ninety nine
999=nine hundred and ninety nine
1001=one thousand one
9999=nine thousand nine hundred and ninety nine
10000=ten thousand
100000=one hundred thousand
100001=one hundred thousand one
1000000=one million
10000000=ten million
100000001=one hundred million one
1020300401=one billion twenty million three hundred thousand four hundred and one
102a300401=error
是个体=error
{
string readFilePath = AppDomain.CurrentDomain.BaseDirectory + "input.txt";
List<string> list = ReadFile(readFilePath);
//写文件,将每一行写入文件中
string writeFilePath = AppDomain.CurrentDomain.BaseDirectory + "output.txt";
FileStream fs = new FileStream(writeFilePath, FileMode.Create, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("gb2312"));
int number;
for (int i = 0; i < list.Count; i++)
{
string output = "";
if (int.TryParse(list[i], out number) == false)
{
output = "error";
}
else
{
output = NumberToEnglishString(number);
}
sw.WriteLine("{0}={1}", list[i], output);
}
sw.Close();
fs.Close();
//程序结束后在debug文件夹下会生成output.txt
Console.ReadLine();
}
/// <summary>
/// 读取文件 将每一行添加到集合中
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static List<string> ReadFile(string path)
{
List<string> list = new List<string>();
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding("gb2312"));
string s = sr.ReadLine();
while (s != null)
{
if (s.Length > 0)
{
list.Add(s);
}
s = sr.ReadLine();
}
sr.Close();
fs.Close();
return list;
}
/// <summary>
/// 数字转化为英文字符串 递归算法即可
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
static string NumberToEnglishString(int number)
{
if (number < 0) //暂不考虑负数
{
return "";
}
if (number < 20) //0到19
{
switch (number)
{
case 0:
return "zero";
case 1:
return "one";
case 2:
return "two";
case 3:
return "three";
case 4:
return "four";
case 5:
return "five";
case 6:
return "sex";
case 7:
return "seven";
case 8:
return "eight";
case 9:
return "nine";
case 10:
return "ten";
case 11:
return "eleven";
case 12:
return "twelve";
case 13:
return "thirteen";
case 14:
return "fourteen";
case 15:
return "fifteen";
case 16:
return "sixteen";
case 17:
return "seventeen";
case 18:
return "eighteen";
case 19:
return "nineteen";
default:
return "";
}
}
if (number < 100) //20到99
{
if (number % 10 == 0) //20,30,40,...90的输出
{
switch (number)
{
case 20:
return "twenty";
case 30:
return "thirty";
case 40:
return "forty";
case 50:
return "fifty";
case 60:
return "sixty";
case 70:
return "seventy";
case 80:
return "eighty";
case 90:
return "ninety";
default:
return "";
}
}
else //21.22,....99 思路:26=20+6
{
return string.Format("{0} {1}", NumberToEnglishString(10 * (number / 10)),
NumberToEnglishString(number % 10));
}
}
if (number < 1000) //100到999 百级
{
if (number % 100 == 0)
{
return string.Format("{0} hundred", NumberToEnglishString(number / 100));
}
else
{
return string.Format("{0} hundred and {1}", NumberToEnglishString(number / 100),
NumberToEnglishString(number % 100));
}
}
if (number < 1000000) //1000到999999 千级
{
if (number % 1000 == 0)
{
return string.Format("{0} thousand", NumberToEnglishString(number / 1000));
}
else
{
return string.Format("{0} thousand and {1}", NumberToEnglishString(number / 1000),
NumberToEnglishString(number % 1000));
}
}
if (number < 1000000000) //1000 000到999 999 999 百万级
{
if (number % 1000 == 0)
{
return string.Format("{0} million", NumberToEnglishString(number / 1000000));
}
else
{
return string.Format("{0} million and {1}", NumberToEnglishString(number / 1000000),
NumberToEnglishString(number % 1000000));
}
}
if (number <= int.MaxValue) //十亿 级
{
if (number % 1000000000 == 0)
{
return string.Format("{0} billion", NumberToEnglishString(number / 1000000000));
}
else
{
return string.Format("{0} billion and {1}", NumberToEnglishString(number / 1000000000),
NumberToEnglishString(number % 1000000000));
}
}
return "";
}
害得被老板狠批了一顿!!!case 6: return "sex";