Du bist hier: Snippet-Verzeichnis » Java (241)
Sprache:

2-dimensional orthogonal range searching-main

Sprache: English
Programmiersprache: Java
Veröffentlicht von: menny [nicht registriert]
Letzte Änderung: 15.05.2006
Aufrufe: 926


Beschreibung

This is a sample Main class that shows how to use 2-dimensional orthogonal range trees.it also includes some file handling functions.

Code

1 // 2 //This class will show how to build and query a 2-dim. range tree. 3 //The 2-dim. object used is class Point (made in Introduction to Computer Sceince course.). 4 // 5 // 6 7 8 9 import java.io.*; // this is for handling files Input/Output. 10 import java.util.*; // this is for the StringTokenizer 11 12 class Orth_Search{ 13 public static void main(String[] args) 14 { 15 int howMany=0; //will hold: how many points exist in the plane (provided by the input file). 16 17 try //will try to make IO operations 18 { 19 20 if (args.length==0) //an error message when no parameters provided. 21 { 22 System.out.println("Correct syntex is:"); 23 System.out.println("java main input.txt output.txt"); 24 System.out.println("or"); 25 System.out.println("java main input.txt"); 26 System.exit(0); 27 } 28 if (args.length>2) //an error message when too much parameters provided. 29 { 30 System.out.println("Correct syntex is:"); 31 System.out.println("java main input.txt output.txt"); 32 System.out.println("java main input.txt"); 33 System.exit(0); 34 } 35 36 FileInputStream fis = new FileInputStream(args[0]); //this will open the input file. 37 InputStreamReader isr = new InputStreamReader(fis); 38 BufferedReader in = new BufferedReader(isr); 39 40 if (!(in.ready())) //file is empty? 41 { //error, file is empty. 42 System.out.println("The file "+args[0]+" is empty."); 43 System.out.println("Unable to continue"); 44 System.exit(0); 45 } 46 47 try //will try to get number of points. 48 { 49 howMany=Integer.parseInt(in.readLine(), 10); 50 } 51 catch (NumberFormatException e) 52 { 53 System.out.println("The file provided is not in the currect format."); 54 System.exit(0); 55 }//catch string to int 56 57 Point[] A=new Point[howMany]; //creating the 2D objects. 58 59 for(int i=0;i<howMany;i++) //retrieving the points from the input file. 60 { 61 StringTokenizer st = new StringTokenizer(in.readLine()); 62 A[i]=new Point(Integer.parseInt(st.nextToken(), 10),Integer.parseInt(st.nextToken(), 10)); 63 }// for 64 65 Tree.mergeSort(A,0);//sorting array by X-cords for use with the constructor 66 67 Tree t=new Tree(A); 68 69 boolean cont=true;//a temp boolean which be false on end of file. 70 int counter=0; 71 while (in.ready()&&cont) //querying... 72 { 73 try 74 { 75 StringTokenizer st = new StringTokenizer(in.readLine()," ",false); 76 int xlow=Integer.parseInt(st.nextToken(), 10); //retrieving the cell to be checked. 77 int xhigh=Integer.parseInt(st.nextToken(), 10); 78 int ylow=Integer.parseInt(st.nextToken(), 10); 79 int yhigh=Integer.parseInt(st.nextToken(), 10); 80 81 Point[] B=t.query2D(xlow,xhigh,ylow,yhigh); //querying the tree. 82 counter++; 83 84 if (args.length!=2) //if no output file provided, will output to screen. 85 { 86 System.out.println(xlow+" "+xhigh+" "+ylow+" "+yhigh); 87 System.out.println("C"+counter); 88 if (B!=null) for(int i=0;i<B.length;i++) System.out.println(B[i]); 89 } 90 else //output filename provided. outputing to file. 91 { 92 FileOutputStream fos = new FileOutputStream(args[1],true); 93 Writer out = new OutputStreamWriter(fos); 94 out.write(xlow+" "+xhigh+" "+ylow+" "+yhigh+"\n"); 95 96 out.write("C"+counter+"\n"); 97 98 if (B!=null) for(int i=0;i<B.length;i++) out.write(B[i]+"\n"); 99 out.close(); 100 } 101 }//try 102 catch (Exception e) //this will catch EOF. 103 { 104 cont=false; 105 }//catch 106 }//while in.ready()&&cont 107 108 in.close(); 109 }//try 110 catch (IOException e) //this will catch all the errors in input file. 111 { 112 e.printStackTrace(); 113 System.exit(0); 114 }//catch IO 115 }//main 116 }//class

Noch kein Kommentar vorhanden

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS