首页 > Windows开发 > 详细

C#使用栈进行十以内的进制转换

时间:2016-10-15 22:39:26      阅读:216      评论:0      收藏:0      [点我收藏+]
static void Main(string[] args) {
            int n,m;
            while (true) {
                Console.WriteLine("请输入要转换的数和进制,用英文空格分开:");
                string s = Console.ReadLine();
                string[] str = s.Split( );
                n = Convert.ToInt32( str[0] );
                m = Convert.ToInt32( str[1] );
                Console.WriteLine(n+"的m进制为:"+test3(n,m));
}
public static string test3(int num, int fomat) {

            Stack s = new Stack();
            while (num != 0) {
                int t = num % fomat;
                s.Push(t);
                num = (num-t)/fomat;
            }
            int n = s.Count;
            string res = "";
            for (int i = 0; i < n; i++) {
                res += s.Pop();
            }
            return res;
}

技术分享

C#使用栈进行十以内的进制转换

原文:http://www.cnblogs.com/ahaoboy/p/5965298.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!