matlab制作科学计算器,MATLAB科学计算器源代码
时间: 2023-07-25 09:03:53 浏览: 140
以下是MATLAB科学计算器的源代码,你可以根据需要进行修改和完善:
```matlab
% 创建计算器窗口
f = figure('Visible','off','Position',[360,500,400,300]);
% 创建数学函数按钮
sin_btn = uicontrol('Style','pushbutton','String','sin',...
'Position',[315,220,70,25],'Callback',@sin_callback);
cos_btn = uicontrol('Style','pushbutton','String','cos',...
'Position',[315,190,70,25],'Callback',@cos_callback);
tan_btn = uicontrol('Style','pushbutton','String','tan',...
'Position',[315,160,70,25],'Callback',@tan_callback);
sqrt_btn = uicontrol('Style','pushbutton','String','sqrt',...
'Position',[315,130,70,25],'Callback',@sqrt_callback);
% 创建数字按钮
num_btn = cell(1,10);
for i = 1:10
num_btn{i} = uicontrol('Style','pushbutton','String',i-1,...
'Position',[(i-1-3*fix((i-1)/3))*75+15,60+60*fix((i-1)/3),60,50],'Callback',@num_callback);
end
% 创建操作符按钮
add_btn = uicontrol('Style','pushbutton','String','+',...
'Position',[225,190,70,25],'Callback',@add_callback);
sub_btn = uicontrol('Style','pushbutton','String','-',...
'Position',[225,160,70,25],'Callback',@sub_callback);
mul_btn = uicontrol('Style','pushbutton','String','*',...
'Position',[225,130,70,25],'Callback',@mul_callback);
div_btn = uicontrol('Style','pushbutton','String','/',...
'Position',[225,100,70,25],'Callback',@div_callback);
dot_btn = uicontrol('Style','pushbutton','String','.',...
'Position',[150,100,70,25],'Callback',@dot_callback);
eq_btn = uicontrol('Style','pushbutton','String','=',...
'Position',[315,100,70,25],'Callback',@eq_callback);
clear_btn = uicontrol('Style','pushbutton','String','clear',...
'Position',[225,220,70,25],'Callback',@clear_callback);
% 创建文本框显示计算结果
result_text = uicontrol('Style','text','String','',...
'Position',[15,250,370,40],'HorizontalAlignment','right');
% 初始化计算器
num1 = '';
num2 = '';
op = '';
result = '';
% 显示计算器窗口
f.Visible = 'on';
% 数字按钮回调函数
function num_callback(hObject,eventdata)
global num1 num2 op result
if isempty(op)
num1 = [num1 hObject.String];
result = num1;
else
num2 = [num2 hObject.String];
result = [num1 ' ' op ' ' num2];
end
result_text.String = result;
end
% 点号按钮回调函数
function dot_callback(hObject,eventdata)
global num1 num2 op result
if isempty(op)
if isempty(num1)
num1 = '0.';
elseif ~contains(num1,'.')
num1 = [num1 '.'];
end
result = num1;
else
if isempty(num2)
num2 = '0.';
elseif ~contains(num2,'.')
num2 = [num2 '.'];
end
result = [num1 ' ' op ' ' num2];
end
result_text.String = result;
end
% 加号按钮回调函数
function add_callback(hObject,eventdata)
global num1 num2 op result
op = '+';
result = [num1 ' ' op ' '];
result_text.String = result;
end
% 减号按钮回调函数
function sub_callback(hObject,eventdata)
global num1 num2 op result
op = '-';
result = [num1 ' ' op ' '];
result_text.String = result;
end
% 乘号按钮回调函数
function mul_callback(hObject,eventdata)
global num1 num2 op result
op = '*';
result = [num1 ' ' op ' '];
result_text.String = result;
end
% 除号按钮回调函数
function div_callback(hObject,eventdata)
global num1 num2 op result
op = '/';
result = [num1 ' ' op ' '];
result_text.String = result;
end
% 等号按钮回调函数
function eq_callback(hObject,eventdata)
global num1 num2 op result
if isempty(num1) || isempty(num2) || isempty(op)
return;
end
switch op
case '+'
result = num2str(str2double(num1) + str2double(num2));
case '-'
result = num2str(str2double(num1) - str2double(num2));
case '*'
result = num2str(str2double(num1) * str2double(num2));
case '/'
if str2double(num2) == 0
result = 'Error';
else
result = num2str(str2double(num1) / str2double(num2));
end
end
num1 = result;
num2 = '';
op = '';
result_text.String = result;
end
% 清空按钮回调函数
function clear_callback(hObject,eventdata)
global num1 num2 op result
num1 = '';
num2 = '';
op = '';
result = '';
result_text.String = result;
end
% sin按钮回调函数
function sin_callback(hObject,eventdata)
global num1 num2 op result
if isempty(op)
result = num2str(sin(str2double(num1)));
num1 = result;
else
result = num2str(sin(str2double(num2)));
num2 = result;
end
result_text.String = result;
end
% cos按钮回调函数
function cos_callback(hObject,eventdata)
global num1 num2 op result
if isempty(op)
result = num2str(cos(str2double(num1)));
num1 = result;
else
result = num2str(cos(str2double(num2)));
num2 = result;
end
result_text.String = result;
end
% tan按钮回调函数
function tan_callback(hObject,eventdata)
global num1 num2 op result
if isempty(op)
result = num2str(tan(str2double(num1)));
num1 = result;
else
result = num2str(tan(str2double(num2)));
num2 = result;
end
result_text.String = result;
end
% sqrt按钮回调函数
function sqrt_callback(hObject,eventdata)
global num1 num2 op result
if isempty(op)
result = num2str(sqrt(str2double(num1)));
num1 = result;
else
result = num2str(sqrt(str2double(num2)));
num2 = result;
end
result_text.String = result;
end
```
以上是MATLAB科学计算器的源代码,包括各种按钮的回调函数,你可以根据需要进行修改和完善。
阅读全文