004_栈事例2(进制转化)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CStack
{
    class example_02
    {
        static void Main() {

            int num, baseNum;
            Console.WriteLine("输入一个数字");
            num = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("输入一个base");
            baseNum = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(num+"Convert to:");
            MulBase(num, baseNum);
            Console.WriteLine("base"+baseNum);

            Console.ReadLine();
        }

        /// 
        /// 进制转换
        /// 
        /// 给定的数字
        /// 进制
        static void MulBase(int n,int b) {
            Stack Digits = new Stack();

            do
            {
                Digits.Push(n%b);//获取余数
                n /= b;//获取商
            } while (n!= 0);

            //打印进制变换后的数字
            while (Digits.Count>0)
            {
                Console.Write(Digits.Pop());
            }
        }
    }
}

你可能感兴趣的:(004_栈事例2(进制转化))