一个输入数字转化为大写的小程序(用到递归)

本文介绍了一种使用C#编程,通过递归算法将输入的数字转换为大写的简单方法。程序读取用户输入的数字,进行四舍五入截取并转换,最后去除多余的零,并输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

今天学生问了一个问题就是输入数字转化为大写,用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);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值