您的位置:首页 > 其它

第四周作业——图的表示

2014-04-11 22:53 387 查看
package work;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class GraphRepresentation {
public static void main(String[] args) {
try { FileReader r=new FileReader("F:/Arithmetic/tinyG.txt");
BufferedReader o=new BufferedReader(r);
int tc;
String str="";
str=o.readLine();
int a=Integer.parseInt(str);
str=o.readLine();
int E=Integer.parseInt(str);
int[][] arry=new int[a][a];
str="";
int v=0,e=0;
while((tc=o.read())!=-1)
{   str=str+(char)tc;
if ((char)tc==' ')
{  v=Integer.parseInt(str.trim());
str="";
}
if((char)tc=='\n')
{   e=Integer.parseInt(str.trim());
arry[v][e]=1;
arry[e][v]=1;
str="";
}
}
o.close();
r.close();
FileOutputStream f=new FileOutputStream("F:/Arithmetic/tinyG_matrix.txt");
OutputStreamWriter O=new OutputStreamWriter(f);
BufferedWriter w=new BufferedWriter(O);
str="";
System.out.println("图的邻接矩阵为 :");
for(int i=0;i<a;i++)
{  for(int j=0;j<a;j++)
{   System.out.print(arry[i][j]+" ");
str=str+Integer.toString(arry[i][j]);
if(j==12){
System.out.println();
w.write(str);
w.newLine();
str="";
}
}
}
w.close();
O.close();
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}


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