CSDN博客

img shaokun305

数据结构(二)--栈的应用(数制转换器)

发表于2004/7/17 16:42:00  2028人阅读

/**

 * <p>Title: 栈的应用----可以进行所有数值之间的转化类</p>
 
 * <p>Description: </p>
 
 * <p>Copyright: Copyright (c) 2004</p>
 
 * <p>Company: </p>
 
 * @author shaokun305
 
 * @version 1.0
 
 */

     public class test1 {       

  public static String  ChangeNumeric(String aastr,int n,int m){
 
    //转化成10进制

    int aa=0;
 
    String result="";
 
    Stack stack=new Stack();
 
    String mmx=aastr.toUpperCase();
 
    int endint=0;
 
    for(int i=0;i<mmx.length();i++){
 
      int mid=1;
 
      for(int j=0;j<(mmx.length()-1-i);j++)
 
        mid*=n;

      //System.out.println("mid="+mid);

      // System.out.println("mmx.charAt(i)="+mmx.charAt(i));
 
      int jint=0;
 
                 

       switch(mmx.charAt(i)){

         case 'A':
 
           jint=10;

           break;

         case 'B':
 
           jint=11;

           break;

         case 'C':
 
           jint=12;

           break;

         case 'D':
 
           jint=13;

           break;

         case 'E':
 
           jint=14;

           break;

         case 'F':
 
           jint=15;

           break;

         default:

           jint=Integer.parseInt(""+mmx.charAt(i));

       }

       endint=endint+jint*mid;

       //System.out.println("endint="+endint);

     }

     aa=endint;

                 

     //aa中现在保存的是10进制的数值

     //循环取模求余,进行压栈.

                 

     while(aa!=0){

       stack.push(new Integer(aa%m));
 
       aa/=m;

     }

     //弹栈转化获得的整数值,

     while(!stack.isEmpty()){

       int j=((Integer)stack.pop()).intValue();
 
       String jstr="";
 
       switch(j){

         case 10:
 
           jstr="A";

           break;

         case 11:
 
           jstr="B";

           break;

         case 12:
 
           jstr="C";

           break;

         case 13:
 
           jstr="D";

           break;

         case 14:
 
           jstr="E";

           break;

         case 15:
 
           jstr="F";

           break;

         default:

           jstr=""+j;          

       }

       result+=jstr;

     }

   //输出转化结果

     return result;
 
   //System.out.println("result="+result);

    //System.out.println(new java.util.Date());
 
  }
             

  public test1() {
 
  }

  public static void main(String[] args) {
 
    System.out.println(new java.util.Date());
 
    test1 testx = new test1();
 
    String result="";
 
    Stack stack=new Stack();
 
    if(args.length>=3){

      int aa=0;//Integer.parseInt(args[0]);
 
      int n=Integer.parseInt(args[1]);
 
      int m=Integer.parseInt(args[2]);
 
      /*/转化成10进制

      String mmx=args[0].toUpperCase();
 
      int endint=0;
 
      for(int i=0;i<mmx.length();i++){
 
        int mid=1;
 
        for(int j=0;j<(mmx.length()-1-i);j++)
 
          mid*=n;

        //System.out.println("mid="+mid);

        // System.out.println("mmx.charAt(i)="+mmx.charAt(i));
 
        int jint=0;
 
        switch(mmx.charAt(i)){

          case 'A':
 
            jint=10;

            break;

          case 'B':
 
            jint=11;

            break;

          case 'C':
 
            jint=12;

            break;

          case 'D':
 
            jint=13;

            break;

          case 'E':
 
            jint=14;

            break;

          case 'F':
 
            jint=15;

            break;

          default:

            jint=Integer.parseInt(""+mmx.charAt(i));

        }

        endint=endint+jint*mid;

        //System.out.println("endint="+endint);

      }

      aa=endint;

                 

      //aa中现在保存的是10进制的数值

      //循环取模求余,进行压栈.

                 

      while(aa!=0){

        stack.push(new Integer(aa%m));
 
        aa/=m;

      }

      //弹栈转化获得的整数值,

      while(!stack.isEmpty()){

        int j=((Integer)stack.pop()).intValue();
 
        String jstr="";
 
        switch(j){

          case 10:
 
            jstr="A";

            break;

          case 11:
 
            jstr="B";

            break;

          case 12:
 
            jstr="C";

            break;

          case 13:
 
            jstr="D";

            break;

          case 14:
 
            jstr="E";

            break;

          case 15:
 
            jstr="F";

            break;

          default:

            jstr=""+j;
                 
        }

        result+=jstr;

      }*/

      result=test1.ChangeNumeric(args[0],n,m);                

    }

    //输出转化结果

    System.out.println("result="+result);

    System.out.println(new java.util.Date());
 
  }
 
}

#你编译好这个test1类,如果使用jdk,可以用javac test1.java
然后再:java test1 210145 10 16 回车
输出:
Tue Apr 20 11:50:43 CST 2004 //开始时间
result=334E1
Tue Apr 20 11:50:43 CST 2004 //结束时间
含义:把十进制的210145 转化成16进制数是:334E1
再试验:java test1 4d7a8f 16 2
输出:
Tue Apr 20 11:54:44 CST 2004
result=10011010111101010001111
Tue Apr 20 11:54:44 CST 2004
含义:把16进制的4d7a8f转换成2进制的是10011010111101010001111

#具体的利用栈的转换算法说明: 
例如 (1348)10=(2504)8,其运算过程如下  
           n         n div 8     n mod 8
          1348        168           4
          168         21            0
          21          2             5
          2           0             2

 

0 0

相关博文

我的热门文章

img
取 消
img