第四周算法作业

时间:2023-02-05 22:13:32
 
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 ();
}
}
}
第四周算法作业