⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parsefunction.java

📁 java 作图的程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
             if(x == null) throw new Exception("X Array error");             double array[] = new double[n];             for(int i=0; i<n; i++) {                 this.x = x[i];                 array[i] = evaluate(root);             }             return array;        }  /**   * Return an array of solutions given an array of x values and y values   * @param n number of values to process in the input array   * @param x Array containing the x values.   * @param y Array containing the y values.   * @return Array containing the solutions.   * @exception Exception   *       Generic exception if the array index n<=0, or x is null, or y is null.   */        public double[] getResults(int n, double x[], double y[])                                     throws Exception {             if(n <= 0) throw new Exception("Array index error");             if(x == null) throw new Exception("X Array error");             if(y == null) throw new Exception("Y Array error");             double array[] = new double[n];             for(int i=0; i<n; i++) {                 this.x = x[i];                 this.y = y[i];                 array[i] = evaluate(root);             }             return array;        }  /**   * Return an array of solutions given an array of x values, y values   * and z values.   * @param n number of values to process in the input array   * @param x Array containing the x values.   * @param y Array containing the y values.   * @return Array containing the solutions.   * @exception Exception   *      Generic exception if the array index n<=0, or x is null,    *    or y is null, or z is null.   */        public double[] getResults(int n, double x[], double y[], double z[])                                     throws Exception {             if(n <= 0) throw new Exception("Array index error");             if(x == null) throw new Exception("X Array error");             if(y == null) throw new Exception("Y Array error");             if(z == null) throw new Exception("Z Array error");             double array[] = new double[n];             for(int i=0; i<n; i++) {                 this.x = x[i];                 this.y = y[i];                 this.z = z[i];                 array[i] = evaluate(root);             }             return array;        }  /**   * Return a boolean array with index 0 true if the independent   * variable x was found in the function, index 1 true if   * y was found, and index 2  true if z was found.   */        public boolean[] getVariables()  {        	boolean b[] =  new boolean[3];        	        	b[0] = independent_x;        	b[1] = independent_y;        	b[2] = independent_z;            return b;        }  /**   * Set the value of the independent variable X.   */        public void setX( double x ) { this.x = x; }   /**   * Set the value of the independent variable Y.   */        public void setY( double y ) { this.y = y; }   /**   * Set the value of the independent variable Z.   */        public void setZ( double z ) { this.z = z; } /************************ Private Methods********************/  /*  ** Parse the string and build the node link list.  ** This builds up a simple Left-Right binary node tree.  **  ** A GROUP token (start of parenthesis) forces a new group node  ** that starts an independent branch of the tree with the contents  ** of the group linked to the left node of the group node.  ** Intrinsic functions behave like a GROUP token they start an independent  ** branch of the tree with the contents of the group linked to the  ** left node of the intrinsic function.  ** Intrinsic functions that have to be passed 2 parameters have the  ** second parameter linked to there right node.  **  (This has to be modified for functions containing more than 2 parameters  **  currently the code cannot deal with more than 2 parameters)  */        private int parseString(Node node) {        Node left;        Node right;        int token;        int t;        // Get the next token in the string        token = nextWord();        // Do some preliminary branching.        if(token == ERROR) {             System.out.println("Error parsing \""+sval+"\"");             return ERROR;        } else         if( token != EOS && debug ) {             System.out.println("Parse: "+sval+"\t Token: "+token);        } else         if( token == EOS && debug ) {             System.out.println("Parse: EOS");        }                // Main token switch        switch(token) {        		  /*	  ** Number token or constant tokens.          ** Place the value in the node and recurse on this          ** terminal node. It will be used by an operator.          */        case NUMBER:       	       node.type  = Node.VALUE;       	       node.value = nval;               return parseString(node);       	               case PI:      	       node.type = Node.VALUE;      	       node.value = Math.PI;               return parseString(node);      	               case E:      	       node.type = Node.VALUE;      	       node.value = Math.E;      	       return parseString(node);        case BOLTZMAN:      	       node.type = Node.VALUE;      	       node.value =             SpecialFunction.BOLTZMAN;      	       return parseString(node);        case ECHARGE:      	       node.type = Node.VALUE;      	       node.value =             SpecialFunction.ECHARGE;      	       return parseString(node);        case EMASS:      	       node.type = Node.VALUE;      	       node.value =             SpecialFunction.EMASS;      	       return parseString(node);        case PMASS:      	       node.type = Node.VALUE;      	       node.value =             SpecialFunction.PMASS;      	       return parseString(node);        case GRAV:      	       node.type = Node.VALUE;      	       node.value =             SpecialFunction.GRAV;      	       return parseString(node);        case PLANCK:      	       node.type = Node.VALUE;      	       node.value =             SpecialFunction.PLANCK;      	       return parseString(node);        case LIGHTSPEED:      	       node.type = Node.VALUE;      	       node.value =             SpecialFunction.LIGHTSPEED;      	       return parseString(node);        case STEFANBOLTZ:      	       node.type = Node.VALUE;      	       node.value =             SpecialFunction.STEFANBOLTZ;      	       return parseString(node);        case AVOGADRO:      	       node.type = Node.VALUE;      	       node.value =             SpecialFunction.AVOGADRO;      	       return parseString(node);        case GASCONSTANT:      	       node.type = Node.VALUE;      	       node.value =             SpecialFunction.GASCONSTANT;      	       return parseString(node);        case GRAVACC:      	       node.type = Node.VALUE;      	       node.value =             SpecialFunction.GRAVACC;      	       return parseString(node);      	case RAD:      	       node.type = Node.VALUE;      	       node.value = Math.PI/180.0;      	       return parseString(node);	case RANDOM:      	       node.type = Node.VALUE;      	       node.value = Math.random();      	       return parseString(node);	 /*	 ** Independent variables behave like constant nodes except         ** they are flagged as INDEPENDENT nodes. Then we recurse using this         **  this node as it has to be used by an operator         */        case X: case Y: case Z:      	       node.op   = token;      	       node.type = Node.INDEPENDENT;      	             	       if(token == X) independent_x = true;      	       else      	       if(token == Y) independent_y = true;      	       else      	       if(token == Z) independent_z = true;      	             	       return parseString(node);      	       	/*	** Terminal tokens        */	case ENDGROUP: case EOS: case COMMA:               break;       /*       ** beginning of a group '('. Parse the string until the       ** corresponding endgroup is encountered ')'. The created       ** node list is attached to the group nodes left       ** node. Then continue the process by continuing to parse       ** the string after the endgroup.       */        case GROUP:               left = new Node();               if(parseString(left) == ENDGROUP) {                    node.left = left;                    node.type = Node.GROUP;                    node.precedence = Node.P5;                    token = parseString(node);                } else {                    System.out.println("Parse Failed: missing parentheses");                    token = ERROR;                }                break;	/*	** Binary and Unary Operators.        ** The existing node goes to the left and everything        ** on the right gets attached to the right node.        **        ** A unary operator is recognised by the empty        ** node parsed ie nothing exists on the left.        */	case ADD: case SUBTRACT: case MULTIPLY: case DIVIDE: case POWER:               right = new Node();               t = parseString(right);               if(t != ERROR) { 		   if( (token == SUBTRACT || token == ADD )                                       && node.type == Node.NULL ) {                      //System.out.println("...Unary Operator");                                            node.right = right;                      node.precedence = Node.P4;                      node.op = token;                      node.type = Node.OP;		   } else {                      //System.out.println("...Binary Operator");                      left = new Node(node);                      node.left = left;                      node.right = right;                      node.op = token;                      node.type = Node.OP;                      switch (token) {		         case ADD: case SUBTRACT:                            node.precedence = Node.P1;                            break;                         case MULTIPLY: case DIVIDE:                            node.precedence = Node.P2;                            break;                         case POWER:                            node.precedence = Node.P3;                            break;                       }                   }	       }               token = t;                              break;	/*	** Single parameter intrinsic functions behave excacty like        ** parenthesis.        */               case SIN:   case COS:   case TAN:         case ASIN:  case ACOS:  case ATAN:        case LOG:   case SQRT:  case LOG10:        case EXP:   case REMAINDER:         case J0:    case J1: case Y0: case Y1:        case SINH:  case COSH:  case TANH:        case ASINH: case ACOSH: case ATANH:        case FAC:   case GAMMA: case ERF: case ERFC:        case NORMAL:                 node.op = token;                node.type = Node.INTRINSIC;                node.precedence = Node.P0;                token = nextWord();                if(token != GROUP ) {                     System.out.println(                    "Parse Failed: intrinsic function is missing \"(\"");                    token = ERROR;                } else {                   left = new Node();                   if(parseString(left) == ENDGROUP) {                       node.left = left;                       token = parseString(node);                   } else {                       System.out.println(                       "Parse Failed: intrinsic function is missing \")\"");                       token = ERROR;                   }                }                break;	/*	** 2 parameter intrinsic functions        ** First parameter on attached to the left node,         **  second parameter attached to the right.        */	case ATAN2: case JN: case YN: case IGAM: case IGAMC:        case CHISQ: case CHISQC: case POISSON: case POISSONC:                node.op = token;                node.type = Node.INTRINSIC;                node.precedence = Node.P0;                token = nextWord();                if(debug) System.out.println("Parse: "+sval);                if(token != GROUP ) {                     System.out.println(                    "Parse Failed: intrinsic function is missing \"(\"");                    token = ERROR;                } else {                   Node param1 = new Node();                   if(parseString(param1) == COMMA) {                       Node param2 = new Node();                       if(parseString(param2) == ENDGROUP) {                          node.right = param2;                          node.left  = param1;                          token = parseString(node);                       } else {                          System.out.println(                          "Parse Failed: intrinsic function is missing \")\"");                          token = ERROR;                       }                   } else {                          System.out.println(                          "Parse Failed: intrinsic function is missing \",\"");                          token = ERROR;		   }                                      }                break;         default:                break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -