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

Posted 阿豪boy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#使用栈进行十以内的进制转换相关的知识,希望对你有一定的参考价值。

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#使用栈进行十以内的进制转换的主要内容,如果未能解决你的问题,请参考以下文章