1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | procedure Eval(Formula: string; { 要計算的表達式 } var Value: Real; { 返回數值 } var ErrPos: Integer); { 錯誤信息 } const Digit: set of Char = [ '0' .. '9' ]; var Posn: Integer; { 算式當前位置 } CurrChar: Char; { 算式當前字符 } procedure ParseNext; begin repeat Posn := Posn + 1; if Posn <= Length(Formula) then CurrChar := Formula[Posn] else CurrChar := ^M; until CurrChar <> ' ' ; end { ParseNext }; function add_subt: Real; var E: Real; Opr: Char; function mult_DIV: Real; var S: Real; Opr: Char; function Power: Real; var T: Real; function SignedOp: Real; function UnsignedOp: Real; type StdFunc = ( fabs , fsqrt, fsqr, fsin, fcos, farctan, fln, flog, fexp, ffact); StdFuncList = array[StdFunc] of string[6]; const StdFuncName: StdFuncList = ( 'ABS' , 'SQRT' , 'SQR' , 'SIN' , 'COS' , 'ARCTAN' , 'LN' , 'LOG' , 'EXP' , 'FACT' ); var E, L, Start: Integer; Funnet: Boolean; F: Real; Sf: StdFunc; function Fact(I: Integer): Real; begin if I > 0 then begin Fact := I * Fact(I - 1); end else Fact := 1; end { Fact }; begin if CurrChar in Digit then begin Start := Posn; repeat ParseNext until not (CurrChar in Digit); if CurrChar = '.' then repeat ParseNext until not (CurrChar in Digit); if CurrChar = 'E' then begin ParseNext; repeat ParseNext until not (CurrChar in Digit); end; Val(Copy(Formula, Start, Posn - Start), F, ErrPos); end else if CurrChar = '(' then begin ParseNext; F := add_subt; if CurrChar = ')' then ParseNext else ErrPos := Posn; end else begin Funnet := False; for sf := fabs to ffact do if not Funnet then begin l := Length(StdFuncName[sf]); if Copy(Formula, Posn, l) = StdFuncName[sf] then begin Posn := Posn + l - 1; ParseNext; f := UnsignedOp; case sf of fabs : f := abs (f); fsqrt: f := SqrT(f); fsqr: f := Sqr(f); fsin: f := Sin(f); fcos: f := Cos(f); farctan: f := ArcTan(f); fln: f := LN(f); flog: f := LN(f) / LN(10); fexp: f := EXP(f); ffact: f := fact(Trunc(f)); end; Funnet := True; end; end; if not Funnet then begin ErrPos := Posn; f := 0; end; end; UnsignedOp := F; end { UnsignedOp}; begin { SignedOp } if CurrChar = '-' then begin ParseNext; SignedOp := -UnsignedOp; end else SignedOp := UnsignedOp; end { SignedOp }; begin { Power } T := SignedOp; while CurrChar = '^' do begin ParseNext; if t <> 0 then t := EXP(LN( abs (t)) * SignedOp) else t := 0; end; Power := t; end { Power }; begin s := Power; while CurrChar in [ '*' , '/' ] do begin Opr := CurrChar; ParseNext; case Opr of '*' : s := s * Power; '/' : s := s / Power; end; end; mult_DIV := s; end; begin E := mult_DIV; while CurrChar in [ '+' , '-' ] do begin Opr := CurrChar; ParseNext; case Opr of '+' : e := e + mult_DIV; '-' : e := e - mult_DIV; end; end; add_subt := E; end; begin if Formula[1] = '.' then Formula := '0' + Formula; if Formula[1] = '+' then Delete(Formula, 1, 1); for Posn := 1 to Length(Formula) do Formula[Posn] := Upcase(Formula[Posn]); Posn := 0; ParseNext; Value := add_subt; if CurrChar = ^M then ErrPos := 0 else ErrPos := Posn; end; |
2012/06/13
[轉]Delphi实现Js中的Eval函数
原文網址:Delphi实现Js中的Eval函数
2012/06/10
訂閱:
文章 (Atom)