Guide math operation equal.
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Joan
am 16 Nov. 2019
Beantwortet: Walter Roberson
am 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 Kommentare
Walter Roberson
am 16 Nov. 2019
Bearbeitet: Walter Roberson
am 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= ?
Akzeptierte Antwort
Walter Roberson
am 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 =
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!