2016-03-07 58 views
0

如何在此代码上使用输入框而不是提示?代码是 here使用输入框代替弹出功能

一旦我点击链接;一个提示弹出并要求键入一个数字或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(); 
    } 
    } 
    } 
+0

谢谢非常!这工作得非常好。我很感激! –

回答

0

试试下面的例子:

FIDDLE

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>