import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
public class GraphRepresentation {
public static void main(String[] orgs){
Test3 test3=new Test3();
test3.test("D:\\tinyG.txt");
}
}
class Test3{
Test3(){}
void test(String PathName){
FileReader file1 = null;
try {
file1 = new FileReader(PathName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader br=new BufferedReader(file1);
String s1=null;
int line=0;
String [][]strs=new String[13][2];
int count=0;
try {
while((s1=br.readLine())!=null){
line++;
if(line==1||line==2||line==3)continue;
String str=s1.trim();
if(!str.isEmpty()){
strs[count]=str.split(" ");
count++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
int[][] s5=new int [13][13];
for(int i=0;i<12;i++){
for(int j=0;j<12;j++){
s5[i][j]=0;
}
}
for(int i=0;i<13;i++){
for(int j=0;j<2;j++){
s5[Integer.parseInt(strs[i][0])][Integer.parseInt(strs[i][1])]=1;
s5[Integer.parseInt(strs[i][1])][Integer.parseInt(strs[i][0])]=1;
}
}
File file = new File("D:\\array.txt"); //存放数组数据的文件
FileWriter out = null;
try {
out = new FileWriter(file);
} catch (IOException e) {
e.printStackTrace();
} //文件写入流
//将数组中的数据写入到文件中。每行各数据之间TAB间隔
for(int i=0;i<12;i++){
for(int j=0;j<12;j++){
try {
out.write(s5[i][j]+" ");
} catch (IOException e) {
e.printStackTrace();
}
}
try {
out.write("rn");
} catch (IOException e) {
e.printStackTrace();
}
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
第四周作业 1(给定图数据文件(tinyG.txt),计算得到图的邻接矩阵,并把邻接矩阵保存到文件)
最新推荐文章于 2022-07-05 21:03:00 发布