今天学生问了一个问题就是输入数字转化为大写,用vs2010写了个简单的,用到递归算法,每四位截取再转化,初步实现,大家交流.
using System;
using System.Collections.Generic;
using System.Text;
namespace Capp
{
class Program
{
const string numstr = "零壹贰叁肆伍陆柒捌玖";
const string numstr1 = " 拾佰千";
const string numstr2 = " 万亿";
static string sout;
static int ln;
static void Main(string[] args)
{
string num = Console.ReadLine();
long l = long.Parse(num);
sout = "";
s1(l);
ln = 0;
sout = sout.Replace(" ", "");
sout = sout.Replace("零佰", "零").Replace("零千", "零").Replace("零拾", "零");
while (sout.IndexOf("零零") >= 0)
{
sout = sout.Replace("零零", "零");
}
sout += "整";
sout = sout.Replace("零整", "整").Replace("零万", "万");
Console.WriteLine(sout);
}
static void s1(long x){
long n = x % 10000;
string s = "" + n;
string stmp="";
for (int i =0 ; i<s.Length; i++)
{
int m = int.Parse("" + s[i]);
stmp+= ""+numstr[m] + numstr1[s.Length-i];
}
stmp += numstr2[ln];
sout = stmp + sout;
ln++;
if (ln == 3)
ln = 0;
long l = x / 10000;
if (l > 0)
s1(l);
}
}
}