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

📄 mathmodule.java

📁 类似于Windows上的Excel
💻 JAVA
字号:
/*
 * MC2 -- j2me spreadsheet
 *
 * Copyright (c) 2004-2006 Michael Zemljanukha (mixaz@mail.ru)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package com.wapindustrial.calc;

import ral.Real;

/*
 * Math module, includes strings ops, date, etc
 *
 * Created on December 25, 2003, 11:18 AM
 */

public class MathModule extends FunctionModule {

    // basic finction set, implemented in root code FunctionObject
    public static final MathModule MATHMODULE = new MathModule();

    private static final FloatAtom aPI = new FloatAtom( Real.PI );
    private static final FloatAtom aE = new FloatAtom( Real.E );
        
    private static final String _namesArray = 
            "\02PI" +
            "\01E" +
            "\04fadd" +
            "\04fsub" +
            "\04fmul" +
            "\04fdiv" +
            "\03pow" +
            "\04acos" +
            "\04asin" +
            "\04atan" +
            "\03cos" +
            "\03sin" +
            "\03cot" +
            "\03exp" +
            "\03log" +
            "\04sqrt" +
            "\03tan" +
            "\06str-eq" +
            "\07str-len" +
            "\05round" +
            "\06totype" +
            "\07str-cat" +
            "\07str-sub" +
            "\10str-find" +
            "\03sum"
    ;
    
    private static final int NAME_PI = 0;
    private static final int NAME_E = NAME_PI + 2 + 1;
    private static final int NAME_FADD = NAME_E + 1 + 1;
    private static final int NAME_FSUB = NAME_FADD + 4 + 1;
    private static final int NAME_FMUL = NAME_FSUB + 4 + 1;
    private static final int NAME_FDIV = NAME_FMUL + 4 + 1;
    private static final int NAME_POW = NAME_FDIV + 4 + 1;
    private static final int NAME_ACOS = NAME_POW + 3 + 1;
    private static final int NAME_ASIN = NAME_ACOS + 4 + 1;
    private static final int NAME_ATAN = NAME_ASIN + 4 + 1;
    private static final int NAME_COS = NAME_ATAN + 4 + 1;
    private static final int NAME_SIN = NAME_COS + 3 + 1;
    private static final int NAME_COT = NAME_SIN + 3 + 1;
    private static final int NAME_EXP = NAME_COT + 3 + 1;
    private static final int NAME_LOG = NAME_EXP + 3 + 1;
    private static final int NAME_SQRT = NAME_LOG + 3 + 1;
    private static final int NAME_TAN = NAME_SQRT + 4 + 1;
    private static final int NAME_STR_EQ = NAME_TAN + 3 + 1;
    private static final int NAME_STR_LEN = NAME_STR_EQ + 6 + 1;
    private static final int NAME_ROUND = NAME_STR_LEN + 7 + 1;
    private static final int NAME_TOTYPE = NAME_ROUND + 5 + 1;
    private static final int NAME_STR_CAT = NAME_TOTYPE + 6 + 1;
    private static final int NAME_STR_SUB = NAME_STR_CAT + 7 + 1;
    private static final int NAME_STR_FIND = NAME_STR_SUB + 7 + 1;
    private static final int NAME_SUM = NAME_STR_FIND + 8 + 1;
    
    public static final int INDEX_PI = 0;
    public static final int INDEX_E = INDEX_PI + 1;
    public static final int INDEX_FADD = INDEX_E + 1;
    public static final int INDEX_FSUB = INDEX_FADD + 1;
    public static final int INDEX_FMUL = INDEX_FSUB + 1;
    public static final int INDEX_FDIV = INDEX_FMUL + 1;
    public static final int INDEX_POW = INDEX_FDIV + 1;
    public static final int INDEX_ACOS = INDEX_POW + 1;
    public static final int INDEX_ASIN = INDEX_ACOS + 1;
    public static final int INDEX_ATAN = INDEX_ASIN + 1;
    public static final int INDEX_COS = INDEX_ATAN + 1;
    public static final int INDEX_SIN = INDEX_COS + 1;
    public static final int INDEX_COT = INDEX_SIN + 1;
    public static final int INDEX_EXP = INDEX_COT + 1;
    public static final int INDEX_LOG = INDEX_EXP + 1;
    public static final int INDEX_SQRT = INDEX_LOG + 1;
    public static final int INDEX_TAN = INDEX_SQRT + 1;
    public static final int INDEX_STR_EQ = INDEX_TAN + 1;
    public static final int INDEX_STR_LEN = INDEX_STR_EQ + 1;
    public static final int INDEX_ROUND = INDEX_STR_LEN + 1;
    public static final int INDEX_TOTYPE = INDEX_ROUND + 1;
    public static final int INDEX_STR_CAT = INDEX_TOTYPE + 1;
    public static final int INDEX_STR_SUB = INDEX_STR_CAT + 1;
    public static final int INDEX_STR_FIND = INDEX_STR_SUB + 1;
    public static final int INDEX_SUM = INDEX_STR_FIND + 1;
    
    public void initializeNames() {
        
        namesArray = _namesArray.getBytes();
        
        namesArrayCount = namesArray.length;
        
        table = new ModuleName[] {
                new ModuleNameData(NAME_PI,aPI),
                new ModuleNameData(NAME_E,aE),
                new ModuleName(NAME_FADD),
                new ModuleName(NAME_FSUB),
                new ModuleName(NAME_FMUL),
                new ModuleName(NAME_FDIV),
                new ModuleName(NAME_POW),
                new ModuleName(NAME_ACOS),
                new ModuleName(NAME_ASIN),
                new ModuleName(NAME_ATAN),
                new ModuleName(NAME_COS),
                new ModuleName(NAME_SIN),
                new ModuleName(NAME_COT),
                new ModuleName(NAME_EXP),
                new ModuleName(NAME_LOG),
                new ModuleName(NAME_SQRT),
                new ModuleName(NAME_TAN),
                new ModuleName(NAME_STR_EQ),
                new ModuleName(NAME_STR_LEN),
                new ModuleName(NAME_ROUND),
                new ModuleName(NAME_TOTYPE),
                new ModuleName(NAME_STR_CAT),
                new ModuleName(NAME_STR_SUB),
                new ModuleName(NAME_STR_FIND),
                new ModuleName(NAME_SUM)
        };
        
        namesCount = table.length;
    }
    
    public LispObject evaluate(ModuleName modulename, FunctorList sexp ) throws EvaluateException {
            int size = sexp.listSize();
            int optype = modulename.offset;
            switch( optype ) {
                // control ops
                // math floating point ops
                // 1 float -> float
                case NAME_ACOS:
                case NAME_ASIN:
                case NAME_ATAN: 
                case NAME_COS:
                case NAME_SIN:
                case NAME_COT:
                case NAME_EXP:
                case NAME_LOG:
                case NAME_SQRT:
                case NAME_TAN:
                {
                    Real ff3 = new Real(((FloatAtom)sexp.evaluateArg1()).value);
                    switch( optype ) {
                        case NAME_ACOS:
                            ff3.acos();
                            break;
                        case NAME_ASIN:
                            ff3.asin();
                            break;
                        case NAME_ATAN:
                            ff3.atan();
                            break;
                        case NAME_COS:
                            ff3.cos();
                            break;
                        case NAME_SIN:
                            ff3.sin();
                            break;
                        case NAME_COT:
                            ff3.tan();
                            ff3.recip();
                            break;
                        case NAME_EXP:
                            ff3.exp();
                            break;
                        case NAME_LOG:
                            ff3.ln();
                            break;
                        case NAME_SQRT:
                            ff3.sqrt();
                            break;
                        case NAME_TAN:
                            ff3.tan();
                            break;
                    }
                    return new FloatAtom( ff3 );
                }

                // 2 float -> float
                case NAME_FADD:
                case NAME_FSUB:
                case NAME_FMUL:
                case NAME_FDIV:
                case NAME_POW:
                {
                    Real ff3 = new Real(((FloatAtom)sexp.evaluateArg1()).value);
                    Real ff2 = ((FloatAtom)sexp.evaluateArg2()).value;
                    switch( optype ) {
                        case NAME_FADD:
                            ff3.add(ff2);
                            break;
                        case NAME_FSUB:
                            ff3.sub(ff2);
                            break;
                        case NAME_FMUL:
                            ff3.mul(ff2);
                            break;
                        case NAME_FDIV:
                            ff3.div(ff2);
                            break;
                        case NAME_POW:
                            ff3.pow(ff2);
                            break;
                    }
                    return new FloatAtom( ff3 );
                }
                
                case NAME_STR_EQ:
                {
                    String s1 = sexp.getString1();
                    String s2 = sexp.getString2();
                    return BooleanAtom.createBoolean( s1.compareTo( s2 ) == 0 );
                }
                case NAME_STR_LEN:
                {
                    return new ShortAtom( sexp.getString1().length() );
                }
                case NAME_STR_CAT:
                {
                    /* (str-cat string string) -> string */
                	StringBuffer sb = new StringBuffer();
                	for( int i=1; i<size; i++ )
                		sb.append(sexp.getString(i));
                    return new StringAtom( sb.toString() );
                }
                case NAME_STR_SUB:
                {
                    /* (str-sub string int int) -> string */
                    String s1 = sexp.getString1();
                    int s2 = sexp.getShort2();
                    int s3 = sexp.getShort3();
                    return new StringAtom( s1.substring( s2,s3 ) );
                }
                case NAME_STR_FIND:
                {
                    /* (str-find string string string) -> short */
                    String s1 = sexp.getString1();
                    String s2 = sexp.getString2();
                    return new ShortAtom( s1.indexOf( s2 ) );
                }
                case NAME_ROUND:
                {
                    /** (round float short) -> float */
                    Real f1 = new Real(((FloatAtom)sexp.getFloat(1)).value);
                    int s1 = sexp.getShort2();
                    Real rmove= new Real(10);
                    rmove.pow(s1);
                    f1.mul(rmove);
                    f1.round();
                    f1.div(rmove);
                    return new FloatAtom( f1 );
                }
                case NAME_TOTYPE:
                {
                    /** (totype obj type) -> obj */
                    LispObject lo = sexp.evaluateArg1();
                    int type = sexp.getShort2();
                    return lo.convertToType( type );
                }
//#ifndef NOGUI    
                case NAME_SUM:
                {
                    /** (reference obj type) -> obj */
                    LispObject rr = NIL;
                    for( int i=1; i<size; i++ ) {
                        LispObject arg = sexp.getArgumentN(i);
                        if( arg.typeNumber() == TYPE_REFERENCE ) {
                            Reference ref = (Reference) arg;
                            if( (ref.absFlags&Reference.BAD_REF)!=0 )
                                return FormulaError.REF_ERROR;
                            for( int ii=ref.i1; ii<=ref.i2; ii++ )
                                for( int jj=ref.j1; jj<=ref.j2; jj++ ) {
                                    LispObject cellvalue = CanvasHandler1.canvasHandler.getCellValue( ii, jj );
                                    int type = cellvalue.typeNumber();
                                    if( type == TYPE_SHORT || type == TYPE_LONG || type == TYPE_FLOAT ) {
                                        rr = new FunctorList2(Operator.OPERATOR.table[Operator.INDEX_ADD],rr,cellvalue).evaluateSExp();
                                    }
                                }
                        }
                        else {
                            arg = arg.evaluateSExp();
                            int type = arg.typeNumber();
                            if( type == TYPE_SHORT || type == TYPE_LONG || type == TYPE_FLOAT ) {
                                rr = new FunctorList2(Operator.OPERATOR.table[Operator.INDEX_ADD],rr,arg).evaluateSExp();
                            }
                        }
                    }
                    return rr;
                }
//#endif                
            }
            return NIL;
    }

//#ifdef JAVA_COMPILER     
    protected String getModuleClassName() {
        return "MathModule";
    }
//#endif

}

⌨️ 快捷键说明

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