Guide math operation equal.

1 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2019-11-16
编辑:Walter Roberson 2019-11-16
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= ?

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2019-11-17
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 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by