JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 程式分享區 » Web  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modeno previous topicgo to next topic
本主題所含的標籤
作者 算式運算器(beta)
roytsang





發文: 618
積分: 1
於 2011-06-14 10:14 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這是我多年的夢想,想寫一個算式運算器,最近寫了一個,暫時只支援+-*/ 和^(次方),未支援括孤,
希望大家試試玩,看看有沒有bug,如果沒有問題,我才繼續。
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<html>
  <body>
    <script type="text/javascript" src="Vector.js"></script>
    <script language=javascript>
      function Calculator(x)
      {
        var self=this,myX=x;
        var result="";
        var debugArea=document.getElementById("debug");
        var debug=true;
        var operators=new Vector();
        var operands=new Vector();
        if ((x==null) || (x==""))
        {
          throw "The entered expression is empty or null.";
        }
        else 
        {  
          if (hasInvalidCharacter(x))
          {
            throw "The entered expression contains invalid character.";
          }
          else
          {
            result="";
            debugArea.innerHTML="";
            parseExpression();
          }
        }
        function calculatePower()
        {
          var s,i,op1,op2,op;
          s=operators.getSize();
          for (i=0;i<s;i++)
          {
            op=operators.getElementAt(i);
            if (op.indexOf("^")>-1)
            {
              op1=operands.getElementAt(i);
              op2=operands.getElementAt(i+1);
              if ((op=="^") || (op=="^+"))
              {
                op1=Math.pow(op1,op2);
              }
              else
              {
                op1=Math.pow(op1,-op2);
              }
              operands.overwriteElementAt(op1,i);
              operands.removeElementAt(i+1);
              operators.removeElementAt(i);
              break;
            }
          }          
        }
        function calculateTimeDiv()        
        {
          var s,i,op1,op2,op;
          s=operators.getSize();
          for (i=0;i<s;i++)
          {
            op=operators.getElementAt(i);
            if ((op=="*")||(op=="/")||(op=="*-")||(op=="*+")||(op=="/-")||(op=="/+"))
            {
              op1=operands.getElementAt(i);
              op2=operands.getElementAt(i+1);
              switch (op)
              {
                case "*" :
                case "*+":op1=op1*op2;
                      break
                case "*-":op1=op1* (-op2);
                      break        
                case "/" :
                case "/+":op1=op1/op2;
                      break
                case "/-":op1=op1/(-op2);
                      break              
              }
              operands.overwriteElementAt(op1,i);
              operands.removeElementAt(i+1);
              operators.removeElementAt(i);
              break;
            }
          }          
        }
        function calculateAddSub()
        {
          var s,i,op1,op2,op;
          s=operators.getSize();
          for (i=0;i<s;i++)
          {
            op=operators.getElementAt(i);
            if ((op=="+")||(op=="-"))
            {
              op1=operands.getElementAt(i);
              op2=operands.getElementAt(i+1);
              if (op=="+") 
              {
                op1=op1*1.0+op2*1.0;
              }
              else
              {
                op1=op1-op2;
              }
              operands.overwriteElementAt(op1,i);
              operands.removeElementAt(i+1);
              operators.removeElementAt(i);
              break;
            }
          }          
        }
        this.calculate=function()
        {
          var op1,op2,oper;
          while (operators.contains("^")||operators.contains("^+")||operators.contains("^-"))
          {
            calculatePower();
            genResult();
          }
          while (operators.contains("/")||operators.contains("/+")||operators.contains("/-")||operators.contains("*")||operators.contains("*+")||operators.contains("*-"))
          {
            calculateTimeDiv();
            genResult();
          }
          while (operators.contains("+")||operators.contains("-"))
          {
            calculateAddSub();
            genResult();
          }
        }
        function parseExpression()
        {
          var epar=/[0-9]+\.?[0-9]*(e\+[0-9]*|e-[0-9]*|e[0-9]*)?/gi;
          var operatorList="+-/*^";
          var preIndex=-1,temp="",temp1="",temp2,counter=0;
          var tempOperands=myX.match(epar),op;
          
          //remove all operands for the expression string
          temp=myX;
          for (i=0;i<tempOperands.length;i++)
          {
            l=tempOperands[i].length;
            operands.insertElementAt(tempOperands[i],i);
            //result+=tempOperands[i]+"\n";
            temp1="";
            for (j=0;j<l;j++)
              temp1+=" ";
            temp=temp.replace(tempOperands[i],temp1);  
          }
 
          //remove all operator for the expression string
          temp2=temp;
          temp=temp.replace(/\+/g,"");
          temp=temp.replace(/-/g,"");
          temp=temp.replace(/\//g,"");
          temp=temp.replace(/\*/g,"");
          temp=temp.replace(/\^/g,"");
          temp=temp.replace(/ /g,"");
          
          if (temp=="")
          {
            for (i=0;i<temp2.length;i++)
            {
              if (operatorList.indexOf(temp2.substr(i,1))>-1)
              {
                op=temp2.substr(i,1);
                if (i==0)
                {
                  operands.overwriteElementAt(op+operands.getElementAt(0),0);
                  preIndex=i;
                }
                else
                {
                  if ((i-preIndex)>1)
                  {  
                    operators.insertElementAt(temp2.substr(i,1),counter);
                    preIndex=i;
                    counter++;  
                  }
                  else
                  {
                    temp=temp2.substr(preIndex,1)+temp2.substr(i,1);
                    
                    switch (temp)
                    {
                      case "-+":
                      case "+-":operators.overwriteElementAt("-",counter-1);
                            break;
                      case "++":
                      case "--":operators.overwriteElementAt("+",counter-1);  
                            break;
                      case "^+":
                      case "^-":
                      case "*-":
                      case "*+":
                      case "/-":
                      case "/+":operators.overwriteElementAt(temp,counter-1);
                            break;
                      default  :throw "The entered expression contains invalid operator sequence.";
                            break;                   
                    }
                  }
                }                    
              }    
            }
            if (operands.getSize()-operators.getSize()!=1)
            {
              throw "The entered expression contains invalid operator count.";
            }
            else
              genResult();
          }
          else
          {
            if (debug)
              debugArea.innerHTML+="temp="+temp+"<br>";
            throw "There is something wrong in entered expression.";
          }                  
        }
        this.getResult=function()
        {
          return result;
        }
        function genResult()
        {
          j=0;
          for (i=0;i<operators.getSize();i++)
          {
            result+=operands.getElementAt(i)+operators.getElementAt(i);
          }
          result+=operands.getElementAt(i)+",";
          
        }
        
        function hasInvalidCharacter(j)
        {
          var hasInvalidCharacter=false;
          var apar=/^[\d+-]/gi;
          if (!apar.test(j))
          {
            hasInvalidCharacter=true;
            if (debug)
              debugArea.innerHTML+="apar.test failed<br>";
          }
          var bpar=/[a-d,f-z]/gi; //contain a-d and f-z character
          if ((!hasInvalidCharacter) && bpar.test(j))
          {
            hasInvalidCharacter=true;
            if (debug)
              debugArea.innerHTML+="bpar.test failed<br>";
          }
          var cpar=/^[+-]\D/gi;   //starts with +- and followed with non-digit character
          if ((!hasInvalidCharacter) && cpar.test(j))
          {
            hasInvalidCharacter=true;
            if (debug)
              debugArea.innerHTML+="cpar.test failed";
          }
          var dpar=/\.\D+/i;      //decimal point followed with non-digit character
          if ((!hasInvalidCharacter) && dpar.test(j))
          {
            hasInvalidCharacter=true;
            if (debug)
              debugArea.innerHTML+="dpar.test failed<br>";
          }          
          var epar=/e$/i;      //not ends with "E"
          if ((!hasInvalidCharacter) && epar.test(j))
          {
            hasInvalidCharacter=true;
            if (debug)
              debugArea.innerHTML+="epar.test failed<br>";
          }
          var fpar=/\De\?/i;    //non digit character follow the character "e";
          if ((!hasInvalidCharacter) && fpar.test(j))
          {
            hasInvalidCharacter=true;
            if (debug)
              debugArea.innerHTML+="fpar.test failed<br>";
          }          
          return (hasInvalidCharacter);
          
        }            
      }  
      function getResult(v)
      {
        var myExp=v.expression.value;
        v.expression.value=myExp;
        var resultArea=document.getElementById("resultArea");
        var i,c,calculationResult,finishObject=document.createElement("sup");
        try
        {
          c=new Calculator(myExp);
          resultArea.innerHTML=myExp+"<br><hr>";
          c.calculate();
          calculationResult=c.getResult().split(",");
          for (i=0;i<calculationResult.length-2;i++)
            resultArea.innerHTML+="="+calculationResult[i]+"<br>";
          resultArea.innerHTML+="=<span style='border-bottom: 3px double;'>"+calculationResult[i]+"</span>";
          c=null;
        }
        catch (er)
        {
          resultArea.innerHTML="<font color=red>"+er+"</font><br>";
        }    
      }            
    </script>
    Arithmetic Expression Solver
    
    <form action="" method="post" onsubmit="getResult(this);return false;">
      <table>
        <tr>
           <td>Please input the expression</td>
        </tr>
        <tr>
          <td>    
          <input type=text value="1*0.3+2^6/8e+0-5.9+3-4" name="expression">
          <input type=submit value="Get Result">
        </td>
        </tr>      
       </table> 
    </form>
    <div id="resultArea">
    </div>
    <div id="debug">
    </div>
  </body>
</html>          
 


Vector.js (8.26k)


roytsang edited on 2011-06-14 10:20
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
4082 算式運算器(beta) roytsang 10879 2011-06-14 10:14
3624 Re:算式運算器(beta) ace33022 38 2011-06-14 21:28
3839 Re:算式運算器(beta) tkcn 354 2011-06-15 14:00
3664 Re:算式運算器(beta) roytsang 99 2011-06-15 16:28
3446 Re:算式運算器(beta) roytsang 161 2011-07-06 14:05
3472 Re:算式運算器(beta) asdfvg0000 140 2011-07-06 15:22
3435 Re:算式運算器(beta) roytsang 55 2011-07-07 09:16
3432 Re:算式運算器(beta) jfire 29 2011-07-07 10:19
3390 Re:算式運算器(beta) roytsang 52 2011-07-07 12:17
3372 Re:算式運算器(beta) roytsang 4 2011-07-07 13:36
3443 Re:算式運算器(beta) roytsang 13 2011-07-07 15:05
2942 Re:算式運算器(beta) roytsang 77 2012-08-30 22:10
1067 Re:算式運算器(beta) jack850628 32 2016-12-19 09:40
1130 Re:算式運算器(beta) roytsang 20 2016-12-19 09:52
494 Re:算式運算器(beta) roytsang 74 2018-11-07 14:51
476 Re:算式運算器(beta) caterpillar 154 2018-11-09 10:00
452 Re:算式運算器(beta) roytsang 453 2018-11-09 16:55
453 Re:算式運算器(beta) caterpillar 196 2018-11-10 00:52
432 Re:算式運算器(beta) roytsang 346 2018-11-29 08:53
485 Re:算式運算器(beta) javaX 9 2018-11-30 03:19
574 Re:算式運算器(beta) yjy198410 196 2018-11-11 03:50
» JWorld@TW »  Java 程式分享區 » Web

reply to postflat modeno previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8