您的位置:首页 > 其它

【小朽开源路-①初出茅庐】十进制转(2到16)进制原理及安卓实现

2014-03-15 21:40 295 查看

前言

  我的开源路声明:[传送门]

  为了实现自己的目标,不幸自己就选择在博客园上练习自己英语。希望英语多,中文少,然后渐渐地成为习惯。加油!

Subject

  实现十进制转(2到16)进制原理及安卓

The Principle (原理)

  ①the Stack ADT (栈知识)

    小朽是活学活用,栈知识复习[传送门]

  ②the Short division (短除法)

    

  Start...

              

the jar struckture

  

  ALStack.java

package sedion.jeffli.action;
import java.util.ArrayList;import java.util.Stack;

/**
* My Open-Source road
* @author Jeff Li
*
* @param <T>
*/
public class ALStack<T> extends Stack<T> {

/**
*
*/
private static final long serialVersionUID = -1261738006146239371L;
private ArrayList<T> stackList = null;

//create an empty stack by creating an empty ArrayList
public ALStack(){
stackList = new ArrayList<T>();
}

}


  

  BaseStr.java

      

package sedion.jeffli.action;

import java.util.Scanner;

public class BaseStr {
public static void main(String[] args) {
int num , b;
Scanner keyIn = new Scanner(System.in);

System.out.print("Enter a decimal number: ");
num = keyIn.nextInt();

System.out.print("Enter a base (2 to 16): ");
b = keyIn.nextInt();
System.out.print(" "+num+" base "+b+" is "+baseString(num, b));
}

//the short division principle
public static String baseString(int num,int b){
//digitChar.charAt(digit) is the character that represents the digit , 0 <= digit <= 15
String digitChar = "0123456789ABCDEF",numStr = "";

//stack holds the base-b digits of num
ALStack<Character> stk = new ALStack<Character>();

do{
//push right-most digit on the stack
stk.push(digitChar.charAt(num % b));

//remove right-most digit form num
num /= b;

}while(num != 0);

while(!stk.isEmpty()){

//pop stack and add digit on top of stack to numStr
numStr += stk.pop().charValue();
}

return numStr;
}
}


    *核心

      短除法原理,短除就是在除法中写除数的地方写两个数共有的质因数,然后落下两个数被公有质因数整除的商,之后再除,以此类推,直到结果互质为止

                


    实现demo,its a bit easy ,right?

      


       //push right-most digit on the stack
stk.push(digitChar.charAt(num % b));

//remove right-most digit form num
num /= b;


Andriod Practice 

   源码下载:链接:http://pan.baidu.com/share/link?shareid=1403222381&uk=3307409781 密码:ub64

   应用下载:链接:http://pan.baidu.com/share/link?shareid=1403222381&uk=3307409781 密码:ub64

   勿喷,效果图

    


寄读者,寄知识来源

   读者,你好!你我不相识,谢谢你们支持。我的梦想会越来越接近。keep on,共勉!

   知识来源 http://www.cnblogs.com/shitianzeng/articles/2336765.html

    知识来源于想着,我走路想着玩一个。就玩好了,星期六快乐。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: