Guide math operation equal.

12 visualizzazioni (ultimi 30 giorni)
Joan
Joan il 16 Nov 2019
Risposto: Walter Roberson il 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 Commenti
Walter Roberson
Walter Roberson il 16 Nov 2019
Modificato: Walter Roberson il 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 il 17 Nov 2019
just 2+2

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 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 =

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by