椓镒澹可以用來判斷一個算術(shù)表達(dá)式中的括號是否匹配浓体。編寫一個函數(shù),該函數(shù)接受一個算術(shù)表達(dá)式為參數(shù),返回括號缺失的位置助析。下面是一個括號不匹配的算術(shù)表達(dá)式的例子:2.3+ 23 / 12 + (3.14159*0.24
一個算術(shù)表達(dá)式的后綴表達(dá)式形式如下:op1 op2 operator 使用兩個棧犀被,一個用來存儲操作數(shù),另外一個用來存儲操作符外冀,設(shè)計并實(shí)現(xiàn)一個Javascript函數(shù)寡键,該函數(shù)可以將中綴表達(dá)式轉(zhuǎn)換為后綴表達(dá)式,然后利用棧對該表達(dá)式求值雪隧。
現(xiàn)實(shí)生活中棧的一個例子是佩斯糖果盒西轩。想象一下你有一盒佩斯糖果,里面塞滿了紅色脑沿、黃色和白色的糖果藕畔,但是你不喜歡黃色的糖果。使用棧(有可能用到多個棧)寫一段程序庄拇,在不改變盒內(nèi)其他糖果疊放順序的基礎(chǔ)上注服,將黃色糖果移除。
{
// 首先實(shí)現(xiàn)一個棧
function Stack() {
this.dataStore = []
this.top = 0
this.push = push
this.pop = pop
this.peek = peek
this.length = length
this.clear = clear
}
function push(element) {
this.dataStore[this.top++] = element
}
function pop(params) {
return this.dataStore[--this.top]
}
function peek() {
return this.dataStore[this.top - 1]
}
function length() {
return this.top
}
function clear() {
this.top = 0
}
// 數(shù)值間的相互轉(zhuǎn)換
// 利用棧將一個數(shù)字從一種數(shù)值轉(zhuǎn)換成另一種數(shù)值措近。假設(shè)想將數(shù)組n轉(zhuǎn)換為以b為基數(shù)的數(shù)字溶弟,實(shí)現(xiàn)的轉(zhuǎn)換算法如下:
// 1. 最高位為n%b,將此位壓入棧
// 2. 使用n/b代替n
// 3. 重復(fù)步驟1和1,直到n等于0瞭郑,且沒有余數(shù)
// 4. 持續(xù)將棧內(nèi)元素彈出勺美,直到棧為空,依次將這些元素排列稿械,就得到轉(zhuǎn)換后數(shù)字的字符串形式
// 此算法只針對基數(shù)為2~9的情況
function mulBase(num, base) {
var s = new Stack();
do {
s.push(num % base)
num = Math.floor( num/ base)
} while (num > 0)
var converted = ''
while (s.length() > 0) {
converted += s.pop()
}
return converted
}
// 測試用例
// var num = 32;
// var base = 2
// var newNum = mulBase(num, base)
// console.log(newNum)
// 利用stack類拙已,判斷給定字符串是否是回文的程序
function isPalindrome(word) {
var s = new Stack()
for(var i = word.length - 1; i >= 0; i--) {
s.push(word[i])
}
var rword = ''
while (s.length() > 0) {
rword += s.pop()
}
console.log(word)
console.log(rword)
}
// 測試用例
// isPalindrome('hello')
// 普通遞歸
function factorial(n) {
if (n === 0) {
return 1
} else {
return n * factorial(n - 1)
}
}
// 測試用例
// console.log(factorial(5))
// 用棧來模擬遞歸
function fact(n) {
var s = new Stack()
while(n > 1) {
s.push(n--)
}
var product = 1
while (s.length() > 0) {
product *= s.pop()
}
return product
}
// 思考:用棧來模擬遞歸,將需要遞歸的數(shù)據(jù)存儲在棧里袜茧,再將棧中的元素彈出去做遞歸計算菜拓。
// 測試用例
// console.log(fact(5))
// 1. 棧可以用來判斷一個算術(shù)表達(dá)式中的括號是否匹配笛厦。編寫一個函數(shù)纳鼎,該函數(shù)接受一個算術(shù)表達(dá)式為參數(shù),返回括號缺失的位置裳凸。下面是一個括號不匹配的算術(shù)表達(dá)式的例子:2.3+23/12+(3.14159*0.24
function isMatch(express) {
var str = String(express)
var s = new Stack()
for(var i = 0; i < str.length; i++) {
if(str[i] === '(') {
s.push(i) // 注意 這里存儲的是擴(kuò)號的位置
} else if (str[i] === ')') {
s.pop()
}
}
console.log(`在第${s.peek()}個字符是不匹配的括號`)
}
// isMatch('2.3+23/12+(3.14159*0.24')
// 2. 一個算術(shù)表達(dá)式的后綴表達(dá)式形式如下:op1 op2 operator
// 使用兩個棧贱鄙,一個用來存儲操作數(shù),另外一個用來存儲操作符姨谷,設(shè)計并實(shí)現(xiàn)一個Javascript函數(shù)逗宁,該函數(shù)可以將中綴表達(dá)式轉(zhuǎn)換為后綴表達(dá)式,然后利用棧對該表達(dá)式求值梦湘。
// 3.現(xiàn)實(shí)生活中棧的一個例子是佩斯糖果盒瞎颗。想象一下你有一盒佩斯糖果件甥,里面塞滿了紅色、黃色和白色的糖果哼拔,但是你不喜歡黃色的糖果引有。使用棧(有可能用到多個棧)寫一段程序,在不改變盒內(nèi)其他糖果疊放順序的基礎(chǔ)上倦逐,將黃色糖果移除譬正。
// 先取糖果 如果不是黃色就將糖果放到A棧,如果是黃色糖果就將糖果放到B棧檬姥,直到將原棧的長度為0
// 再將A棧的糖果依次放入到oirgin原棧
const boxS = new Stack();
boxS.push('red');
boxS.push('yellow');
boxS.push('white');
boxS.push('white');
boxS.push('yellow');
boxS.push('yellow');
boxS.push('red');
boxS.push('red');
boxS.push('white');
boxS.push('yellow');
boxS.push('red');
function Tangguo(sourceStack) {
var tempStack = new Stack()
var yellowStack = new Stack()
while (sourceStack.length() > 0) {
if(sourceStack.peek() === 'yellow') {
yellowStack.push(sourceStack.pop()) // 移除黃色糖果
} else {
tempStack.push(sourceStack.pop()) // 暫時保留其它顏色糖果
}
}
var newStack = new Stack()
while (tempStack.length() > 0) {
newStack.push(tempStack.pop())
}
return newStack
}
console.log(Tangguo(boxS))
}
中綴表達(dá)式轉(zhuǎn)換后綴表達(dá)式的實(shí)現(xiàn)曾我,不理解,后面還需要思考