edit() { simulator.setVar("flag", true); } edit2() { simulator.setVar("reset", true); } getCorrectNum(double up, double down) { int up1 = (int)Math.round(up*100.); int down1 = (int)Math.round(down*100.); for(int i = Math.min(Math.abs(up1), Math.abs(down1)); i>0; i--){ if(up1%i==0 && down1%i==0){ up1 = up1/i; down1 = down1/i; } } simulator.setVar("trueUp", up1); simulator.setVar("trueDown", down1); } void dispatch() { String command = simulator.getNextEvent(); if (command.equals("buttonClick")) edit2(); else if (command.equals("textChanged")) edit(); } boolean isValidFunc(String func) { String funcPrefix = ""; String funcRoot = ""; boolean isValid = true; java.util.StringTokenizer st = new java.util.StringTokenizer(func, "("); funcPrefix = st.nextElement(); funcRoot = st.nextElement(); isValid = false; int xCount= 0; int signCount = 0; for(int m = 0; m< funcRoot.length(); m++){ if((funcRoot.substring(m, m+1)).equalsIgnoreCase("x")){ xCount++; if(!(funcRoot.substring(m+1, m+2)).equalsIgnoreCase("+") && !(funcRoot.substring(m+1, m+2)).equalsIgnoreCase("-") && !(funcRoot.substring(m+1, m+2)).equalsIgnoreCase(")")){ return false; } } if((funcRoot.substring(m, m+1)).equalsIgnoreCase("+") || (funcRoot.substring(m, m+1)).equalsIgnoreCase("-")){ signCount++; } } if(xCount!=1 || signCount>1){ return false; } java.util.StringTokenizer st1 = new java.util.StringTokenizer(funcPrefix, "*"); String temp = st1.nextElement(); if(st1.hasMoreTokens()) func = st1.nextElement(); else func = temp; funcPrefix = func; isValid = true; //System.out.println("func1 = "+func); //System.out.println("func1 = "+funcPrefix); //System.out.println("func1 = "+); if(funcPrefix.equalsIgnoreCase("sin")) { //System.out.println("here!"); simulator.setVar("func_type", 1); simulator.setVar("obrat", "(-1)^n*arcsin"); } else if(funcPrefix.equalsIgnoreCase("cos")) { simulator.setVar("func_type", 2); simulator.setVar("obrat", "±arccos"); } else if(funcPrefix.equalsIgnoreCase("tg")) { simulator.setVar("func_type", 3); simulator.setVar("obrat", "arctg"); } else if(funcPrefix.equalsIgnoreCase("ctg")) { simulator.setVar("func_type", 4); simulator.setVar("obrat", "arcctg"); } else { isValid = false; } return isValid; } Element graphCanvas = simulator.getObject("Plotter Tank 0"); Element textField1 = simulator.getObject("el00063"); Element textField2 = simulator.getObject("el00060"); Element textField3 = simulator.getObject("el00068"); Element text = simulator.getObject("Text shape_0002"); Element text_no = simulator.getObject("Text shape_0003"); Element text_wait = simulator.getObject("Text shape_0004"); Element curve1 = simulator.getObject("el00054"); Element curve2 = simulator.getObject("el00057"); Element button = simulator.getObject("el00074"); text.setVisibility(false); textField3.setEnabled(false); textField3.update(); textField2.setEnabled(false); textField2.update(); text.setVisibility(false); text.update(); text_no.setVisibility(false); text_no.update(); text_wait.setVisibility(false); text_wait.update(); Boolean correct = new Boolean(true); Boolean parse = new Boolean(false); simulator.setVar("flag",false); simulator.setVar("reset",false); simulator.setVar("func_type", 0); simulator.setVar("obrat", ""); simulator.setVar("trueUp", 0); simulator.setVar("trueDown", 0); String func1 = ""; String func2 = ""; String obrat = ""; int count = 0; physicon.funcs2.Func ff1; physicon.funcs2.Func ff2; simulator.setVar("flag", true); physicon.parser2.Parser p = new physicon.parser2.Parser(); while (true) { if (simulator.getVar("flag")) { text_no.setVisibility(false); text_no.update(); String answer = ""; String a = ""; String b = ""; String c = ""; String d = ""; boolean isPlus = false; for(int i = 0; i< count; i++) simulator.deleteObject("el11"+i); text.setVisibility(false); text.update(); String str = textField1.getString("Caption"); textField2.set("Caption", ""); textField2.update(); text_wait.setVisibility(true); text_wait.update(); func1 = ""; func2 = ""; int i = 0; boolean showFunc = true; parse = false; while(!parse){ if(i==str.length()-1){ correct = false; parse = true; } if(str.substring(i, i+1).equals("=")){ func1 = str.substring(0, i); func2 = str.substring(i+1, str.length()); } i++; } //System.out.println("f1 = " + func1); try {ff1 = p.parse(func1);} catch (Exception e) { curve1.setVisibility(false); curve2.setVisibility(false); curve1.update(); curve2.update(); text.setVisibility(true); text.update(); text_wait.setVisibility(false); text_wait.update(); showFunc = false; } // System.out.println("f2 = " + func2); try {ff2 = p.parse(func2);} catch (Exception e) { curve1.setVisibility(false); curve2.setVisibility(false); curve1.update(); curve2.update(); text.setVisibility(true); text.update(); text_wait.setVisibility(false); text_wait.update(); showFunc = false; } String prefix = ""; String postfix = ""; boolean isPow = false; boolean parsing = true; if(!isValidFunc(func1)){ curve1.setVisibility(false); curve2.setVisibility(false); curve1.update(); curve2.update(); text.setVisibility(true); text.update(); text_wait.setVisibility(false); text_wait.update(); showFunc = false; } if(showFunc){ try{ java.util.StringTokenizer st = new java.util.StringTokenizer(func1, "("); String funcPrefix = st.nextElement(); String funcInside = st.nextElement(); java.util.StringTokenizer st1 = new java.util.StringTokenizer(funcPrefix, "*"); a = st1.nextElement(); // System.out.println("a = " + a); if(!st1.hasMoreTokens()) a = "1"; funcInside = funcInside.substring(0, funcInside.length()-1); java.util.StringTokenizer st2 = new java.util.StringTokenizer(funcInside, "+"); String bx = st2.nextElement(); if(st2.hasMoreTokens()){ c = st2.nextElement(); isPlus = true; } else{ java.util.StringTokenizer st2 = new java.util.StringTokenizer(funcInside, "-"); String bx = st2.nextElement(); if(st2.hasMoreTokens()){ c = st2.nextElement(); } else c = ""; } java.util.StringTokenizer st3 = new java.util.StringTokenizer(bx, "x"); if(st3.hasMoreTokens()){b = st3.nextElement(); // System.out.println("b = " + b); if(b.length()==0) b = "1"; //else b = b.substring(0, b.length()-1); else if(b.substring(b.length()-1, b.length()).equalsIgnoreCase("*")) b = b.substring(0, b.length()-1);} else b = "1"; // System.out.println("b3 = " + b); // if(st3.hasMoreTokens()){ // } // else b = "1"; d = func2; Double d1 = new Double(d); Double a1 = new Double(a); double b1 = new Double(b); double argD = d1.doubleValue()/a1.doubleValue(); getCorrectNum(Math.round(d1.doubleValue()*100.)/100., Math.round(a1.doubleValue()*100.)/100.); int dd1 = simulator.getVar("trueUp"); int aa1 = simulator.getVar("trueDown"); // System.out.println("a = " + a); /// System.out.println("b = " + b); // System.out.println("c = " + c); // System.out.println("d = " + d); String arg = ""; boolean isPeriodChange = false; // System.out.println("1"); //if(Math.abs(d1.doubleValue())>=1) arg = d1.doubleValue()+"/"+a1.doubleValue();//Math.round(arg*10.)/10.; if(d1.doubleValue()==a1.doubleValue()) arg = "1"; else if(d1.doubleValue()==-a1.doubleValue()) arg = "-1"; //else if(Math.abs(d1.doubleValue())>=1) arg = Math.abs(Math.round(d1.doubleValue()*10.)/10.)+"/"+Math.abs(Math.round(a1.doubleValue()*10.)/10.);//Math.round(arg*10.)/10.; if(a.length()<=4 && d.length()<=4 )arg = dd1+"/"+aa1;//Math.round(arg*10.)/10.; else arg = d1.doubleValue()+"/"+a1.doubleValue(); // if(!c.equalsIgnoreCase("") && argD!=0 && argD!=1 && argD!=0.5 && argD!=-0.5 && argD!=-1){ // if(simulator.getVar("obrat").equalsIgnoreCase("arccos")) answer = "±("; // else if(simulator.getVar("obrat").equalsIgnoreCase("arcsin")) answer = "(-1)^n*("; // else answer = "("; // } String sin = ""; if(!c.equalsIgnoreCase("") && !b.equalsIgnoreCase("") && Math.abs(argD)!=1 && Math.abs(argD)!=0.5 && Math.abs(argD)!=0) answer += "("; if(argD==0){ if(simulator.getVar("obrat").equalsIgnoreCase("(-1)^n*arcsin")) answer += ""; else if(simulator.getVar("obrat").equalsIgnoreCase("±arccos")) answer += "Pi/"+(int)(Math.round(2*b1)); else if(simulator.getVar("obrat").equalsIgnoreCase("arctg")) answer += ""; else if(simulator.getVar("obrat").equalsIgnoreCase("arcctg")) answer += "Pi/"+(int)(Math.round(2*b1)); isPeriodChange = true; } else if(argD==0.5 && (simulator.getVar("obrat").equalsIgnoreCase("(-1)^n*arcsin") || simulator.getVar("obrat").equalsIgnoreCase("±arccos"))){ if(simulator.getVar("obrat").equalsIgnoreCase("(-1)^n*arcsin")) answer += "(-1)^n*Pi/"+(int)(Math.round(6*b1)); else if(simulator.getVar("obrat").equalsIgnoreCase("±arccos")) answer += "±Pi/"+(int)(Math.round(3*b1)); //isPeriodChange = true; } else if(argD==-0.5 && (simulator.getVar("obrat").equalsIgnoreCase("(-1)^n*arcsin") || simulator.getVar("obrat").equalsIgnoreCase("±arccos"))){ if(simulator.getVar("obrat").equalsIgnoreCase("(-1)^n*arcsin")) answer += "(-1)^(n+1)*Pi/"+(int)(Math.round(6*b1)); else if(simulator.getVar("obrat").equalsIgnoreCase("±arccos")) answer += "±2*Pi/"+(int)(Math.round(3*b1)); //isPeriodChange = true; } else if(argD==1){ if(simulator.getVar("obrat").equalsIgnoreCase("(-1)^n*arcsin")){ //answer += "Pi/2"; answer += (b1>0 ? "-Pi/"+(int)(Math.round(2*b1)) : "Pi/"+(int)(Math.round(2*b1))); sin = "2"; } if(simulator.getVar("obrat").equalsIgnoreCase("±arccos")){ answer += ""; } if(simulator.getVar("obrat").equalsIgnoreCase("arctg")){ answer += "Pi/"+(int)(Math.round(4*b1)); } if(simulator.getVar("obrat").equalsIgnoreCase("arcctg")){ answer += "Pi/"+(int)(Math.round(4*b1)); } } else if(argD==-1){ if(simulator.getVar("obrat").equalsIgnoreCase("(-1)^n*arcsin")){ answer += (b1>0 ? "-Pi/"+(int)(Math.round(2*b1)) : "Pi/"+(int)(Math.round(2*b1))); sin = "2"; } if(simulator.getVar("obrat").equalsIgnoreCase("±arccos")){ if(b1==1.) answer += "Pi"; else answer += "Pi/"+b1; } if(simulator.getVar("obrat").equalsIgnoreCase("arctg")){ answer += "-Pi/"+(int)(Math.round(4*b1)); } if(simulator.getVar("obrat").equalsIgnoreCase("arcctg")){ answer += "-Pi/"+(int)(Math.round(4*b1)); } } else if(argD!=0){ // if(simulator.getVar("obrat").equalsIgnoreCase("arctg") && argD<0){ // answr += // } if(!simulator.getVar("obrat").equalsIgnoreCase("±arccos")){ if(argD<0) answer += "-"; } answer += simulator.getVar("obrat")+"("+arg+")"; if(!b.equalsIgnoreCase("1")&&!b.equalsIgnoreCase("-1")&&!b.equalsIgnoreCase("(1)")&&!b.equalsIgnoreCase("(-1)")) answer += "/"+b; } // System.out.println("2"); if(c.equalsIgnoreCase("0")){} else{ if(!c.equalsIgnoreCase("")){ if(Double.valueOf(c)>0){ if(argD==0 || Math.abs(argD)==0.5 || Math.abs(argD)==1) answer = "("+answer; if(isPlus) answer += " - "; else if(!isPlus && argD!=0 && answer!="") answer += " + "; answer += c; if(!b.equalsIgnoreCase("1")&&!b.equalsIgnoreCase("-1")&&!b.equalsIgnoreCase("(1)")&&!b.equalsIgnoreCase("(-1)")) answer += "/"+b; if(argD==0 || Math.abs(argD)==0.5 || Math.abs(argD)==1) answer += ")"; // System.out.println("1"); } else{ answer += c; if(!b.equalsIgnoreCase("1")&&!b.equalsIgnoreCase("-1")&&!b.equalsIgnoreCase("(1)")&&!b.equalsIgnoreCase("(-1)")) answer += "/"+b; } } } //System.out.println("answer 1= " + answer); // System.out.println("3"); if(!c.equalsIgnoreCase("") && Math.abs(argD)!=1 && Math.abs(argD)!=0.5 && Math.abs(argD)!=0 /*&& !b.equalsIgnoreCase("1")&&!b.equalsIgnoreCase("-1")&&!b.equalsIgnoreCase("(1)")&&!b.equalsIgnoreCase("(-1)")*/){ answer += ")"; //System.out.println("answer = " + answer); } // System.out.println("answer 2= " + answer); if(!b.equalsIgnoreCase("1")&&!b.equalsIgnoreCase("-1")&&!b.equalsIgnoreCase("(1)")&&!b.equalsIgnoreCase("(-1)")){ //if(simulator.getVar("obrat").equalsIgnoreCase("(-1)^n*arcsin")&&argD!=0) answer += "*(1/"+b+")"; } boolean isTwo = false; // System.out.println("answer3 = " + answer); if(!isPeriodChange && !simulator.getVar("obrat").equalsIgnoreCase("arcctg") && !simulator.getVar("obrat").equalsIgnoreCase("arctg") && !simulator.getVar("obrat").equalsIgnoreCase("(-1)^n*arcsin")){ answer += (answer.equalsIgnoreCase("") ? "" : " + ")+ "2"; isTwo = true; } else if(answer.trim()!="" && !simulator.getVar("obrat").equalsIgnoreCase("arctg")||!simulator.getVar("obrat").equalsIgnoreCase("arcctg")){ if(!answer.trim().equals("")){ answer += " + "; } } else if(simulator.getVar("obrat").equalsIgnoreCase("(-1)^n*arcsin")&&argD!=0){ answer += " + "; } // System.out.println("answer 4= " + answer); // if(isPeriodChange && !b.equalsIgnoreCase("1")) answer += "1"; if(!b.equalsIgnoreCase("1")&&!b.equalsIgnoreCase("-1")&&!b.equalsIgnoreCase("(1)")&&!b.equalsIgnoreCase("(-1)")) answer += (!isTwo ? "1" : "")+"/"+b+"*"; // System.out.println("answer 4= " + answer); if(!sin.trim().equalsIgnoreCase("")) answer += sin; // System.out.println("answer 5= " + answer); answer += "Pi*n"; }catch(Exception e){ curve1.setVisibility(false); curve2.setVisibility(false); curve1.update(); curve2.update(); text.setVisibility(true); text.update(); text_wait.setVisibility(false); text_wait.update(); showFunc = false; } } if(a.equalsIgnoreCase("0")) func1="0"; if(showFunc){ String[] param = graphCanvas.getProperty("Minimal_X_value"); double minValue = (Double.valueOf(param[0].trim())).doubleValue()*Math.PI; String[] param1 = graphCanvas.getProperty("Maximal_X_value"); double maxValue = (Double.valueOf(param1[0].trim())).doubleValue()*Math.PI; boolean isEqual = true; for(int k = 0; k<10; k++){ double param = 10.*Math.random(); if(ff1.getValue(param)!=ff2.getValue(param) || ff1.getValue(-param)!=ff2.getValue(-param)){ isEqual = false; break; } } if(!isEqual){ double step = 0.0157*Math.PI; double[] eqilibrium = new double[500]; boolean isEquilibrium = false; count = 0; double epsilon = 0.005; double point = 0; int count1 = 0; boolean up = (ff1.getValue(minValue)>ff2.getValue(minValue)); for(double i = minValue; i<=maxValue; count1++){ i = minValue + count1*step; if(ff1.getValue(i)>ff2.getValue(i) && !up){ if(Math.abs(ff1.getValue(i)-ff1.getValue(i-step))<10 && Math.abs(ff2.getValue(i)-ff2.getValue(i-step))<10){ eqilibrium[count] = i-step/2; isEquilibrium = true; count++; } up = true; } else if(ff1.getValue(i)ff2.getValue(i+step)); isEquilibrium = true; } else isEquilibrium = true; } else isEquilibrium = false; } int answerCount = 0; for(int i = 0; i0){ textField2.set("Caption", answer); textField2.update(); text_wait.setVisibility(false); text_wait.update(); } if(count==0){ text_no.setVisibility(true); text_no.update(); text_wait.setVisibility(false); text_wait.update(); } } else{ textField2.set("Caption", "["+minValue+", "+maxValue+"]"); textField2.update(); text_wait.setVisibility(false); text_wait.update(); } // System.out.println("func = " + func1); curve1.set("Function", func1); curve1.setVisibility(showFunc); curve1.update(); curve2.set("Function", func2); curve2.setVisibility(showFunc); curve2.update(); } simulator.setVar("flag", false); } else if(simulator.getVar("reset")){ text.setVisibility(false); text.update(); text_no.setVisibility(false); text_no.update(); curve1.setVisibility(false); curve2.setVisibility(false); curve1.update(); curve2.update(); textField1.set("Caption", ""); textField1.update(); textField2.set("Caption", ""); textField2.update(); text_wait.setVisibility(false); text_wait.update(); simulator.setVar("reset", false); } dispatch(); Thread.sleep(500); }