-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArithmeticOperators.m
105 lines (53 loc) · 2.89 KB
/
ArithmeticOperators.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
%{
ArithmeticOperators class:
Manages the arithmetic operator buttons for the calculator.
This class is responsible for creating buttons for operations like addition,
subtraction, multiplication, and division. It handles the user interactions with
these buttons, appending the selected operator to the current input expression.
Hardcoding used for:
- The text and name for each operator button
- Assigning the callback function of each operator button
- The positioning of each operator button
%}
classdef ArithmeticOperators
properties
ParentContainer % Parent container for the operator buttons
InputExpression % Reference to the input expression field
end
methods
function obj = ArithmeticOperators(parent, inputExpr)
%{
Constructor for ArithmeticOperators class. Initializes operator buttons.
%}
obj.ParentContainer = parent;
obj.InputExpression = inputExpr;
% Create operator buttons
obj.createButtons();
end
function createButtons(obj)
%{
Dynamically creates buttons for each arithmetic operator.
%}
% Define operators and their positions
operators = {'+', '-', '*', '/'};
% Adjust positions to be right-aligned with the number pad
positions = [250, 235, 50, 30; 250, 195, 50, 30; 305, 235, 50, 30; 305, 195, 50, 30];
% Iterate through operators to create buttons
for i = 1:length(operators)
op = operators{i};
pos = positions(i, :);
% Button creation with callback to append operator
uibutton(obj.ParentContainer, 'Text', op, 'Position', pos, ...
'ButtonPushedFcn', @(btn,event) obj.appendToExpression(op));
end
end
function appendToExpression(obj, char)
%{
Appends the selected arithmetic operator to the input expression.
%}
% Append operator to input field
currentExpr = obj.InputExpression.Value;
obj.InputExpression.Value = [currentExpr, char];
end
end
end