⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readfile.java

📁 一个java程序,很有用的哦,可以看看,借鉴借览的
💻 JAVA
字号:
/*
 * ReadFile.java
 *
 * Created on 2007年5月28日, 下午10:59
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package cindy;

import java.io.*;
import java.text.*;
import java.util.*;
/**
 *
 * @author njchenyi
 */
public class ReadFile {
    
    private DecimalFormat df=new DecimalFormat("#.00");
    private BufferedReader infile = null;
    String filename ="glass7C数据.txt";
    private String inLine;
    private Cluster cluster;
    private int index=0;
    private Vector all = new Vector();  //存放分类
    private Vector temp = new Vector(); //存放所有的样本
    private int eachNo = 0;
    //初始分类的个数
    private static int CLUSTERNO=6;
    
    /** Creates a new instance of FileReader */
    public ReadFile() {
        try{
            infile = new BufferedReader(new FileReader(filename));
            while((inLine=infile.readLine())!=null) {
                index++;
                //System.out.println(inLine);
                
                //数据文件名
                //Sample i=new iris3CSample(inLine,index);
                Sample i = new glass7C(inLine, index);
                
                temp.add(i); //将样本存放进一个堆栈
                //((Cluster)all.elementAt(0)).addSample(i);
                //System.out.println(df.format(((Cluster)all.elementAt(0)).getWeightPoint()[0]));
                //System.out.println(((Cluster)all.elementAt(0)).getDistanceSum(i));
            }
            step();
        }
        
        catch(FileNotFoundException ex){
            System.out.println("can not find file "+ filename);
        } catch(IOException ex){
            System.out.println(ex.getMessage());
        } finally{
            try {
                if(infile!=null)
                    infile.close();
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
    
    private void step(){
        dividCluster();
        calculateP();
        //calculateJ();
    }
    private void dividCluster(){
        for(int i =0; i< CLUSTERNO; i++){
            //产生相应个分类,存放进堆栈
            cluster = new Cluster();
            all.add(cluster);
            for( int j=(temp.size()/CLUSTERNO)*(i); j<(temp.size()/CLUSTERNO)*(i+1);j++){
                //将堆栈中的样本平均存放进分类
                ((Cluster)all.elementAt(i)).addSample((Sample)temp.elementAt(j));
                
                //System.out.println(df.format(((Cluster)all.elementAt(i)).getWeightPoint()[0]));
                //System.out.println(((Cluster)all.elementAt(i)).getDistanceSum((Sample)temp.elementAt(j)));
            }
        }
        //System.out.println(df.format(((Cluster)all.elementAt(0)).getWeightPoint()[0]));
        //System.out.println(df.format(((Cluster)all.elementAt(1)).getWeightPoint()[0]));
        //System.out.println(df.format(((Cluster)all.elementAt(2)).getWeightPoint()[0]));
    }
    
    public void calculateP(){
        double p = 0.0;
        double tmpP;
        int tmpCluster;
        int tmpSample;
        int currentCluster;
        
        for(int i = 0; i< temp.size(); i++){
            //System.out.println(i);
            tmpP =100.0;
            tmpCluster=0;
            tmpSample=0;
            currentCluster=0;
            
            for(int j = 0; j<all.size();j++){
                int k =((Cluster)all.elementAt(j)).getSampleNo();
                if(k!=1){
                    if(((Cluster)all.elementAt(j)).isInCluster((Sample)temp.elementAt(i))){
                        //System.out.println(i + "  "+j);
                        currentCluster = j;
                        
                        
                        p = (k/(k-1))*((Cluster)all.elementAt(j)).getDistanceSum((Sample)temp.elementAt(i));
                        //System.out.println(p);
                        if(tmpP>p){
                            tmpP=p;
                            tmpCluster = j;
                            tmpSample = i;
                        }
                    } else{
                        
                        p = ((double)k/(k+1))*(((Cluster)all.elementAt(j)).getDistanceSum((Sample)temp.elementAt(i)));
                        //System.out.println(p);
                        if(tmpP>p){
                            tmpP=p;
                            tmpCluster = j;
                            tmpSample = i;
                        }
                    }
                }
            }
            //System.out.println(tmpP+" "+tmpCluster+" "+tmpSample);
            changeSample(currentCluster, tmpCluster, tmpSample);
        }
    }
    
    private void changeSample(int fromcluster, int tocluster, int sample){
        //System.out.println(fromcluster+" "+tocluster+" "+sample);
        ((Cluster)all.elementAt(fromcluster)).removeSample((Sample)temp.elementAt(sample));
        ((Cluster)all.elementAt(tocluster)).addSample((Sample)temp.elementAt(sample));
        //System.out.println(((Cluster)all.elementAt(fromcluster)).getSampleNo());
        //System.out.println(((Cluster)all.elementAt(tocluster)).getSampleNo());
    }
    
    public double calculateJ(){
        double Je=0.0;
        for(int i = 0; i<all.size();i++){
            Je=Je+((Cluster)all.elementAt(i)).getClusterDistanceSum();
            //System.out.println(Je);
        }
        //System.out.println(Je);
        return Je;
    }
    
    public Vector getClusterVector(){
        return all;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -