Mục blog của 97213529 郭益銘

Hình của 97213529 郭益銘
Bởi 97213529 郭益銘 - Thứ Hai, 22 Tháng Mười Hai 2008, 2:39 SA
Bất kỳ ai trên thế giới

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConvertTest
{
    class zzz
    {
        public double to10(string value, int based)
        {
            double sum = 0.0;
            int count = 0;
            if (value.Contains("."))
                count = value.IndexOf('.') - 1;
            else
                count = value.Length - 1;
            for (int i = 0; i < value.Length; i++) //n=>10
                if (value[i] != '.')
                {
                    if (char.Parse(value[i].ToString().ToUpper()) >='A')
                        sum += (char.Parse(value[i].ToString().ToUpper())-'A'+10) * Math.Pow(based, count--);
                    else
                    sum += Int32.Parse(value[i].ToString()) * Math.Pow(based, count--);
                }
            return sum;
        }
        public void TentoN(double sum,int gg)
        {
            string[] arr = sum.ToString().Split('.');
            ArrayList dog1 = new ArrayList();
            int left = Int32.Parse(arr[0]);
            while (left != 0)
            {
                dog1.Add(left % gg);
                left /= gg;
            }
            dog1.Reverse();
            if (sum.ToString().Contains("."))
            {
                dog1.Add(".");
                double right = double.Parse("0." + arr[1].ToString());
                for (int i = 0; i < 10; i++)
                {
                    if (Math.Floor(right) == Math.Ceiling(right))
                        break;
                    right = right * gg;
                    dog1.Add(Math.Floor(right));
                    right = right - Math.Floor(right);
                }
            }
            for (int i = 0; i < dog1.Count; i++)
            {
                if (Int32.Parse(dog1[i].ToString())>9)
                    Console.Write((char)('A' + 10 - Int32.Parse(dog1[i].ToString())));
                else
                    Console.Write(dog1[i]);
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("請輸入數值:");
            string value = Console.ReadLine();
            Console.Write("請輸入基底:");
            int based = Int32.Parse(Console.ReadLine());
            zzz dog = new zzz();
            double sum = dog.to10(value,based);
            Console.WriteLine("10進位是 " + sum);//10
            //=========================TO 10==================
            Console.Write("請輸入基底:");
            int gg = Int32.Parse(Console.ReadLine());
            //int a=Convert.ToInt32(arr[0]);
            dog.TentoN(sum, gg);
            Console.ReadKey();
        }
    }
}

Thẻ từ khoá: