您的位置:首页 > 其它

第四周作业——图的表示

2014-04-01 19:48 253 查看
package program;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class GraphRepresentation {

public static void main(String[] args) {
// TODO 自动生成的方法存根

File file1 = new File("tinyG.txt");
File file2 = new File("tinyG_matrix.txt");
int V;
int E;
int g[][];
int tempchar;
String str = "";

try {
FileReader in = new FileReader(file1);
BufferedReader bufr = new BufferedReader(in);
FileWriter fw = new FileWriter(file2);
BufferedWriter bufw = new BufferedWriter(fw);
str = bufr.readLine();
V = Integer.parseInt(str);
str = bufr.readLine();
E = Integer.parseInt(str);
g = new int[V][V];
str = "";
int a = 0, b = 0;
while ((tempchar = bufr.read()) != -1) {
str = str + (char) tempchar;
if ((char) tempchar == ' ') {
a = Integer.parseInt(str.trim());
str = "";
}
if ((char) tempchar == '\n') {
b = Integer.parseInt(str.trim());
g[a][b] = 1;
g[b][a] = 1;
str = "";
}

}
str="";
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
System.out.print(g[i][j]+" ");
str=str+Integer.toString(g[i][j]);
if(j==12){
System.out.println("");
bufw.write(str);
bufw.newLine();
str="";
}
}
}
bufr.close();
in.close();
bufw.close();
fw.close();
}

catch(Exception e) {
e.printStackTrace();
}

}

}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: