2017-12-05 5 views
2

s1またはs2のスタックを選択でき、pushpopのような操作を実行できるスタックの作業モデルを作成しようとしています。チェックボックスをオンにしてスタックのオプションを選択できる対話型スタック

<html> 
 
<head> 
 
    <title>Stack With Constructor </title> 
 
</head> 
 
<body> 
 
    <div>Stack </div> 
 
    <div> 
 
    <p id="p1"> 
 
     stack 
 
    </p> 
 
    <input type="checkbox" name="stack1" value="s1">s1 
 
    <br> 
 
    <input type="checkbox" name="stack2" value="s2">s2 
 
    <br> 
 
    <textarea id="tdisplay"></textarea> 
 
    </div> 
 
    <textarea id="tpush"></textarea> 
 
    <button onclick="doJob()">push</button> 
 
    <button onclick="doJob1()">pop</button> 
 
    <textarea id="tpop"></textarea> 
 

 
    <script> 
 
    function myfunction() { 
 
     if (document.getElementById("stack1") == true) { 
 
     var s1 = new MyStack(); 
 
     } 
 
     if (document.getElementById("stack2") == true) { 
 
     var s2 = new MyStack(); 
 

 
     } 
 
    } 
 
    </script> 
 

 
    <script> 
 
    function push(v) { 
 
     if (this.st === 0) { 
 
     console.log("Stack Overflow"); 
 
     } else { 
 
     this.st = this.st - 1; 
 
     this.stk[this.st] = v; 
 
     document.getElementById("tdisplay").value = this.print();; 
 
     } 
 
    } 
 

 
    function pop() { 
 
     if (this.st === 10) { 
 
     console.log("Stack Underflow"); 
 
     } else { 
 
     document.getElementById("tdisplay").innerHTML = ""; 
 
     var temp = this.stk[this.st]; 
 
     this.st = this.st + 1; 
 

 
     return temp; 
 
     } 
 
    } 
 

 
    function print() { 
 
     console.log("Printing Stack"); 
 
     return this.stk.reduce((acc, cv, i, arr,) => { 
 
     console.log(cv); 
 
     return (i) ? acc + " " + "\n" + cv : cv; 
 
     }, ''); 
 
    } 
 

 
    function MyStack() { 
 
     this.st = 10; 
 
     this.stk = new Array(10); 
 
     this.push = push; 
 
     this.pop = pop; 
 
     this.print = print; 
 
    } 
 

 
    function doJob() { 
 
     var x = document.getElementById("tpush").value; 
 
     s1.push(x); 
 

 
     document.getElementById("tpush").value = ""; 
 
    } 
 

 
    function doJob1() { 
 
     var y = s1.pop(); 
 
     document.getElementById("tpop").value = y; 
 
     document.getElementById("tdisplay").value = s1.print(); 
 
    } 
 
    </script> 
 

 
</body> 
 

 
</html>

次のように私は問題を取得しています:

  1. 私はpop値の出力を得ることができません。つまり、popのボタンを押すと配列の残りの項目が表示されます。

  2. checkboxメソッドの使い方はわかりません。つまり、s1チェックボックスを選択すると、s1スタックが作成され、s2と同じ操作を実行できます。彼/彼女は彼らができるその後、s1を選択した場合

    1. ユーザーs1またはs2スタック
    2. を選択します。それは、次の手順で同じように動作することができますので、

    結果は、対話型のスタックのための適切なブラウザ機能をする必要がありますs1のすべての操作で動作します。

  3. ユーザーはs2でも切り替えることができ、すべての操作を実行できます。

ポップ機能とチェックボックスの機能に問題があります。これは、あなたが正しい方向に始めるのに役立つだろう

答えて

1

物事私が気づい:チェックボックスのブール値の

  1. 誤った使い方を。 (.checkedを使用)。
  2. また、チェックボックスの名前はstack1/2ではありませんでした。

    <body> 
    <div>Stack</div> 
    <div> 
        <p id="p1"> 
         stack 
        </p> 
        <input type="checkbox" id="stack1" value="s1">s1 
        <br> 
        <input type="checkbox" id="stack2" value="s2">s2 
        <br> 
        <textarea id="tdisplay"></textarea> 
    </div> 
    <textarea id="tpush"></textarea> 
    <button onclick="doJob()">push</button> 
    <button onclick="doJob1()">pop</button> 
    <textarea id="tpop"></textarea> 
    
    <script> 
        var s1, s2; 
    
        function myfunction() { 
         if (document.getElementById("stack1").checked == true) { 
          console.log('myFunction called!'); 
          s1 = new MyStack(); 
         } 
         if (document.getElementById("stack2").checked == true) { 
          s2 = new MyStack(); 
         } 
        } 
    
    
        function push(v) { 
         if (this.st === 0) { 
          console.log("Stack Overflow"); 
         } else { 
          this.st = this.st - 1; 
          this.stk[this.st] = v; 
          document.getElementById("tdisplay").value = this.print(); 
          ; 
         } 
        } 
    
        function pop() { 
         if (this.st === 10) { 
          console.log("Stack Underflow"); 
         } else { 
          document.getElementById("tdisplay").innerHTML = ""; 
          var temp = this.stk[this.st]; 
          this.st = this.st + 1; 
    
          return temp; 
         } 
        } 
    
        function print() { 
         console.log("Printing Stack"); 
         return this.stk.reduce((acc, cv, i, arr,) = > { 
          console.log(cv); 
         return (i) ? acc + " " + "\n" + cv : cv; 
        }, 
         '' 
        ) 
         ; 
        } 
    
        function MyStack() { 
         this.st = 10; 
         this.stk = new Array(10); 
         this.push = push; 
         this.pop = pop; 
         this.print = print; 
        } 
    
        function doJob() { 
         console.log('push pressed!'); 
    
         myfunction(); 
         console.log(s1); 
         var x = document.getElementById("tpush").value; 
         s1.push(x); 
    
         document.getElementById("tpush").value = ""; 
        } 
    
        function doJob1() { 
         console.log('pop pressed!'); 
         var y = s1.pop(); 
         document.getElementById("tpop").value = y; 
         document.getElementById("tdisplay").value = s1.print(); 
        } 
    </script> 
    
+1

私はポップ機能のための解決策が出ているが、まだチェックボックスのチェックボックス –

+0

のために行く方法を考え出すことはないだろう: '' 'のdocument.getElementById(「stack1」)。 checked == true'''私は '' myFunction() ''で実装しました。 – Programmerrrrr

関連する問題