使用输入框而不是弹出功能

Using the input box instead of pop function

本文关键字:功能 输入      更新时间:2023-09-26

如何使用输入框而不是提示此代码?代码是这里。

一旦我点击链接;弹出一个提示,要求输入一个数字或Q退出。我如何将其更改为输入框?

function Stack() {
   var items = [];
   this.push = function(element){
   items.push(element);
   };
  this.pop = function(){
  return items.pop();
  };
 this.peek = function(){
 return items[items.length-1];
 };
this.isEmpty = function(){
return items.length == 0;
};
this.size = function(){
return items.length;
};
this.clear = function(){
items = [];
};
this.print = function(){
alert("Stack Elements are:"+items.toString());
};
}

声明堆栈对象:

var stack = new Stack();
while(1)
{
  var element = prompt("Enter stack element,(q or Q to exit)", "");
  if(element == 'q' || element =='Q')
  {
 break;
 }
else
 {
 if(element=='*'
 || element=='+' || element=='-' || element=='/')
 {
//Check if stack is empty
if(stack.isEmpty() || stack.size<2 )
{
alert("Invalid Operation, Stack is empty or size is less than 2");

 }   
else
{
var op1 = stack.pop();
var op2 = stack.pop();
if(element=='*')
{
var res = op1 * op2;
}
else if(element=='+')
{
 var res = op1 + op2;
 }
else if(element=='-')
{
var res = op1 - op2;
}
else if(element=='/')
{
var res = op1 / op2;
}
stack.push(res);
stack.print();
  }   
  }
  else
  {
   stack.push(element);
   stack.print();
  }
  }
  }   

尝试以下示例:

小提琴

var stack = new Stack();
function Stack() {
  var items = [];
  this.push = function(element) {
    items.push(element);
  };
  this.pop = function() {
    return items.pop();
  };
  this.peek = function() {
    return items[items.length - 1];
  };
  this.isEmpty = function() {
    return items.length == 0;
  };
  this.size = function() {
    return items.length;
  };
  this.clear = function() {
    items = [];
  };
  this.print = function() {
    theList.options.length = 0;
    for (var i = 0; i < items.length; i++) {
      var theOption = new Option(items[i]);
      theList.options[theList.options.length] = theOption;
    }
  };
}
function pushStack(newVal) {
  if (newVal == '*' || newVal == '+' || newVal == '-' || newVal == '/') {
    //Check if stack is empty
    if (stack.isEmpty() || stack.size < 2) {
      alert("Invalid Operation, Stack is empty or size is less than 2");
    } else {
      var op1 = stack.pop();
      var op2 = stack.pop();
      if (newVal == '*') {
        var res = op1 * op2;
      } else if (newVal == '+') {
        var res = op1 + op2;
      } else if (newVal == '-') {
        var res = op1 - op2;
      } else if (newVal == '/') {
        var res = op1 / op2;
      }
      stack.push(res);
    }
  } else {
    stack.push(newVal);
  }
}
function popStack() {
  var popVal = stack.pop();
  if (popVal == undefined)
    return "Empty stack!";
  else
    return popVal;
}
function showStack() {
  stack.print();
}
<FORM>
  <INPUT type="text" name="txtPush" id="txtPush" />
  <INPUT type=button value="Push" onClick='pushStack(txtPush.value); txtPush.value=""; showStack(theList);' />
  <SELECT name="theList" id="theList" size=12>
    <OPTION>Displays the current state of the stack!</OPTION>
  </SELECT>
</FORM>