笔算开n次方

来源:互联网 发布:php 防xss 编辑:程序博客网 时间:2024/05/16 18:56

    在J2ME中,有时会用到开方运算,但是MIDP1.0中没有提供该功能。这里使用笔算开方的方式,实现了开任意数的n次方。这里使用了long作为运算的临时变量类型,在数值较大或者保留位数太多的时候,会出现因为long数值溢出而导致的错误。

  1. public class Extract {
  2.     /**
  3.      * 开n次方
  4.      * 
  5.      * @param root
  6.      *            被开方数
  7.      * @param n
  8.      *            开方,开n次方
  9.      * @param baoliu
  10.      *            小数部分保留的位数
  11.      * @return
  12.      */
  13.     public static String extract(String root, int n, int baoliu) {
  14.         String[] zhengShu = null;
  15.         String[] xiaoShu = null;
  16.         int pointIndex = root.indexOf('.');
  17.         if (pointIndex == -1) {
  18.             zhengShu = getZhengShu(root, n);
  19.             xiaoShu = getXiaoshu("0", n, baoliu);
  20.         } else {
  21.             zhengShu = getZhengShu(root.substring(0, pointIndex), n);
  22.             xiaoShu = getXiaoshu(root.substring(pointIndex + 1), n, baoliu);
  23.         }
  24.         String result = "";
  25.         long a = 0;// 初值
  26.         long c = 0;// 差
  27.         long b = 0;// 尝试的商值
  28.         int index = 0;
  29.         while (index < zhengShu.length) {
  30.             c = c * power(10, n) + Long.parseLong(zhengShu[index]);
  31.             b = 1;
  32.             while (power(10 * a + b, n) - power(10 * a, n) <= c) {
  33.                 b++;
  34.                 if (b == 10) {
  35.                     break;
  36.                 }
  37.             }
  38.             b = b - 1;
  39.             c = c - (power(10 * a + b, n) - power(10 * a, n));
  40.             a = a * 10 + b;
  41.             index++;
  42.         }
  43.         result += a + ".";// 整数部分计算完毕
  44.         index = 0;
  45.         while (index < xiaoShu.length) {
  46.             c = c * power(10, n) + Long.parseLong(xiaoShu[index]);
  47.             b = 1;
  48.             while (power(10 * a + b, n) - power(10 * a, n) <= c) {
  49.                 b++;
  50.                 if (b == 10) {
  51.                     break;
  52.                 }
  53.             }
  54.             b = b - 1;
  55.             c = c - (power(10 * a + b, n) - power(10 * a, n));
  56.             a = a * 10 + b;
  57.             index++;
  58.         }
  59.         result += (a + 5) % power(10, baoliu + 1);// 这里 a + 5 是为了实现四舍五入
  60.         result = result.substring(0, result.length() - 1);// 放弃最后1位
  61.         return result;
  62.     }
  63.     /**
  64.      * 根据开方n的数值,将整数部分划分成若干片段
  65.      * 
  66.      * @param str
  67.      * @param n
  68.      * @return
  69.      */
  70.     private static String[] getZhengShu(String str, int n) {
  71.         int length = str.length() / n;
  72.         if (str.length() % n != 0) {
  73.             length += 1;
  74.         }
  75.         String[] zhengShu = new String[length];
  76.         for (int i = zhengShu.length - 1; i > 0; i--) {
  77.             zhengShu[i] = str.substring(str.length() - n);
  78.             str = str.substring(0, str.length() - n);
  79.         }
  80.         zhengShu[0] = str;
  81.         return zhengShu;
  82.     }
  83.     /**
  84.      * 根据开方n的数值和保留的位数,将小数部分划分成若干片段
  85.      * 
  86.      * @param str
  87.      * @param n
  88.      * @param decimalDigits
  89.      * @return
  90.      */
  91.     private static String[] getXiaoshu(String str, int n, int decimalDigits) {
  92.         int length = decimalDigits + 1;
  93.         while (str.length() < length * n) {
  94.             str += "0";
  95.         }
  96.         String[] xiaoShu = new String[length];
  97.         for (int i = 0; i < xiaoShu.length; i++) {
  98.             xiaoShu[i] = str.substring(0, n);
  99.             str = str.substring(n);
  100.         }
  101.         return xiaoShu;
  102.     }
  103.     /**
  104.      * 得到一个数的n次方
  105.      * 
  106.      * @param shu
  107.      * @param n
  108.      * @return
  109.      */
  110.     private static long power(long shu, int n) {
  111.         long result = 1;
  112.         while (n > 0) {
  113.             result *= shu;
  114.             n--;
  115.         }
  116.         return result;
  117.     }
  118.     /**
  119.      * @param args
  120.      */
  121.     public static void main(String[] args) {
  122.         System.out.println("将3开2次方,保留3位小数:" + extract("3"23));
  123.         System.out.println("将3开5次方,保留3位小数:" + extract("3"53));
  124.         System.out.println("将3.08开2次方,保留3位小数:" + extract("3.08"23));
  125.     }
  126. }

输出结果是:

 

将3开2次方,保留3位小数:1.732
将3开5次方,保留3位小数:1.246
将3.08开2次方,保留3位小数:1.755

原创粉丝点击