然後以前忘記提
我很久沒寫程式了
雖說我清潔隊的工作其實目前的力氣還是不太能勝任的
若僅是這樣
因此還是要回歸原點
那稍微複雜些的話
理論上做到這邊應該不難才對
好不好解決晚些就知道)
JavaScript的處理最簡單狀況的話,源碼大概也要這樣才能達成
<!DOCTYPE html> <meta charset="UTF-16"/> <HTML> <HEAD> </HEAD> <script> var formula_1 = "3-2";
var result=""; var queue=[]; var stack=[];
function Shunting_Yard_Algorithm() { for(var i=0;i<formula_1.length;i++) { //result += formula_1[i]; switch(formula_1[i]) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: queue += formula_1[i]; break; case +: case -: //case *: //case /: //乘除晚些再處理(handle later) stack.push(formula_1[i]); break; default: alert("out of control(預料之外)"); } } // alert(result); //測試字串是否正常
//alert(stack); //看堆疊內容
result += queue; while(stack.length>0) { result += stack.pop(); }
alert(result); //看看字串結果如何,see string state }
Shunting_Yard_Algorithm(); //要呼叫(call)一次才會運作
</script> <BODY>
</BODY> </HTML>
---(編程遇到的麻煩)
因此似乎是在讀入時就要處理
嗯,不麻煩,那感覺存數字較好
<!DOCTYPE html> <meta charset="UTF-16"/> <HTML> <HEAD> </HEAD> <script> var formula_1 = "36-24-6";
var result=""; var result_number = 0; var queue=[]; var stack=[]; var current_number=0; var temp_number_middle=0; var temp_number_right=0; var set_flag=false; //var more_digit_flag=false;
function Shunting_Yard_Algorithm() { for(var i=0;i<formula_1.length;i++) { //result += formula_1[i]; switch(formula_1[i]) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: queue += formula_1[i]; break; case +: case -: //case *: //case /: //乘除晚些再處理(handle later) queue += " "; stack.push(formula_1[i]); break; default: alert("out of control(預料之外)"); } }
if(Number.isInteger(parseInt(formula_1[formula_1.length-1]))) { queue += " "; } //↑處理字串結束時數值也正好結束的特例, //也許有字串結束字元等更聰明的方法,但我現階段想偷懶
//alert(result); //看看字串結果如何,see string state
for(var i=0;i<result.length;i++) { switch(result[i]) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: if(!set_flag) { if(current_number==0) { current_number= result[i]; set_flag=true;
i++ while(Number.isInteger(parseInt(result[i]))) { current_number = current_number*10 + parseInt(result[i]); i++; } console.log("i=" + i); //↑在Firefox,Chrome,Opera,Vivaldi中, //按F12切到Console或主控台可以看偵錯資訊 } } else { //暫時不考慮輸入的式子是錯誤的狀況 if(temp_number_middle == 0) { temp_number_middle = parseInt(result[i]); } else { temp_number_middle *= 10; temp_number_middle += parseInt(result[i]); //也可以寫成temp_number_middle = temp_number_middle*10 + parseInt(result[i]); } console.log("temp_number_middle=" + temp_number_middle); } break; case +: if(temp_number_right==0) { result_number = current_number + temp_number_middle; current_number = result_number; } else { temp_number_middle = temp_number_middle + temp_number_right; temp_number_right=0; } break; case -: if(temp_number_right==0) { result_number = current_number - temp_number_middle; current_number = result_number; } else { temp_number_middle = temp_number_middle - temp_number_right; temp_number_right=0; } break; break; //case *: //case /: //乘除晚些再處理(handle later) case : if(Number.isInteger(parseInt(result[i+1]))) { temp_number_right = result[i+1]; i++;
while(Number.isInteger(parseInt(result[i+1]))) { temp_number_right = temp_number_right*10 + result[i+1]; i++; } } break; default: alert("out of control(預料之外)"); } } alert(result_number); }
但這樣的程式會有個問題
************
許久沒更新文章(上次是2018/03/11)
標題因為字數允許長度有修改過
因此簡化標題
雖然現在離寫出太閣立志傳那樣的小遊戲還有些距離
但只剩微小的距離
以前的問題其實沒解決
只是在Javascript中
字串的處理較C語言簡單許多
可透過正規表達式少去許多困擾
更新的程式碼在
(雖然目前運運算元優先順序是寫死在if裡面,這部份是還可以改良啦)
<!DOCTYPE html> <meta charset="utf-8"> <html> <body>
<textarea id="input_data"></textarea> <button id="run">執行</button> <div id="div_test"></div>
<script> run.addEventListener("click",compute); var expression_wait_parse=[]; var op_stack=[]; var postfix_result=[]; function compute(){ postfix_result=[]; document.getElementById("div_test").innerHTML = document.getElementById("input_data").value+"<br>"; expression_wait_parse = document.getElementById("input_data").value; expression_wait_parse= expression_wait_parse.replace(/s/g,""); expression_wait_parse = expression_wait_parse.split(/(-?d*.?d+)|([()])/);
for(var i=1;i<expression_wait_parse.length-1;i++) { if(!expression_wait_parse[i]) { if(/-/.test(expression_wait_parse[i+1])) { expression_wait_parse[i]="-"; expression_wait_parse[i+1]=(expression_wait_parse[i+1]*-1).toString(); } } }
expression_wait_parse=expression_wait_parse.filter(function(e){return e});
alert(expression_wait_parse);
for(var i=0;i<expression_wait_parse.length;i++) { if(/d/.test(expression_wait_parse[i])) { postfix_result.push(expression_wait_parse[i]); } else if(/[+-*/]/.test(expression_wait_parse[i])) { console.log(expression_wait_parse[i]); if(op_stack.length < 1) { op_stack.push(expression_wait_parse[i]); } else { for(var j=op_stack.length-1; j>=0;j--) { if(op_stack[j]=="(") { break; }
if((op_stack[j]=="+" || op_stack[j]=="-") && (expression_wait_parse[i]=="+" ||expression_wait_parse[i]=="-")) { postfix_result.push(op_stack.pop()); }
if((op_stack[j]=="*" || op_stack[j]=="/") && (expression_wait_parse[i]=="*" ||expression_wait_parse[i]=="/")) { postfix_result.push(op_stack.pop()); }
if((op_stack[j]=="*" || op_stack[j]=="/") && (expression_wait_parse[i]=="+" ||expression_wait_parse[i]=="-")) { postfix_result.push(op_stack.pop()); } }
op_stack.push(expression_wait_parse[i]); } console.log(op_stack); } else if(/(/.test(expression_wait_parse[i])) { op_stack.push(expression_wait_parse[i]); } else if(/)/.test(expression_wait_parse[i])) { var temp = op_stack.pop();
while(temp!="(") { postfix_result.push(temp); temp = op_stack.pop(); alert(postfix_result); } } }
for(var i=op_stack.length-1;i>=0;i--) { postfix_result.push(op_stack.pop()); }
document.getElementById("div_test").innerHTML += postfix_result+"<br>";
var compute_stack=[]; var num1,num2;
for(var i=0;i<postfix_result.length;i++) { if(/d/.test(postfix_result[i])) { compute_stack.push(postfix_result[i]); } else if(/[+-*/]/.test(postfix_result[i])) { num2 = compute_stack.pop(); num1 = compute_stack.pop(); compute_stack.push(eval(num1+postfix_result[i]+num2)); } }
document.getElementById("div_test").innerHTML += "計算結果" + compute_stack.pop()+"<br>"; } </script>
</body> </html>
推薦閱讀:
TAG:思考 |