# 求高手把下面Java代码转换成C++代码，跪求，不胜感激！

tear_knight 2015-06-25 03:59:06
code 1：
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
public class Client {
public static void main(String[] args) throws IOException{
//读数据，并以第三个点进行排序
File fin=new File("E:\\folia1.txt");
Collections.sort(list, new myComparator());

int N=Integer.parseInt(args[0]);//多少个数据为一个块，输入的参数
double downLimits=Integer.parseInt(args[1]);//压缩点的下限
double upLimits=Integer.parseInt(args[2]);//压缩点的上限

int M=(list.size()%N==0)?list.size()/N:list.size()/N+1;//总共可以划分为M块

//计算中心点
Coordinate[] center=new Coordinate[M];
for(int i=0;i<M;i++){
if(i==M-1){
center[i]=calculateCenter(list, i*N, list.size()-1);
}else {
center[i]=calculateCenter(list, i*N, i*N+N-1);
}
}

//计算距离
double[] distance=new double[list.size()];
for(int i=0;i<list.size();i++){
distance[i]=center[i/N].distance(list.get(i));
}

//压缩点
HashSet<Integer> set=resuce(distance,downLimits,upLimits);

//输出结果
for(int i=0;i<list.size();i++){
if(!set.contains(i)){
System.out.println(list.get(i).x+" "+list.get(i).y+" "+list.get(i).z+" "+list.get(i).intension);
}
}
//输出到文件中
//FileWriter fw = new FileWriter("E:\\out.txt");
File fout = new File("E:\\out.txt");
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
for(int i=0;i<list.size();i++){
if(!set.contains(i)){
bw.write(list.get(i).x+" "+list.get(i).y+" "+list.get(i).z+" "+list.get(i).intension);
bw.newLine();
}
}
bw.close();
}
private static Coordinate calculateCenter(ArrayList<Coordinate> list,int start,int end){
double x=0.0, y=0.0, z=0.0, intension=0.0;
for(int i=start;i<=end;i++){
x+=list.get(i).x;
y+=list.get(i).y;
z+=list.get(i).z;
intension+=list.get(i).intension;
}
x/=(end-start);y/=(end-start);z/=(end-start);intension/=(end-start);
return new Coordinate(x,y,z,intension);
}

/* 根据上下限参数进行压缩，记录需要压缩掉点的索引
* 下限等于0，上限不等于0，意味着压缩掉大于等于上限的点
* 下限不等于0，上限等于0，意味着压缩掉小于等于下限的点
* 下限上限都不等于0，意味着压缩掉小于等于上限，大于等于下限的点
* */
private static HashSet<Integer> resuce(double[] distance,double downLimits,double upLimits){
HashSet<Integer> set=new HashSet<Integer>();
if(upLimits==0){
for(int i=0;i<distance.length;i++){
if(distance[i]<=downLimits){
}
}
}else if(downLimits==0){
for(int i=0;i<distance.length;i++){
if(distance[i]>=upLimits){
}
}
}else {
for(int i=0;i<distance.length;i++){
if(distance[i]>=(double)downLimits && distance[i]<=(double)upLimits){
}
}
}
return set;
}
}

import java.util.Comparator;
class Coordinate {
double x;
double y;
double z;
double intension;

Coordinate(){
}

Coordinate(double x,double y,double z,double intension){
this.x=x;
this.y=y;
this.z=z;
this.intension=intension;
}

double distance(Coordinate coordinate) {
double res=Math.pow(this.x-coordinate.x, 2)+
Math.pow(this.y-coordinate.y, 2)+
Math.pow(this.z-coordinate.z, 2);
return Math.sqrt(res);
}

}
class myComparator implements Comparator<Coordinate>{
@Override
public int compare(Coordinate coordinate1, Coordinate coordinate2){
double z1=coordinate1.z;
double z2=coordinate2.z;

if(z1>z2)return 1;
else if(z1<z2)return -1;
else return 0;
}
}

code 3：
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public static ArrayList<Coordinate> readAllData(File fin) throws IOException{
ArrayList<Coordinate> list=new ArrayList<Coordinate>();
Scanner scanner=null;
try {
while(scanner.hasNextDouble()){
Coordinate temp=new Coordinate();
temp.x=scanner.nextDouble();
temp.y=scanner.nextDouble();
temp.z=scanner.nextDouble();
temp.intension=scanner.nextDouble();
}
} finally {
if(scanner!=null)
scanner.close();
}
return list;
}
}
...全文
72 回复 打赏 收藏 转发到动态 举报

50,595

javaspring bootspring cloud 技术论坛（原bbs）

• 近7日
• 近30日
• 至今