Вы находитесь на странице: 1из 9

import import import import

java.util.StringTokenizer; java.util.*; java.util.InputMismatchException; javax.swing.tree.DefaultTreeModel;

class InterpreterTwo { String lookAhead; StringTokenizer token; int stringNumber; boolean flag; boolean stopFlag = false; boolean addTree; boolean leftOperand=true; boolean rightOperand =true; Tree tree; Tree fullTree; String operandLeft; String operandRight; String operator; ArrayList list; InterpreterTwo() { list = new ArrayList(); flag = true; addTree = true; } void expression() { try { if(lookAhead.equals("~") ) { tree = new Tree(new BTNode(lookAhead,null,null)); fullTree.addTreeAsChildLeft(tree); getToken(); expression(); } else if(operand()) {

if(leftOperand == true) { // System.out.println("left"); fullTree.insertLeft(new BTNode(lookAhead,null,null)); }

else { //System.out.println("right"); fullTree.insertRight(new BTNode(lookAhead,null,null)); } getToken(); } else if(lookAhead.equals("(") ) { //each exp here is its own tree with a leftoperand, rightoperand and operator at root parentheSizedGrammar(); } else { throw new InputMismatchException(lookAhead + " caused an error because unexpected character and was found at string number: " + stringNumber); // error("unidentified character "); } } catch(InputMismatchException e) { if(stopFlag == false) System.out.println(e); flag = false; stopFlag=true; } } boolean operand() { //System.out.println("operand"); if(lookAhead.equalsIgnoreCase("T") || lookAhead.equalsIgnoreCase("F") || lookAhead.equalsIgnoreCase("true") || lookAhead.equalsIgnoreCase("false") ) return true; else return false; } void parentheSizedGrammar() { getToken(); expression(); leftOperand=false;

operator(); expression(); match(")");

leftOperand=true; } void operator() { try { if(lookAhead.equalsIgnoreCase("^") || lookAhead.equalsIgnoreCase("v") ) { tree = new Tree(new BTNode(lookAhead)); fullTree.addTreeAsChildLeft(tree); getToken(); } else { //error("expecting operator "); throw new InputMismatchException(lookAhead + " caused an error because expecting operator and was found at string number: " + stringNumber); } } catch(InputMismatchException e) { if(stopFlag == false) System.out.println(e); flag = false; stopFlag=true; } } void getToken() { if(token.hasMoreTokens()) { lookAhead = token.nextToken(); stringNumber++; } } boolean evaluate(String s) { stopFlag=false;

stringNumber=0; token = new StringTokenizer(s); getToken(); fullTree = new Tree(); expression(); return flag; } boolean evaluateShowingTraversal(String s) { stopFlag=false; //add a flag to start tree adding or just insert print tree in this one method only could work as well stringNumber=0; token = new StringTokenizer(s); getToken(); fullTree = new Tree(); expression(); putTogetherTree(); return flag; } void match(String s) { //System.out.println("lookahead: " + lookAhead + " s: " + s); try { if(lookAhead.equals(s)) getToken(); else { throw new InputMismatchException(lookAhead + " caused an error because expecting ) and was found at string number: " + stringNumber); //error("expecting ) "); } } catch(InputMismatchException e) { if(stopFlag == false) System.out.println(e); flag = false; stopFlag=true; }

} //

void putTogetherTree() { System.out.println("===================="); fullTree.pretrav(fullTree.getRoot()); System.out.println("===================="); }

} import import import import import import java.io.*; java.nio.file.*; java.nio.file.attribute.*; static java.nio.file.FileVisitResult.*; static java.nio.file.FileVisitOption.*; java.util.*;

public class Find { public static class Finder extends SimpleFileVisitor<Path> { private final PathMatcher matcher; private int numMatches = 0; Finder(String pattern) { matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern); } //Compares the glob pattern against the file or directory name. void find(Path file) { Path name = file.getFileName(); if (name != null && matcher.matches(name)) { numMatches++; System.out.println(file); } } //Prints the total number of matches to standard out. void done() { System.out.println("Matched: " + numMatches); } //Invoke the pattern matching method on each file. @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { find(file); return CONTINUE; } //Invoke the pattern matching method on each directory.

@Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { find(dir); return CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) { System.err.println(exc); return CONTINUE; } } static void usage() { System.err.println("java Find <path> -name \"<glob_pattern>\""); System.exit(-1); } public static void main(String[] args) throws IOException { if (args.length < 3 || !args[1].equals("-name")) usage(); Path startingDir = Paths.get(args[0]); String pattern = args[2]; Finder finder = new Finder(pattern); Files.walkFileTree(startingDir, finder); finder.done(); } }

Point p[]; int num; void setup() { size(500,250); num=12; p=new Point[num*2]; for(int i=0; i< num*2; i++) p[i]=new Point(0,0); initPt(); } void draw() { background(200); p[num-1].set(mouseX,mouseY); // check intersections with all lines for(int j=0; j< num; j++) {

line(p[j*2].x,p[j*2].y, p[j*2+1].x,p[j*2+1].y); for(int i=0; i< num; i++) if(i!=j) { Point pt=findIntersection( p[i*2],p[i*2+1], p[j*2],p[j*2+1]); if(pt!=null) ellipse(pt.x,pt.y, 14,14); } } } void initPt() { for(int i=0; i< num; i++) { if(random(100)>50) { p[i*2].set(20,random(20,height-20)); p[i*2+1].set(width-20,random(20,height-20)); } else { p[i*2].set(random(20,width-20),20); p[i*2+1].set(random(20,width-20),height-20); } } } void mousePressed() { initPt(); p[num-2].set(mouseX,mouseY); } // calculates intersection and checks for parallel lines. // also checks that the intersection point is actually on // the line segment p1-p2 Point findIntersection(Point p1,Point p2, Point p3,Point p4) { float xD1,yD1,xD2,yD2,xD3,yD3; float dot,deg,len1,len2; float segmentLen1,segmentLen2; float ua,ub,div; // calculate differences xD1=p2.x-p1.x; xD2=p4.x-p3.x; yD1=p2.y-p1.y; yD2=p4.y-p3.y; xD3=p1.x-p3.x;

yD3=p1.y-p3.y; // calculate the lengths of the two lines len1=sqrt(xD1*xD1+yD1*yD1); len2=sqrt(xD2*xD2+yD2*yD2); // calculate angle between the two lines. dot=(xD1*xD2+yD1*yD2); // dot product deg=dot/(len1*len2); // if abs(angle)==1 then the lines are parallell, // so no intersection is possible if(abs(deg)==1) return null; // find intersection Pt between two lines Point pt=new Point(0,0); div=yD2*xD1-xD2*yD1; ua=(xD2*yD3-yD2*xD3)/div; ub=(xD1*yD3-yD1*xD3)/div; pt.x=p1.x+ua*xD1; pt.y=p1.y+ua*yD1; // calculate the combined length of the two segments // between Pt-p1 and Pt-p2 xD1=pt.x-p1.x; xD2=pt.x-p2.x; yD1=pt.y-p1.y; yD2=pt.y-p2.y; segmentLen1=sqrt(xD1*xD1+yD1*yD1)+sqrt(xD2*xD2+yD2*yD2); // calculate the combined length of the two segments // between Pt-p3 and Pt-p4 xD1=pt.x-p3.x; xD2=pt.x-p4.x; yD1=pt.y-p3.y; yD2=pt.y-p4.y; segmentLen2=sqrt(xD1*xD1+yD1*yD1)+sqrt(xD2*xD2+yD2*yD2); // if the lengths of both sets of segments are the same as // the lenghts of the two lines the point is actually // on the line segment. // if the point isn't on the line, return null if(abs(len1-segmentLen1)>0.01 || abs(len2-segmentLen2)>0.01)

return null; // return the valid intersection return pt; } class Point{ float x,y; Point(float x, float y){ this.x = x; this.y = y; } void set(float x, float y){ this.x = x; this.y = y; } }

Вам также может понравиться