1. 程式人生 > >X進位制轉換為Y進位制(X,Y為2到64間的任意數)

X進位制轉換為Y進位制(X,Y為2到64間的任意數)

思路:

1.X進位制轉換為10進位制

2.10進位制轉化為Y進位制

import java.util.Scanner;
/**
 * X進位制到Y進位制轉換(範圍在2到64之間)
 * 思路:1.X進位制轉換為10進位制
 *       2.10進位制轉換為Y進位制
 */
public class XtoY {
    //編碼
    private  char[] chs = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B',
            'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
            'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '+', '/' };

    public  String convert(int input_mod,String input_value,int output_mod ){
        return TentoY(XtoTen(input_mod,input_value),output_mod);
    }

    //X進位制轉化成10進位制
    public  int XtoTen(int input_mod,String input_value){
        if(input_value==" "||input_mod<2||input_mod>64) {
            System.out.println("輸入值無效!");
            return -1;
        }
        int mul_factor=1;
        int res_ten=0;
        char ch;

        int len=input_value.length();
        for(int i=0;i<len;i++){
            ch=input_value.charAt(len-1-i);
            res_ten +=getIndexOfChar(ch)* mul_factor;
            mul_factor = mul_factor * input_mod;
        }
        return res_ten;
    }

    //如16進制中a為10
    public int getIndexOfChar(char ch){
        if(ch == ' ') {
            return -1;
        }
        for (int i = 0; i < chs.length-1; i++) {
            if(ch == chs[i]) {
                return i;
            }
        }
        return -1;
    }

    //10進位制轉化為Y進位制
    public  String TentoY(int ten_value,int output_mod){
        if(output_mod<2||output_mod>64||ten_value<0)
            return null;
        if(output_mod==10){
            return ten_value+"";
        }
        String out="";
        int num_remain = ten_value;
        int index = 0;
        while(num_remain >= 1)    {
            index = (int)num_remain % output_mod;
            out=chs[index]+out;
            num_remain = num_remain / output_mod;
        }
        return out;
    }
    public static void  main(String[] args){
        System.out.println("請輸入資料的進位制X(1<X<65):");
        Scanner sc_x = new Scanner(System.in);
        int x= getIntOfScanner(sc_x);
        System.out.println("請輸入X進位制的資料:");
        Scanner sc_x_val = new Scanner(System.in);
        String x_val = sc_x_val.next();
        System.out.println("請輸入輸出資料的進位制Y(1<Y<65):");
        Scanner sc_y = new Scanner(System.in);
        int y= getIntOfScanner(sc_y);
        System.out.println(x+"進位制的資料"+x_val+"轉換為"+y+"進位制的值的結果為:");
        XtoY x_y_1 = new XtoY();
        System.out.println(x_y_1.convert(x,x_val,y));
    }

    //判斷輸入是否數字
    public static int  getIntOfScanner(Scanner sc ){
        while (true) {
            try {
                String str = sc.next();
                int k = Integer.valueOf(str);
                //判斷輸入的數字範圍
                if (k<2||k>64){
                    System.out.println("ERROR!");
                    System.out.println("請重新輸入:");
                }else {
                    return k;
                }
            } catch (Exception e) {
                System.out.println("ERROR!");
                System.out.println("請重新輸入:");
            }
        }
    }

}