My rpn calculator can't compute negative numbers...
By
TheAutomator
hi everyone!
I need some help with my rpn calculator.
I first translated it from vbscript to autoitscript and that's when i stumbled on problem one:
(46) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
$Stack[ubound($Stack)] = $Item
^ ERROR
After that (and i know this from testing it as a vbscript) it can't work with negative numbers:
"1 - - 2" to Reverse Polish notation = "1 - 2 -" gives me the build in error...
Can you guy's help me with my code pleas?
Func Print($what,$er = 0)
$t = ''
if $err <> 0 then $t = 'ERROR!'
MsgBox($er,$st,$what)
EndFunc
Func RPN($Tokenarray)
Local $Stack = []
Local $Result
Local $Operator_B
Local $Operator_A
For $Token = 0 To Ubound($Tokenarray)
If Isoperator($Tokenarray[$Token]) Then
$Operator_B = Pop($Stack)
$Operator_A = Pop($Stack)
If $Operator_A = "" Then
Print("The user has not input sufficient values in the expression.",16)
exit
EndIf
Switch $Tokenarray[$Token]
Case "+"
$Result = Number($Operator_A,3) + Number($Operator_B,3)
Case "-"
$Result = Number($Operator_A,3) - Number($Operator_B,3)
Case "*"
$Result = Number($Operator_A,3) * Number($Operator_B,3)
Case "/"
$Result = Number($Operator_A,3) / Number($Operator_B,3)
Case "^"
$Result = Number($Operator_A,3) ^ Number($Operator_B,3)
EndSwitch
Push($Result,$Stack)
Else
Push($Tokenarray[$Token],$Stack)
EndIf
Next
If Ubound($Stack) > 1 Then
Print("The user input has too many values.",16)
Exit
EndIf
Return pop($Stack)
EndFunc
Func Push($Item, Byref $Stack)
If Ubound($Stack) > -1 Then
Redim $Stack[Ubound($Stack) + 1]
$Stack[Ubound($Stack)] = $Item
Else
Local $Stack = [$Item]
EndIf
EndFunc
Func Pop($Stack)
$pop = ""
If Ubound($Stack) > -1 Then
$Pop = $Stack[Ubound($Stack)]
Redim $Stack[Ubound($Stack) - 1]
EndIf
Return $pop
EndFunc
Func Peek($Stack)
$peek = ""
If Ubound($Stack) > -1 Then
$Peek = $Stack(Ubound($Stack))
EndIf
Return $peek
EndFunc
Func Isoperator($Operator)
Return StringInstr("+-*/^&<=>", $Operator) <> 0 And StringLen($Operator) = 1
EndFunc
Func Precedence($Operator)
If Isoperator($Operator) Then
Switch $Operator
case "^"
Return 4
case "*","/"
Return 3
case "+","-"
Return 2
case "&"
Return 1
case "<","=",">"
Return 0
EndSwitch
EndIf
EndFunc
;##################################################################################################
; (4 - -5) = (4|-|-|5) = (4|-|5|-)
; input tokens rpn
Global $calculate_this = ["4","-","5","-"]
Print(rpn($calculate_this))
thanks in advance ,
TheAutomator