使用原生的javascript进行字符的四则运算。

先讲解原理:利用数据结构的堆栈原理实现字符串的四则运算。
                目前测试出来,暂时还不支持负数。

以下就是代码:

  1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2 <html xmlns="http://www.w3.org/1999/xhtml">
  3 <head>
  4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5 <title>无标题文档</title>
  6 <script type="text/javascript">
  7 var expression = new Array();
  8 var postexpression = new Array();
  9 var opexpression = new Array();
 10 var stexpression = new Array();
 11 //计算字符串值
 12 var expc={
 13     ex:expression,
 14     index:0,
 15     totallength:0
 16     }
 17 //存放数值的数组
 18 var postexp={
 19     pe:postexpression,
 20     index:0,
 21     totallength:0
 22     }    
 23 //存放操作符的数组
 24 var op={
 25     ope:opexpression,
 26     top:-1
 27 }
 28 //从后缀表达式进行计算
 29 var st={
 30     data:stexpression,
 31     top:-1,
 32     result:0,
 33     numa:0,
 34     numb:0,
 35     tempresult:0,
 36     intnum:0,//存放整数部分
 37     decimals:0,   //存放小数部分
 38     point:''//存放小数点
 39     }
 40 //初始化字符串,将字符串存入到数组中
 41 function init(s){
 42   
 43       for(var i=0;i <s.length;i++)
 44       {
 45         expression[expression.length]=s.substr(i,1)
 46       }
 47       expc.totallength=s.length;
 48     while(expc.index<expc.totallength)
 49     {
 50        switch(expc.ex[expc.index])
 51        {
 52         case '(':
 53                 op.top++;
 54                 op.ope[op.top]=expc.ex[expc.index];
 55                 expc.index++;
 56                 break;
 57         case ')':
 58                 while(op.ope[op.top]!='(')
 59                    {
 60                     postexp.pe[postexp.index++]    =op.ope[op.top];
 61                     op.top--;
 62                     }
 63                     op.top--;
 64                     expc.index++;
 65                     break;
 66         case '+':
 67         case '-':
 68                 while(op.top!=-1&&op.ope[op.top]!='(')
 69                     {
 70                      postexp.pe[postexp.index++]=op.ope[op.top];
 71                      op.top--;                                
 72                     }
 73                     op.top++;
 74                     op.ope[op.top]= expc.ex[expc.index];
 75                     expc.index++;
 76                     break;
 77         case '*':
 78         case '/':
 79               while(op.ope[op.top]=="*"||op.ope[op.top]=="/")
 80               {
 81                   postexp.pe[postexp.index]=op.ope[op.top];
 82                   op.top--;
 83               }
 84               op.top++;
 85               op.ope[op.top]=expc.ex[expc.index];
 86               expc.index++;
 87               break;
 88         case ' ':break;
 89         default:
 90              while((expc.ex[expc.index]>='0'&&expc.ex[expc.index]<='9')||(expc.ex[expc.index]=='.'))
 91              {
 92                  postexp.pe[postexp.index++]=expc.ex[expc.index]
 93                  expc.index++;
 94              }
 95              postexp.pe[postexp.index++]='#';
 96        }//switch
 97     }//while
 98     while(op.top!=-1)
 99     {
100         postexp.pe[postexp.index++]=op.ope[op.top];
101         op.top--;
102     }
103     postexp.totallength= postexp.pe.length;
104     var s="";
105     
106      for (var i=0;i<postexp.totallength;i++)
107         s=s+postexp.pe[i];
108         expc.ex.length=0;
109         expc.index=0;
110         expc.totallength=0;
111         op.ope.length=0;
112         op.top=-1;
113        postexp.index=0;
114      //计算值。
115  
116     
117     while(postexp.index<postexp.totallength)
118     {
119        switch(postexp.pe[postexp.index])
120        {
121            case '+':
122               st.numa=st.data[st.top];
123               st.top--;
124               st.numb=st.data[st.top];
125               st.top--;
126               st.result=parseFloat(st.numa)+parseFloat(st.numb);
127               st.top++;
128               st.data[st.top]=st.result;
129               break;
130             case '-':
131               st.numa=st.data[st.top];
132               st.top--;
133               st.numb=st.data[st.top];
134               st.top--;
135               st.result=parseFloat(st.numb)-parseFloat(st.numa);
136               st.top++;
137               st.data[st.top]=st.result;
138               break;
139             case '*':
140                st.numa=st.data[st.top];
141               st.top--;
142               st.numb=st.data[st.top];
143               st.top--;
144               st.result=parseFloat(st.numb)*parseFloat(st.numa);
145               st.top++;
146               st.data[st.top]=st.result;
147               break;
148             case '/':
149               st.numa=st.data[st.top];
150               st.top--;
151               st.numb=st.data[st.top];
152               st.top--;
153               if(st.numa!=0)
154               {
155               st.result=parseFloat(st.numb)/parseFloat(st.numa);
156               st.top++;
157               st.data[st.top]=st.result;
158               }     
159               else
160               {
161                    alert("你输入的数据有误请重新输入");
162               }
163               break;
164             default:  
165                 st.tempresult=0;
166                 while((postexp.pe[postexp.index]>='0'&&postexp.pe[postexp.index]<='9')||(postexp.pe[postexp.index]=='.'))
167                 {
168                     if(postexp.pe[postexp.index]=='.')
169                     { 
170                        postexp.index++;
171                        st.point='.';
172                         while(postexp.pe[postexp.index]>='0'&&postexp.pe[postexp.index]<='9')
173                         {
174                           st.decimals=10*st.decimals+parseFloat(postexp.pe[postexp.index]);
175                           postexp.index++;
176                         }
177                     }
178                     else
179                     {
180                       st.intnum = 10*st.intnum+parseFloat(postexp.pe[postexp.index]);
181                       postexp.index++;
182                     }
183                 }
184                 if(st.point=='.')
185                 {
186                     var a=st.intnum+"";
187                     var b=st.decimals+"";
188                     var temp=a+st.point+b;
189                 st.tempresult=  parseFloat(temp);
190                 }
191                 else
192                 {
193                     st.tempresult = st.intnum;
194                 }
195                 st.decimals=0;
196                 st.intnum=0;
197                 st.point='';
198                 st.top++;
199                 st.data[st.top]=st.tempresult;
200               break;  
201                
202        }    
203        postexp.index++
204        }
205         document.getElementById('result').innerHTML=st.data[st.top] ;
206       postexp.pe.length=0;
207       postexp.index=0;
208       postexp.totallength=0;
209       st.data.length=0;
210       st.decimals=0;
211       st.intnum=0;
212       st.numa=0;
213       st.numb=0;
214       st.point='';
215       st.result=0;
216       st.top=-1;
217 }
218   
219 </script>
220 
221 </head>
222 
223 <body>
224 <p onclick="init()">fdfd</p>
225 <input type="text" id="c" />
226 <input type="button" value="计算" onclick="init(document.getElementById('c').value)"/>
227 <br />
228 结果是:<label id="result"></label>
229 </body>
230 </html>

 

 编程时 需要注意一点的是:当计算一次结束时,要记得还原对象的初始值。

 

 

 

 

posted on 2011-04-12 23:22  wtq  阅读(863)  评论(0编辑  收藏  举报