-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumber_Systems.m
More file actions
187 lines (156 loc) · 7.48 KB
/
Copy pathNumber_Systems.m
File metadata and controls
187 lines (156 loc) · 7.48 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
function number_system_converter()
fig = uifigure('Name', 'Number System Converter', 'Position', [100 100 600 400]);
input_label = uilabel(fig, 'Text', 'Input:', 'Position', [20 350 100 22]);
input_field = uieditfield(fig, 'text', 'Position', [120 350 200 22]);
from_label = uilabel(fig, 'Text', 'From:', 'Position', [20 300 100 22]);
from_dropdown = uidropdown(fig, 'Position', [120 300 200 22], ...
'Items', {'Binary', 'Octal', 'Decimal', 'Hexadecimal'});
to_label = uilabel(fig, 'Text', 'To:', 'Position', [20 250 100 22]);
to_dropdown = uidropdown(fig, 'Position', [120 250 200 22], ...
'Items', {'Binary', 'Octal', 'Decimal', 'Hexadecimal'});
% Create output area before referencing it in button callbacks
output_area = uitextarea(fig, 'Position', [20 20 560 120], 'Editable', 'off');
convert_button = uibutton(fig, 'Text', 'Convert', 'Position', [120 200 100 22], ...
'ButtonPushedFcn', @(btn,event) convert_number(input_field, from_dropdown, to_dropdown, output_area));
clear_button = uibutton(fig, 'Text', 'Clear', 'Position', [230 200 100 22], ...
'ButtonPushedFcn', @(btn,event) clear_fields(input_field, output_area));
output_label = uilabel(fig, 'Text', 'Output:', 'Position', [20 150 100 22]);
menu = uimenu(fig, 'Text', 'Tools');
uimenu(menu, 'Text', 'Bitwise Operations', 'MenuSelectedFcn', @(btn,event) open_bitwise_operations());
uimenu(menu, 'Text', 'Number System Tutorial', 'MenuSelectedFcn', @(btn,event) show_tutorial());
uimenu(menu, 'Text', 'About', 'MenuSelectedFcn', @(btn,event) show_about(fig));
end
function convert_number(input_field, from_dropdown, to_dropdown, output_area)
% Input validation
if isempty(input_field.Value)
display_error(output_area, 'Input field cannot be empty.');
return;
end
% Get input values
input_str = input_field.Value;
from_system = from_dropdown.Value;
to_system = to_dropdown.Value;
try
% Convert input to decimal
switch from_system
case 'Binary'
if ~all(ismember(input_str, '01'))
error('Invalid binary input. Use only 0 and 1.');
end
decimal = bin2dec(input_str);
case 'Octal'
if ~all(ismember(input_str, '01234567'))
error('Invalid octal input. Use digits 0-7.');
end
decimal = base2dec(input_str, 8);
case 'Decimal'
if ~all(ismember(input_str, '0123456789'))
error('Invalid decimal input. Use digits 0-9.');
end
decimal = str2double(input_str);
if isnan(decimal)
error('Invalid decimal number.');
end
case 'Hexadecimal'
if ~all(ismember(lower(input_str), '0123456789abcdef'))
error('Invalid hexadecimal input. Use digits 0-9 and letters A-F.');
end
decimal = hex2dec(input_str);
end
% Check for overflow
if decimal > intmax('uint64') || decimal < intmin('uint64')
error('Number is too large or small for conversion.');
end
% Convert decimal to target system
switch to_system
case 'Binary'
result = dec2bin(decimal);
case 'Octal'
result = dec2base(decimal, 8);
case 'Decimal'
result = num2str(decimal);
case 'Hexadecimal'
result = dec2hex(decimal);
end
% Display result
output_area.Value = sprintf('Result: %s', result);
catch ME
display_error(output_area, ME.message);
end
end
function clear_fields(input_field, output_area)
input_field.Value = '';
output_area.Value = '';
end
function open_bitwise_operations()
bit_fig = uifigure('Name', 'Bitwise Operations', 'Position', [200 200 400 300]);
input1_label = uilabel(bit_fig, 'Text', 'Input 1:', 'Position', [20 250 100 22]);
input1_field = uieditfield(bit_fig, 'text', 'Position', [120 250 200 22]);
input2_label = uilabel(bit_fig, 'Text', 'Input 2:', 'Position', [20 200 100 22]);
input2_field = uieditfield(bit_fig, 'text', 'Position', [120 200 200 22]);
operation_label = uilabel(bit_fig, 'Text', 'Operation:', 'Position', [20 150 100 22]);
operation_dropdown = uidropdown(bit_fig, 'Position', [120 150 200 22], ...
'Items', {'AND', 'OR', 'XOR', 'NOT'});
bit_output_area = uitextarea(bit_fig, 'Position', [20 20 360 30], 'Editable', 'off');
calculate_button = uibutton(bit_fig, 'Text', 'Calculate', 'Position', [120 100 100 22], ...
'ButtonPushedFcn', @(btn,event) perform_bitwise_operation(input1_field, input2_field, operation_dropdown, bit_output_area));
bit_output_label = uilabel(bit_fig, 'Text', 'Output:', 'Position', [20 50 100 22]);
end
function perform_bitwise_operation(input1_field, input2_field, operation_dropdown, output_area)
try
% Input validation
if isempty(input1_field.Value)
error('Input 1 cannot be empty.');
end
if isempty(input2_field.Value) && ~strcmp(operation_dropdown.Value, 'NOT')
error('Input 2 cannot be empty for this operation.');
end
a = bin2dec(input1_field.Value);
if ~strcmp(operation_dropdown.Value, 'NOT')
b = bin2dec(input2_field.Value);
end
op = operation_dropdown.Value;
switch op
case 'AND'
result = bitand(a, b);
case 'OR'
result = bitor(a, b);
case 'XOR'
result = bitxor(a, b);
case 'NOT'
result = bitcmp(a, 'uint64');
end
output_area.Value = sprintf('Result: %s', dec2bin(result));
catch ME
display_error(output_area, ME.message);
end
end
function show_tutorial()
tutorial_fig = uifigure('Name', 'Number System Tutorial', 'Position', [300 300 500 400]);
tutorial_text = uitextarea(tutorial_fig, 'Position', [20 20 460 360], 'Editable', 'off');
tutorial_text.Value = sprintf(['Number System Tutorial\n\n', ...
'Binary (Base 2):\n', ...
'- Uses only 0 and 1\n', ...
'- Each digit represents a power of 2\n', ...
'- Example: 1010 = 1*2^3 + 0*2^2 + 1*2^1 + 0*2^0 = 8 + 0 + 2 + 0 = 10 (decimal)\n\n', ...
'Octal (Base 8):\n', ...
'- Uses digits 0-7\n', ...
'- Each digit represents a power of 8\n', ...
'- Example: 12 = 1*8^1 + 2*8^0 = 8 + 2 = 10 (decimal)\n\n', ...
'Decimal (Base 10):\n', ...
'- Uses digits 0-9\n', ...
'- Our standard number system\n\n', ...
'Hexadecimal (Base 16):\n', ...
'- Uses digits 0-9 and letters A-F\n', ...
'- Each digit represents a power of 16\n', ...
'- Example: A = 10, B = 11, ..., F = 15\n', ...
'- Example: 2A = 2*16^1 + 10*16^0 = 32 + 10 = 42 (decimal)']);
end
function show_about(fig)
message = sprintf('Number System Converter v1.0\n\nCreated by Mhar Andrei Macapallag\nDate: 2024-10-06\n\nThis program provides comprehensive number system conversion and bitwise operation capabilities with a user-friendly interface.');
uialert(fig, message, 'About', 'Icon', 'info');
end
function display_error(output_area, message)
output_area.Value = sprintf('Error: %s', message);
end
number_system_converter();