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 MultipleDigit 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 To As String
Dim CurrentPos As Integer
Function CalcTotalByVal Pos As Integer As Long
Task : Calculating the Result Using Recursion
Case : if Pos is bigger than what you have in the Memory array
Nothing is available
Case : if Pos is exactly at the end of the Memory array
Return the number in the position
Case : MemoryPos is a number and MemoryPos 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
End Function
This subroutine clears the calculator memory and put an initial value in the calculator
Sub ClearMemoryByVal InitialValue As String
Put the initial value in the Memory array's first entry
Memory InitialValue
Reset the position to the first position
CurrentPos
End Sub
This subroutine handles the user input by reading the input value InputButton.
Sub HandleUserInputByVal InputButton As String
Dim Display As String
Remember the current display of the calculator
Display RangeDisplayValue
This If statement prevents the calculator from crashing when the CurrentPos is
which happens when you suddenly stop the VBA code from running
If CurrentPos Then
ClearMemory
Display
End If
Now you will process the user input based on the input button
First, process the digits
If InputButton Or
InputButton Or
InputButton Or
InputButton Or
InputButton Or
InputButton Or
InputButton Or
InputButton Or
InputButton Or
InputButton Then
Case : The current entry is an operator so you need to add a new entry for the digit
If MemoryCurrentPos Or
MemoryCurrentPos Or
MemoryCurrentPosx Or
MemoryCurrentPos Then
Task : 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
MemoryCurrentPos InputButton
Display MemoryCurrentPos
Case : The current entry is a number so you need to add the digit at the end of it
Else
Task : Extending the Calculator to Work with MultipleDigit Numbers
This code is not working, you can only see singledigit numbers in the calculator
Add the digit to the memory array
MemoryCurrentPos InputButton
Display MemoryCurrentPos
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 MemoryCurrentPos Or
MemoryCurrentPos Or
MemoryCurrentPosx Or
MemoryCurrentPos Then
Beep
Else
Task : 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
MemoryCurrentPos 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
Display Memory
Finally, process the C clear operation
ElseIf InputButton "Clear" Then
Clear the current memory
ClearMemory
Display
End If
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
