一个简单的计算器
题目
完成一个简单的计算器。
要求(但不限于)
GUI 上具有数字键盘输入区域,能够进行加、减、乘、除、三角函数等基础
运算。
界面简洁、美观
可能需要的控件:
Push Button
Edit Text
Pop-up Menu
1 功能介绍
本程序是一个简单的计算器,能够进行加、减、乘、除、三角函数等基础运算。
2 功能实现
首先用 MATLAB GUI 功能,在绘制一个静态文本框和一个文本编辑框,以及 24 个命令
按钮,调整好各控件大小、颜色,整体布局如图所示。
图 1 计算机 guide 布局
1
2.1 GUI 布局
1. 打开 Matlab,输入 Guide 回车。
2. 然后双击“Black GUI(Default)”出现 GUI 窗口。
图 2 新建 guide
图 3 插入 button
2
3. 添加你所需要的按钮。
图 4 新建按钮
4. 对按钮进行修改
将按钮设置为红色,位置进行排布,静态文本框设置为白色。
5. 保存,添加功能函数
把 做 好 的 按 钮 及 静 态 文 本 框 保 存 后 自 动 弹 出 Editor 的 M 文 本 , 对 然 后 对 相 应 的
pushbutton 添加功能函数。以下是相应按钮的功能函数。
2.2 按键编写
1. 数字键设计:0—9 以及小数点函数都一样,只是参数不同:
例如:按键‘1’响应:
textString=get(handles.text1,'String')
textString=strcat(textString,'0')
set(handles.text1,'String',textString)
2. 四则运算函数
‘+’功能响应:
textString
get(handles.text1,'String');
=
3
=strcat(textString,'+');
get(handles.text1,'String');
=
=strcat(textString,'-');
get(handles.text1,'String');
=
=strcat(textString,'*');
textString
set(handles.text1,'String',textString)
‘-’功能响应:
textString
textString
set(handles.text1,'String',textString)
‘×’功能响应:
textString
textString
set(handles.text1,'String',textString)
‘÷’功能响应:
textString
textString
set(handles.text1,'String',textString)
3. 科学计算函数
textString=handles.text1;
textString=sin(str2num(get(handles.text1,'String'))*pi/180);
set(handles.text1,'String',num2str(textString)
其中 get(handles.text1,'String'))*pi/180);是把角度转换为弧度,这样在编程环境中才
能识别,cos 才能起作用。然后执行 set 函数,把结果输出来。同理在 cos 的回调函数
中夜输入相应的函数,只需把
textString=sin(str2num(get(handles.text1,'String'))*pi/180);中的 sin 改成 cos。
=
=strcat(textString,'/');
get(handles.text1,'String);
2.3 计算器的使用
输入 5+6。
4
得到结果:
图 5 输入 5+6
图 6 5+6 结果
5
输入 3^2-1.2。
得到结果:
图 7 输入 3^2-1.2
图 8 3^2-1.2 结果
6
2.4 程序源代码
NEW('CALLBACK',hObject,eventData,handles,...) calls the local
function named CALLBACK in NEW.M with the given input arguments.
NEW('Property','Value',...) creates a new NEW or raises the
existing singleton*.
applied to the GUI before new_OpeningFunction gets called.
unrecognized property name or invalid value makes property application
stop.
All inputs are passed to new_OpeningFcn via varargin.
Starting from the left, property value pairs are
An
Choose "GUI allows only one
NEW, by itself, creates a new NEW or raises the existing
singleton*.
H = NEW returns the handle to a new NEW or the handle to
the existing singleton*.
functionvarargout = new(varargin)
% NEW M-file for new.fig
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Copyright 2002-2003 TheMathWorks, Inc.
% Edit the above text to modify the response to help new
% Last Modified by GUIDE v2.5 24-Nov-2015 13:05:25
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',
*See GUI Options on GUIDE's Tools menu.
instance to run (singleton)".
mfilename, ...
gui_Singleton, ...
'gui_Singleton',
'gui_OpeningFcn', @new_OpeningFcn, ...
'gui_OutputFcn',
@new_OutputFcn, ...
[] , ...
'gui_LayoutFcn',
'gui_Callback',
[]);
ifnargin&&ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
ifnargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
7
handle to figure
reserved - to be defined in a future version of MATLAB
structure with handles and user data (see GUIDATA)
command line arguments to new (see VARARGIN)
cell array for returning output args (see VARARGOUT);
handle to figure
reserved - to be defined in a future version of MATLAB
structure with handles and user data (see GUIDATA)
% --- Executes just before new is made visible.
functionnew_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject
% eventdata
% handles
% varargin
% Choose default command line output for new
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes new wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
functionvarargout = new_OutputFcn(hObject, eventdata, handles)
% varargout
% hObject
% eventdata
% handles
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
textString=get(handles.text1,'String')
textString=strcat(textString,'0')
set(handles.text1,'String',textString)
% hObject
% eventdata
% handles
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
textString=get(handles.text1,'String')
textString=strcat(textString,'1')
set(handles.text1,'String',textString)
% hObject
% eventdata
% handles
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
textString=get(handles.text1,'String')
textString=strcat(textString,'2')
set(handles.text1,'String',textString)
% hObject
% eventdata
handle to pushbutton3 (see GCBO)
reserved - to be defined in a future version of MATLAB
8
handle to pushbutton1 (see GCBO)
reserved - to be defined in a future version of MATLAB
structure with handles and user data (see GUIDATA)
handle to pushbutton2 (see GCBO)
reserved - to be defined in a future version of MATLAB
structure with handles and user data (see GUIDATA)