• 样条插值法(Java)--在本地执行


    该程序主要实现样条插值的目的,为本地执行java文件

    该程序包含:样条插值法、读取文件,写入文件,字符型转double型方法等;

    适合初学Java的人学习;

    首次使用eclipse打jar包,中间很曲折,结果很nice,我把步骤分享下~

    该jar包可在本地执行;

    1.将你要实现的功能写成通用的格式,最好是输入和输出都以文件的形式;

    2.程序写好后,开始打jar包,打jar包步骤

       File>export>Runable JAR file>选择你的主类和输出地址>点击完成后会有警告弹出,不用管,继续点击确定就好了;

    export 

    注意点

    警告

    3.jar包完成后,检查jar包大小,可以右击该jar包,查看jar包大小,如果是2MB左右大小,说明jar包大小没问题,如果是几KB的话,可能jar包就有问题。

    成功后的jar包大小

    4.jar包完成后,就可以测试了,可以在cmd中测试,也可以在Linux中测试。这两种方法我都试下~

    5.cmd测试:

    切换到jar包所在位置,执行jar包

    java -jar SplineInterpolatorImpl.jar C:Users91911Desktop est.txt  C:Users91911Desktop esult.txt

    解释:java -jar jar包名称 输入文件 输出结果

     

    6.在Linux中测试

     将jar包和输入文件上传到接口机,执行jar包:

    java -jar SplineInterpolatorImpl.jar test.txt result.txt

    解释:java -jar jar包名称 输入文件 输出文件

     

    完整代码如下:

     样条插值法:

      1 import java.io.BufferedReader;
      2 import java.io.File;
      3 import java.io.FileInputStream;
      4 import java.io.FileNotFoundException;
      5 import java.io.FileWriter;
      6 import java.io.IOException;
      7 import java.io.InputStreamReader;
      8 import java.io.UnsupportedEncodingException;
      9 import java.util.ArrayList;
     10 import java.util.List;
     11 
     12 import org.apache.commons.lang3.StringUtils;
     13 import org.apache.commons.math3.analysis.interpolation.SplineInterpolator;
     14 import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;
     15 
     16 
     17 /**
     18  * 样条插值法
     19  * @author 91911
     20  */
     21 public class SplineInterpolatorImpl {
     22     public static void main(String[] args){
     23 //        String[] source = new String[]{
     24 //                "0,1,2,3    0,1,1,0    2.5",
     25 //                "0,1,2,3    0,1,1,0    1.5"
     26 //        };
     27         // 判断传入参数的长度,必须输入两个参数(输入文件和输出文件),否则报错
     28         if (args.length != 2) {
     29             System.out.println("请输入原文件和输出文件的路径!!");
     30             System.exit(0);
     31         }
     32         SplineInterpolatorImpl splineInterpolatorImpl = new SplineInterpolatorImpl();
     33         List<String> source = splineInterpolatorImpl.getFileContent(args[0]);
     34         File file = new File(args[1]);
     35 //        List<String> source = splineInterpolatorImpl.getFileContent("C:/Users/91911/Desktop/test.txt");
     36 //        File file = new File("C:/Users/91911/Desktop/result.txt");
     37         for(String s1:source) {
     38             String splited[] = s1.split("	");
     39             double[] x = splineInterpolatorImpl.String2Double(splited[0]);
     40             double[] y = splineInterpolatorImpl.String2Double(splited[1]);
     41             double z = Double.parseDouble(splited[2]);
     42             double result = splineInterpolatorImpl.caculate(x, y, z);
     43             exportFile(s1+"	"+result,file);
     44 //            System.out.println(splineInterpolatorImpl.caculate(x, y, z));
     45         }
     46     }
     47 
     48     // 读取配置文档
     49     public static List<String> getFileContent(String filepath) {
     50         List<String> list = new ArrayList<>();
     51         BufferedReader br;
     52         String rec;
     53         try {
     54             br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filepath)), "GBK"));
     55             while ((rec = br.readLine()) != null) {
     56                 if (StringUtils.isNotEmpty(rec.trim())) {
     57                     list.add(rec);
     58                 }
     59             }
     60             br.close();
     61         } catch (UnsupportedEncodingException e) {
     62             // TODO Auto-generated catch block
     63             System.out.println("转码出错!");
     64             e.printStackTrace();
     65             return null;
     66         } catch (FileNotFoundException e) {
     67             // TODO Auto-generated catch block
     68             System.out.println("未找到配置文件 " + filepath + " ,请检查该路径是否正确!");
     69             e.printStackTrace();
     70             return null;
     71         } catch (IOException e) {
     72             // TODO Auto-generated catch block
     73             e.printStackTrace();
     74             return null;
     75         }
     76         return list;
     77     }
     78     
     79     //写文件    
     80     public static void exportFile(String content,File file){                
     81         try {
     82             FileWriter out = new FileWriter(file,true);    
     83             out.write(content + "
    ");
     84             out.flush();
     85             out.close();
     86         }catch (IOException e){
     87             System.out.println("!IO异常,写文件异常");
     88         }    
     89     }
     90     //样条计算法
     91     public double caculate(double[] x,double[] y, double z){
     92         SplineInterpolator sp = new SplineInterpolator();
     93         PolynomialSplineFunction f = sp.interpolate(x, y);
     94         return f.value(z);
     95     }
     96     
     97     //将字符型转换为double型
     98     public static double[] String2Double(String str) {
     99         double[] d = { 1 };
    100         if (str.contains(",")) {
    101             String[] arr = str.split(",");
    102             d = new double[arr.length];
    103             for (int i = 0; i < arr.length; i++) {
    104                 // System.out.println(arr[i]);
    105                 d[i] = Double.valueOf(arr[i].trim());
    106             }
    107         }
    108         return d;
    109     }
    110 }
  • 相关阅读:
    巧用Windebug分析蓝屏故障
    MyBatisPlus 笔记
    Mybatis笔记
    统一大市场 stone
    美联储加息 stone
    MATLAB安装及激活教程
    配置中心Nacos与Apollo比较
    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'xxxxl':
    鉴权拦截器跳转登录页面
    @ConfigurationProperties失效的几种情况看看你是那种
  • 原文地址:https://www.cnblogs.com/xiao02fang/p/9705755.html
Copyright © 2020-2023  润新知