Question: I am using VBA code to make a simple calculator in Excel, please help me to modify the code so that it can complete the

I am using VBA code to make a simple calculator in Excel, please help me to modify the code so that it can complete the following tasks, thank you very much :
Extending the Calculator to Work with Multiple-Digit Numbers
Increasing the Size of the Calculator Memory
Calculating the Result Using Recursion
Here is the uncomplete code:
' The memory of the calculator as an array
' You need to change the size of the memory to something bigger
Dim Memory(1 To 3) As String
Dim CurrentPos As Integer
Function CalcTotal(ByVal Pos As Integer) As Long
'
' Task 4: Calculating the Result Using Recursion
'
' Case 1: if Pos is bigger than what you have in the Memory array
' Nothing is available
' Case 2: if Pos is exactly at the end of the Memory array
' Return the number in the position
' Case 3: Memory(Pos) is a number and Memory(Pos +1) is an operator
' Return the number in the current position together with the rest of the Memory array
' Please delete this line after you finish the function
CalcTotal =0
End Function
' This subroutine clears the calculator memory and put an initial value in the calculator
Sub ClearMemory(ByVal InitialValue As String)
' Put the initial value in the Memory array's first entry
Memory(1)= InitialValue
' Reset the position to the first position
CurrentPos =1
End Sub
' This subroutine handles the user input by reading the input value InputButton.
Sub HandleUserInput(ByVal InputButton As String)
Dim Display As String
' Remember the current display of the calculator
Display = Range("Display").Value
' This If statement prevents the calculator from crashing when the CurrentPos is 0,
' which happens when you suddenly stop the VBA code from running
If CurrentPos =0 Then
ClearMemory "0"
Display ="0"
End If
' Now you will process the user input based on the input button
'
' First, process the digits
If InputButton ="1" Or _
InputButton ="2" Or _
InputButton ="3" Or _
InputButton ="4" Or _
InputButton ="5" Or _
InputButton ="6" Or _
InputButton ="7" Or _
InputButton ="8" Or _
InputButton ="9" Or _
InputButton ="0" Then
' Case 1: The current entry is an operator so you need to add a new entry for the digit
If Memory(CurrentPos)="+" Or _
Memory(CurrentPos)="-" Or _
Memory(CurrentPos)="x" Or _
Memory(CurrentPos)="/" Then
'
' Task 3: Increasing the Size of the Calculator Memory
'
' This code has a problem, if the calculator runs out of memory
' the program will crash
' Add a new number entry
CurrentPos = CurrentPos +1
Memory(CurrentPos)= InputButton
Display = Memory(CurrentPos)
' Case 2: The current entry is a number so you need to add the digit at the end of it
Else
'
' Task 2: Extending the Calculator to Work with Multiple-Digit Numbers
'
' This code is not working, you can only see single-digit numbers in the calculator
' Add the digit to the memory array
Memory(CurrentPos)= InputButton
Display = Memory(CurrentPos)
End If
' Then, process the +,-, x,/ operators
ElseIf InputButton ="+" Or _
InputButton ="-" Or _
InputButton ="x" Or _
InputButton ="/" Then
' Beep the user if you already have an operator in the memory
If Memory(CurrentPos)="+" Or _
Memory(CurrentPos)="-" Or _
Memory(CurrentPos)="x" Or _
Memory(CurrentPos)="/" Then
Beep
Else
'
' Task 3: Increasing the Size of the Calculator Memory
'
' This code has a problem, if the calculator runs out of memory
' the program will crash
' Add a new operator entry
CurrentPos = CurrentPos +1
Memory(CurrentPos)= InputButton
End If
' Process the = operator
ElseIf InputButton ="=" Then
' Calculate the total from the Memory array and
' put the result in the display
ClearMemory CalcTotal(1)
Display = Memory(1)
' Finally, process the C (clear) operation
ElseIf InputButton = "Clear" Then
' Clear the current memory
ClearMemory "0"
Display ="0"
End If

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!