2016-11-23 116 views
1

所以我正在研究圖形計算器(比基本窗口更有挑戰性),我希望能夠完成整個「數學「在一個文本字段中,就像輸入」5 + 3-5 * 11/3「一樣,當您按下'='時它會給出解決方案。Actionscript/Animate - 填充下一個陣列點(如果已經填充)

我決定使用數字和符號數組,我不知道如何使它填補下一個數組,如果這一個已經被使用:

var numbers:Array = new Array("","","","","","","","","","","","","","","",""); 
var actions:Array = new Array("","","","","","","","","","","","","","","",""); 

我使用拆分拆分與符號數I輸入,我想數字被放置在陣列。例如:我輸入555 + 666然後我需要類似

if (numbers[0] = "") {numbers[0] = 555} 
else if (numbers[1] = "") {numbers[1] = 555} 
else if..... 

知道我的意思了嗎? 很難形容...... 類似...當我輸入一個數字時,如果數字[0]已經填滿,請填寫數字[1],如果數字[1]被填充,則轉到數字[2] etc

+0

這當然解決了嗎?在'var':int = 0'處使用'for'循環,如果'i

回答

0

你想實現的是Reverse Polish Notation。在actionscript3中,數組是動態的,而不是固定的大小,這意味着你可以添加元素到數組中而不用擔心容量(至少在你的情況下)。

const array:Array = new Array(); 
trace(array.length);    // prints 0 
array.push(1); 
array.push(2); 
trace(array.length);    // prints 2 

我建議使用Array/Vector的「push」和「pop」方法,因爲這對於這樣的任務來說更自然。使用這些方法將簡化您的實現,因爲你擺脫不必要的檢查,像

if (numbers[1] == "") {...} 

,只是將其替換爲:

numbers.push(value); 

,然後從上取一個值:

const value:String = numbers.pop(); 
1

即使我同意@Nbooo和逆向波蘭表示法 但是矢量可能具有固定長度。

這不是一個答案,但只是一個例子(如果陣列的長度必須定義):

//Just for information.. 
var numbs:Vector.<Number> = new Vector.<Number>(10,true); 
var count:uint = 1; 
for (var i in numbs){ 
    numbs[i] = count++ 
} 
trace(numbs); 

// If You try to add an element to a Vector, 
// You will get the following Error at compile time : 
/* 
RangeError: Error #1126: Cannot change the length of a fixed Vector. 
    at Vector$double/http://adobe.com/AS3/2006/builtin::push() 
    at Untitled_fla::MainTimeline/frame1() 

*/ 
numbs.push(11); 
// Will throw an Error #1126 
trace(numbs); 

如果您使用此代碼更新固定載體,這將拋出一個錯誤:

numbs[4]=11; 
trace(numbs); 

輸出:

1,2,3,4,5,6,7,8,9,10 
1,2,3,4,11,6,7,8,9,10 
// length is 10, so no issue... 

如果考慮陣列和載體之間的性能,請檢查此參考:Vector class versus Array class

我希望這可能會有所幫助。

[編輯]

我建議你在這些鏈接檢查過:

ActionScript 3 fundamentals: Arrays

ActionScript 3 fundamentals: Associative arrays, maps, and dictionaries

ActionScript 3 fundamentals: Vectors and ByteArrays

[/編輯]

最佳REG急性呼吸窘迫綜合徵。 Nicolas。

+0

我可以問你一些關於actionscript的priv聊天嗎?不想浪費一個問題點在stackoverflow的東西這麼簡單,但無法在任何地方找到解決方案.. 鏈接到我打開的聊天室:http://www.e-chat.co/room/174323 – Omnitored

+0

當然,矢量可以是固定的,我只是假設在這種情況下,對於這個問題,非固定矢量將更合適。當然,這只是一個意見。 – Nbooo

+1

@Nbooo:這就是我說的原因:「這不是一個答案,而只是一個例子」;) – tatactic