Guide math operation equal.

1 view (last 30 days)
Joan
Joan on 16 Nov 2019
Answered: Walter Roberson on 17 Nov 2019
Which code do I have to use to do the last operation like a calculator with the operator =.
Example if my last operation was 2+2=4 and I press = again it has to add 2 to the result, but if I multiply 2*2=4 and I press = in my guide it has to put in the screen 8 and if press again it has to be 16 (just like a calculator).
function equal_Callback(hObject, eventdata, handles)
number = get(handles.text1,'string') ;
number = eval(number) ;
set (handles.text1,'string',number) ;
  2 Comments
Walter Roberson
Walter Roberson on 16 Nov 2019
Edited: Walter Roberson on 16 Nov 2019
Is it correct that in such a situation, the variable number would contain '=' ?
In the 2+2 case, would the variable number contain 2+2 or would it contain 2+2= ?
Joan
Joan on 17 Nov 2019
just 2+2

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 17 Nov 2019
You need to parse what is in get(handles.text1,'string') .
If it is not (optional spaces followed by) = (followed by optional spaces) then you need to break apart the string and look for a mathematical operation and keep a record of what the operation and the right-hand operand are; then you execute the string and display the answer and record what the current value is.
If it is (optional spaces followed by) = (followed by optional spaces) then you need to recall the current value and the operation and the right-hand operand, and execute that (might be easiest to convert to string and execute that) and display the answer and record what the current value is.
Watch out for the case where the user enters a constant without entering an operation, such as if the user's input is just 17 with no operation and no right hand operand. In such a case you need to decide what the result should be if the user uses =

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!