?? izracunavanje.java
字號:
{ if ( poz>1 && ((Token)tokeni.elementAt( poz-2 )).oznaka!='F' && ((Token)tokeni.elementAt( poz-1 )).oznaka=='(' && ((Token)tokeni.elementAt( poz+1 )).oznaka==')' || poz==1 && ((Token)tokeni.elementAt( 0 )).oznaka=='(' && ((Token)tokeni.elementAt( 2 )).oznaka==')' ) { tokeni.removeElementAt( poz+1 ); tokeni.removeElementAt( poz-1 ); } return; } private static double stupnjevi_izlaz( double x ) { if ( Aplet.drg=='D' ) return Matematika.rad_to_deg( x ); else if ( Aplet.drg=='R' ) return x; else if ( Aplet.drg=='G' ) return Matematika.rad_to_grad( x ); else return 0; } private static double stupnjevi_ulaz( double x ) { if ( Aplet.drg=='D' ) return Matematika.deg_to_rad( x ); else if ( Aplet.drg=='R' ) return x; else if ( Aplet.drg=='G' ) return Matematika.grad_to_rad( x ); else return 0; } // izracunava i vraca rezultat funkcije // provjera : da private static double rezultat_funkcije( Vector tokeni, int pozicija_funkcije ) { double rezultat=0; double operand; Token t; String fn; t=(Token)tokeni.elementAt( pozicija_funkcije ); fn=((Funkcija)t.token).funkcija; t=(Token)tokeni.elementAt( pozicija_funkcije+2 ); operand=vrijednost_operanda( t ); // 1. Trigonometric if ( fn.equals("sin") ) rezultat=Math.sin( stupnjevi_ulaz( operand ) ); else if ( fn.equals("cos") ) rezultat=Math.cos( stupnjevi_ulaz( operand ) ); else if ( fn.equals("tan") ) rezultat=Math.tan( stupnjevi_ulaz( operand ) ); else if ( fn.equals("cot") ) rezultat=Matematika.cot( stupnjevi_ulaz( operand ) ); else if ( fn.equals("sec") ) rezultat=Matematika.sec( stupnjevi_ulaz( operand ) ); else if ( fn.equals("csc") ) rezultat=Matematika.csc( stupnjevi_ulaz( operand ) ); // 2. Inverse trigonometric else if ( fn.equals("asin") ) rezultat=stupnjevi_izlaz( Math.asin( operand ) ); else if ( fn.equals("acos") ) rezultat=stupnjevi_izlaz( Math.acos( operand ) ); else if ( fn.equals("atan") ) rezultat=stupnjevi_izlaz( Math.atan( operand ) ); else if ( fn.equals("atan2") ) { t=(Token)tokeni.elementAt( pozicija_funkcije+4 ); double operand1=vrijednost_operanda( t ); rezultat=Math.atan2( operand, operand1 ); } else if ( fn.equals("acot") ) rezultat=stupnjevi_izlaz( Matematika.acot( operand ) ); else if ( fn.equals("asec") ) rezultat=stupnjevi_izlaz( Matematika.asec( operand ) ); else if ( fn.equals("acsc") ) rezultat=stupnjevi_izlaz( Matematika.acsc( operand ) ); // 3. Hyperbolic if ( fn.equals("sinh") ) rezultat=Matematika.sinh( operand ); else if ( fn.equals("cosh") ) rezultat=Matematika.cosh( operand ); else if ( fn.equals("tanh") ) rezultat=Matematika.tanh( operand ); else if ( fn.equals("coth") ) rezultat=Matematika.coth( operand ); else if ( fn.equals("sech") ) rezultat=Matematika.sech( operand ); else if ( fn.equals("csch") ) rezultat=Matematika.csch( operand ); // 4. Inverse hyperbolic else if ( fn.equals("asinh") ) rezultat=Matematika.asinh( operand ); else if ( fn.equals("acosh") ) rezultat=Matematika.acosh( operand ); else if ( fn.equals("atanh") ) rezultat=Matematika.atanh( operand ); else if ( fn.equals("acoth") ) rezultat=Matematika.acoth( operand ); else if ( fn.equals("asech") ) rezultat=Matematika.asech( operand ); else if ( fn.equals("acsch") ) rezultat=Matematika.acsch( operand ); // 5. Exponential else if ( fn.equals("log10") ) rezultat=Matematika.log10( operand ); else if ( fn.equals("log2") ) rezultat=Matematika.log2( operand ); else if ( fn.equals("log") ) rezultat=Math.log( operand ); else if ( fn.equals("exp10") ) rezultat=Matematika.exp10( operand ); else if ( fn.equals("exp2") ) rezultat=Matematika.exp2( operand ); else if ( fn.equals("exp") ) rezultat=Math.exp( operand ); else if ( fn.equals("sqrt") ) rezultat=Math.sqrt( operand ); else if ( fn.equals("cur") ) rezultat=Matematika.cur( operand ); // 6. Combinatoric else if ( fn.equals("comb") ) { long operand2; t=(Token)tokeni.elementAt( pozicija_funkcije+4 ); operand2=(long)vrijednost_operanda( t ); rezultat=Matematika.comb( (long)operand, operand2 ); } else if ( fn.equals("combr") ) { long operand2; t=(Token)tokeni.elementAt( pozicija_funkcije+4 ); operand2=(long)vrijednost_operanda( t ); rezultat=Matematika.combr( (long)operand, operand2 ); } else if ( fn.equals("perm") ) rezultat=Matematika.perm( (long)operand ); else if ( fn.equals("permr") ) { long operand2; t=(Token)tokeni.elementAt( pozicija_funkcije+4 ); operand2=(long)vrijednost_operanda( t ); rezultat=Matematika.permr( (long)operand, operand2 ); } else if ( fn.equals("var") ) { long operand2; t=(Token)tokeni.elementAt( pozicija_funkcije+4 ); operand2=(long)vrijednost_operanda( t ); rezultat=Matematika.var( (long)operand, operand2 ); } else if ( fn.equals("varr") ) { long operand2; t=(Token)tokeni.elementAt( pozicija_funkcije+4 ); operand2=(long)vrijednost_operanda( t ); rezultat=Matematika.varr( (long)operand, operand2 ); } // 7. Statistical else if ( fn.equals("sum") ) rezultat = Matematika.sum( tokeni, pozicija_funkcije ); else if ( fn.equals("avg") ) rezultat = Matematika.avg( tokeni, pozicija_funkcije ); else if ( fn.equals("min") ) rezultat = Matematika.min( tokeni, pozicija_funkcije ); else if ( fn.equals("max") ) rezultat = Matematika.max( tokeni, pozicija_funkcije ); else if ( fn.equals("stddev") ) rezultat = Matematika.stddev( tokeni, pozicija_funkcije ); else if ( fn.equals("count") ) rezultat = Matematika.count( tokeni, pozicija_funkcije ); // 8. Other else if ( fn.equals("abs") ) rezultat=Math.abs( operand ); else if ( fn.equals("ceil") ) rezultat=Math.ceil( operand ); else if ( fn.equals("fact") ) rezultat=Matematika.fact( (long)operand ); else if ( fn.equals("floor") ) rezultat=Math.floor( operand ); else if ( fn.equals("pow") ) { double operand2; t=(Token)tokeni.elementAt( pozicija_funkcije+4 ); operand2=vrijednost_operanda( t ); // rezultat=Math.pow( operand, operand2 ); rezultat=Matematika.potenciranje( operand, operand2 ); } else if ( fn.equals("random") ) rezultat=Math.random() * operand; else if ( fn.equals("round") ) { byte operand2; t=(Token)tokeni.elementAt( pozicija_funkcije+4 ); operand2=(byte)vrijednost_operanda( t ); String s=Zaokruzivanje.round( formatirani_broj( operand ), operand2 ); rezultat=Tokenizacija.string_to_double( s ); } else if ( fn.equals("trunc") ) { byte operand2; t=(Token)tokeni.elementAt( pozicija_funkcije+4 ); operand2=(byte)vrijednost_operanda( t ); String s=Zaokruzivanje.trunc( formatirani_broj( operand ), operand2 ); rezultat=Tokenizacija.string_to_double( s ); } else if ( fn.equals("sign") ) rezultat=Matematika.sign( operand ); else if ( fn.equals("frac") ) rezultat=Matematika.frac( operand ); else if ( fn.equals("hypot") ) { double operand2; t=(Token)tokeni.elementAt( pozicija_funkcije+4 ); operand2=vrijednost_operanda( t ); rezultat=Matematika.hypot( operand, operand2 ); } else if ( fn.equals("deg") ) rezultat=Math.toDegrees ( operand ); else if ( fn.equals("rad") ) rezultat=Math.toRadians ( operand ); return rezultat; } // provjera : da public static Vector rezultat( Vector tokeni ) { Vector v = new Vector(); String medurezultat; izbacivanje_visestrukih_zagrada( tokeni ); for ( int j=0; j<tokeni.size(); j++ ) izbacivanje_zagrada( tokeni, j ); // postepeno racunanje izraza do { medurezultat = spoj_tokena( tokeni ); v.addElement( medurezultat ); } while ( korak_izracunavanja( tokeni ) ); Token t=(Token)tokeni.elementAt( 0 ); if ( t.oznaka!='D' ) { double x=vrijednost_operanda( t ); tokeni.setElementAt( new Token( new Double(x), 'D', 0, 0 ), 0 ); v.setElementAt( spoj_tokena( tokeni ), 0 ); } return v; } // provjera : da public static Vector ispis_tokena( Vector tokeni ) { Vector rezultat=new Vector(); String izraz=null; Token t; for ( int i=0; i<tokeni.size(); i++ ) { t=(Token)tokeni.elementAt( i ); izraz=(t.token).toString(); if ( izraz.endsWith( ".0" ) ) izraz=izraz.substring( 0, izraz.length()-2 ); izraz=izraz + " ; " + Provjera_izraza.naziv_tokena( t.oznaka ); rezultat.addElement( izraz ); izraz=null; } return rezultat; } public static int dodjeljivanje( String s ) { int i=0; try { while ( Character.isDigit( s.charAt( i ) ) || Character.isUpperCase( s.charAt( i ) ) || Character.isWhitespace( s.charAt( i ) )) i++; if ( s.charAt( i )=='=' && s.charAt( i+1 )!='=' && Character.isUpperCase( s.charAt( 0 ) ) ) return i; } catch ( Exception e ) { return 0; } return 0; } } // kraj klase
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -