poj1001—java

  1 package com.calutate;
  2 
  3 import java.util.*;
  4 
  5 public class Precision 
  6 {
  7     int max=100000;
  8 
  9     
 10    public void Calculate() 
 11    {
 12         int base=0;
 13         int len=0;
 14         int doclen=0;//真实小数位数
 15         int integerlen=0;//整数位数
 16         int totaldoclen=0;//计算结果小数位数
 17         int totallen=0;//计算结果总位数
 18         int ns=0;//n次幂
 19         int set=0;
 20     
 21     String inputs=new String();
 22     Scanner cin;
 23     Scanner cinn;
 24     
 25     do
 26     {
 27         base=0;
 28         int[] result=new int[max];
 29         System.out.println("输入数据:");
 30         cin=new Scanner(System.in);
 31         inputs=cin.nextLine();
 32         System.out.println("输入n:");
 33         cinn=new Scanner(System.in);
 34         ns=cinn.nextInt();
 35         
 36         char[] cs=inputs.toCharArray();
 37         len=this.realLength(cs);
 38         for (int i = 0; i < len; i++) 
 39         {
 40            
 41            if(cs[i]!='.')
 42            {
 43                int sxs=Integer.parseInt(String.valueOf(cs[i]));
 44                base=base*10+sxs;
 45            }else 
 46            {
 47                integerlen=this.Intlength(base);
 48            }
 49             
 50         }
 51         totallen=this.Intlength(base);
 52         doclen=totallen-integerlen;
 53         
 54         
 55            totaldoclen=doclen*ns;
 56            int based=base;
 57            for (int i = 0; i < len; i++) 
 58            {
 59                result[i]=based%10;
 60                based=based/10;    
 61         }
 62         for (int i = 0; i < ns-1; i++) 
 63         {
 64             for (int j = 0; j <max; j++) 
 65             {    
 66                 result[j]=result[j]*base+set;
 67                 set=result[j]/10;
 68                 result[j]=result[j]%10;
 69             }
 70         }
 71         
 72         totallen=this.realLength(result);
 73         if (integerlen>0)
 74         {
 75             for (int i = totallen-1; i >=totaldoclen; i--)
 76             {
 77                 System.out.print(result[i]);
 78             }
 79             if(totaldoclen>0)
 80             {
 81             System.out.print(".");
 82             for (int i = totaldoclen-1; i >=0; i--) 
 83             {
 84                 System.out.print(result[i]);
 85             }
 86             }
 87         }
 88         else 
 89         {
 90             System.out.print(".");
 91             for (int i = totaldoclen-1; i >=0; i--)
 92             {
 93                 System.out.print(result[i]);
 94             }
 95         }
 96         System.out.println();
 97        // cin.close();
 98        // cinn.close();
 99     }while(inputs!="0");
100     
101      cin.close();
102      cinn.close();
103    }
104  
105    /*
106     计算有效数值长度
107     * */
108    private int realLength(int[] a)
109    {
110        int relLength=0;
111        for (int i = a.length-1; i >= 0; i--) 
112         {
113             if(a[i]!=0)
114             {
115                 relLength=i+1;
116                 break;
117             }
118         }
119        return relLength;
120        
121    }
122    
123    private int realLength(char[] a)
124    {
125        int relLength=0;
126        for (int i = a.length-1; i >= 0; i--) 
127         {
128             if(a[i]!='0')
129             {
130                 relLength=i+1;
131                 break;
132             }
133         }
134        return relLength;
135        
136    }
137    
138    /*
139     计算整数长度
140    */
141    private int Intlength(int a) 
142    {
143        int leng=0;
144        while (a>0) 
145        {
146            a=a/10;
147            leng++;
148        }
149        return leng;
150     
151    }
152 }

 

posted @ 2013-11-09 15:23  C键盘。夜。  阅读(181)  评论(0编辑  收藏  举报