基于MATLAB-GUI的简单计算器.doc
《基于MATLAB-GUI的简单计算器.doc》由会员分享,可在线阅读,更多相关《基于MATLAB-GUI的简单计算器.doc(24页珍藏版)》请在装配图网上搜索。
基于MATLAB-GUI的简单计算器设计 题目:计算器 完成一个简单的计算器。 要求(但不限于): GUI上具有数字键盘输入区域,能够进行加、减、乘、除、三角函数等基础运算。 界面简洁、美观 可能需要的控件: Push Button Edit Text Pop-up Menu 1 功能介绍 本程序是一个简单的计算器程序,使用MATLAB软件编写完成。主要具有加、减、乘、除、三角函数等基础运算,并通过GUI进行程序使用的交互。程序交互界面如下: 图1 程序的交互界面 2 功能实现 首先用MATLABGUI功能,在绘制一个动态文本框和一个文本编辑框,以及25个命令按钮,调整好各控件大小、颜色,整体布局如图所示: 备注:软件版本:MATLAB R2011b 2.1 布局GUI 1、打开MATLAB,在Guide中新建FIG文件 2、然后双击“BlankGUI(Default)”或单击OK键出现GUI窗口 3、添加按钮和动态文本框 4、根据按钮的作用及视觉效果做一定的修改 对按钮的字符串大小、颜色进行设置,对按钮的位置进行排布,尽量使按钮集中在动态文本框下面。最终设置的动态文本框为灰色,其他按钮均为蓝色。 5、保存、添加功能函数 将做好的按钮及动态文本框保存后自动弹出Editor的M文本,然后对相应的pushbutton添加功能函数。以下是相应按钮的功能函数。 (1)数字按键编写。 在functionpushbutton1_Callback(hObject,eventdata,handles)下输入: global jj a=get(handles.edit1,String); if(strcmp(a,0.)==1)&&(jj==0) set(handles.edit1,String,0.) else a=strcat(a,0) set(handles.edit1,String,a) end jj=0 这是使用句柄handles指向对象edit1,并以字符串形式来存储数据文本框edit1的内容,并存储数个“0”,然后由set(handles.edit1,String,a)在edit1中输出。 同理,分别在functionpushbutton2~10_Callback(hObject,eventdata,handles)下给1~9数字按键下编写此类程序。 (2)符号键: 在functionpushbutton11_Callback(hObject,eventdata,handles)下输入: global jj global j a=get(handles.edit1,String) a=strcat(a,+) if(jj==0) set(handles.edit1,String,a) jj=1; end j=0; strcat的作用是将两个字符串连接起来,就是在已输入的存储数据a后添加“+”进行运算。 然后执行set(handles.edit1,String,a)。符号键‘-’、‘*’、‘/’、‘.’与‘+’的运算函数类似, “平方运算”主要是由“^2”功能实现。 function pushbutton12_Callback(hObject, eventdata, handles) global jj global j a=get(handles.edit1,String) a=strcat(a,-) if(jj==0) set(handles.edit1,String,a) jj=1; end j=0; function pushbutton13_Callback(hObject, eventdata, handles) global jj global j a=get(handles.edit1,String) a=strcat(a,*) if(jj==0) set(handles.edit1,String,a) jj=1; end j=0; function pushbutton14_Callback(hObject, eventdata, handles) global jj global j a=get(handles.edit1,String) a=strcat(a,/) if(jj==0) set(handles.edit1,String,a) jj=1; end j=0; function pushbutton15_Callback(hObject, eventdata, handles) global jj global j a=get(handles.edit1,String) a=strcat(a,.) if(jj==0) set(handles.edit1,String,a) jj=1; end j=0; function pushbutton16_Callback(hObject, eventdata, handles) a=get(handles.edit1,String) b=eval(a) set(handles.edit1,String,num2str(b^2)) (3)运算符“=”的编程: a=get(handles.edit1,string) b=eval(a) set(handles.edit1,string,num2str(b)) “eval”的作用是将符号表达式转换成数值表达式再由set(handles.edit1,string,num2str(b)) 输出。 (4)按键“back”编程:即显示一个空字符:set(handles.edit1,String,a) 按键“back”编程: global jj a=get(handles.edit1,String); if(strcmp(a,0.)==1)&(jj==0) set(handles.edit1,String,0.) else ss=char(a); l=length(a); a=ss(1:l-1); set(handles.edit1,String,a) end jj=0; (5)按键“清零”:把动态文本框的字符清空,返回一个空格。 set(handles.edit1,String,0) (6)三角函数的编辑 function pushbutton17_Callback(hObject, eventdata, handles) a=get(handles.edit1,String); b=eval(a) b=b*pi/180; b=sin(b); set(handles.edit1,String,b) b=b*pi/180是把角度转换为弧度,这样在编程环境中才能识别,sin才能起作用。然后执行set函数,把结果输出来。同理在cos,tan,cot的回调函数中也输入相应的函数,只需把b=sin(b);中的sin改为cos,tan,cot即可 (7)按键“()”:在输入数据时添加括号,以便数据的优先计算。 global jj a=get(handles.edit1,String) if(strcmp(a,0)==1)&&(jj==0) set(handles.edit1,String,() else a=strcat(a,() set(handles.edit1,String,a) end jj=0 a=get(handles.edit1,String) s1=strcat(a,)) set(handles.edit1,String,s1) 2.2 计算器的使用 加法运算(+): 按“=”后显示: 减法(-)、乘法(*)、除法(/)运算与加法(+)运算类似。 点号(.)、括号(()): 平方(X^2)运算: 按下(X^2)后 三角函数(sin、cos、tan、cot)运算: 按下(sin、cos、tan、cot)后 back、清零功能: 3 程序总结 本程序实现简单的科学运算功能及便捷的图形化交互界面。具有以下优点: 优点: 1、GUI内数据传递非常简便。非常简便的实现了前台与后台、前台与前台、后台与后台之间的参数传递。 2、图形化用户交互界面简洁明了。在制作计算器界面时操作简单,制作完成后程序的输入框直接弹出,可以直接写入,即可运行计算器。简单的实现了设计与编程的数据传递。 4 课程总结 1、通过MATLAB简单计算器的设计,初步了解了关于MATLAB图形用户界面的部分控件的使用方法。 2、MATLAB的GUI提供的很多实用的控件,方便用于设计属于自己的图形界面。 3、Matlab具有强大、丰富的内置函数和工具箱,界面设计时更加简洁、快捷与直观。 5 参考文献 [1] 《MATLAB语言及其在电子信息工程中的应用》 王洪元主编 清华大学出版社 [2] 《MATLAB中GUI的应用》 王洪元主编 清华大学出版社 附录(主要程序) function varargout = untitled66(varargin) % UNTITLED66 MATLAB code for untitled66.fig % UNTITLED66, by itself, creates a new UNTITLED66 or raises the existing % singleton*. % % H = UNTITLED66 returns the handle to a new UNTITLED66 or the handle to % the existing singleton*. % % UNTITLED66(CALLBACK,hObject,eventData,handles,...) calls the local % function named CALLBACK in UNTITLED66.M with the given input arguments. % % UNTITLED66(Property,Value,...) creates a new UNTITLED66 or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before untitled66_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to untitled66_OpeningFcn via varargin. % % *See GUI Options on GUIDEs Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help untitled66 % Last Modified by GUIDE v2.5 09-Dec-2014 20:42:08 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct(gui_Name, mfilename, ... gui_Singleton, gui_Singleton, ... gui_OpeningFcn, @untitled66_OpeningFcn, ... gui_OutputFcn, @untitled66_OutputFcn, ... gui_LayoutFcn, [] , ... gui_Callback, []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before untitled66 is made visible. function untitled66_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to untitled66 (see VARARGIN) % Choose default command line output for untitled66 handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes untitled66 wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = untitled66_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; function edit1_Callback(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,String) returns contents of edit1 as text % str2double(get(hObject,String)) returns contents of edit1 as a double % --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,BackgroundColor), get(0,defaultUicontrolBackgroundColor)) set(hObject,BackgroundColor,white); end % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj a=get(handles.edit1,String); if(strcmp(a,0.)==1)&&(jj==0) set(handles.edit1,String,0.) else a=strcat(a,0) set(handles.edit1,String,a) end jj=0 % --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj a=get(handles.edit1,String); if(strcmp(a,0)==1)&&(jj==0) set(handles.edit1,String,1) else a=strcat(a,1) set(handles.edit1,String,a) end jj=0 % --- Executes on button press in pushbutton3. function pushbutton3_Callback(hObject, eventdata, handles) % hObject handle to pushbutton3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj a=get(handles.edit1,String); if(strcmp(a,0)==1)&&(jj==0) set(handles.edit1,String,2) else a=strcat(a,2) set(handles.edit1,String,a) end jj=0 % --- Executes on button press in pushbutton4. function pushbutton4_Callback(hObject, eventdata, handles) % hObject handle to pushbutton4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj a=get(handles.edit1,String); if(strcmp(a,0)==1)&&(jj==0) set(handles.edit1,String,3) else a=strcat(a,3) set(handles.edit1,String,a) end jj=0 % --- Executes on button press in pushbutton5. function pushbutton5_Callback(hObject, eventdata, handles) % hObject handle to pushbutton5 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj a=get(handles.edit1,String); if(strcmp(a,0)==1)&&(jj==0) set(handles.edit1,String,4) else a=strcat(a,4) set(handles.edit1,String,a) end jj=0 % --- Executes on button press in pushbutton6. function pushbutton6_Callback(hObject, eventdata, handles) % hObject handle to pushbutton6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj a=get(handles.edit1,String); if(strcmp(a,0)==1)&&(jj==0) set(handles.edit1,String,5) else a=strcat(a,5) set(handles.edit1,String,a) end jj=0 % --- Executes on button press in pushbutton7. function pushbutton7_Callback(hObject, eventdata, handles) % hObject handle to pushbutton7 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj a=get(handles.edit1,String); if(strcmp(a,0)==1)&&(jj==0) set(handles.edit1,String,6) else a=strcat(a,6) set(handles.edit1,String,a) end jj=0 % --- Executes on button press in pushbutton8. function pushbutton8_Callback(hObject, eventdata, handles) % hObject handle to pushbutton8 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj a=get(handles.edit1,String); if(strcmp(a,0)==1)&&(jj==0) set(handles.edit1,String,7) else a=strcat(a,7) set(handles.edit1,String,a) end jj=0 % --- Executes on button press in pushbutton9. function pushbutton9_Callback(hObject, eventdata, handles) % hObject handle to pushbutton9 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj a=get(handles.edit1,String); if(strcmp(a,0)==1)&&(jj==0) set(handles.edit1,String,8) else a=strcat(a,8) set(handles.edit1,String,a) end jj=0 % --- Executes on button press in pushbutton10. function pushbutton10_Callback(hObject, eventdata, handles) % hObject handle to pushbutton10 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj a=get(handles.edit1,String); if(strcmp(a,0)==1)&&(jj==0) set(handles.edit1,String,9) else a=strcat(a,9) set(handles.edit1,String,a) end jj=0 % --- Executes on button press in pushbutton11. function pushbutton11_Callback(hObject, eventdata, handles) % hObject handle to pushbutton11 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj global j a=get(handles.edit1,String) a=strcat(a,+) if(jj==0) set(handles.edit1,String,a) jj=1; end j=0; % --- Executes on button press in pushbutton12. function pushbutton12_Callback(hObject, eventdata, handles) % hObject handle to pushbutton12 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj global j a=get(handles.edit1,String) a=strcat(a,-) if(jj==0) set(handles.edit1,String,a) jj=1; end j=0; % --- Executes on button press in pushbutton13. function pushbutton13_Callback(hObject, eventdata, handles) % hObject handle to pushbutton13 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj global j a=get(handles.edit1,String) a=strcat(a,*) if(jj==0) set(handles.edit1,String,a) jj=1; end j=0; % --- Executes on button press in pushbutton14. function pushbutton14_Callback(hObject, eventdata, handles) % hObject handle to pushbutton14 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj global j a=get(handles.edit1,String) a=strcat(a,/) if(jj==0) set(handles.edit1,String,a) jj=1; end j=0; % --- Executes on button press in pushbutton15. function pushbutton15_Callback(hObject, eventdata, handles) % hObject handle to pushbutton15 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj global j a=get(handles.edit1,String) a=strcat(a,.) if(jj==0) set(handles.edit1,String,a) jj=1; end j=0; % --- Executes on button press in pushbutton16. function pushbutton16_Callback(hObject, eventdata, handles) % hObject handle to pushbutton16 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) a=get(handles.edit1,String) b=eval(a) set(handles.edit1,String,num2str(b^2)) % --- Executes on button press in pushbutton17. function pushbutton17_Callback(hObject, eventdata, handles) % hObject handle to pushbutton17 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) a=get(handles.edit1,String); b=eval(a) b=b*pi/180; b=sin(b); set(handles.edit1,String,b) % --- Executes on button press in pushbutton18. function pushbutton18_Callback(hObject, eventdata, handles) % hObject handle to pushbutton18 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) a=get(handles.edit1,String); b=eval(a) b=b*pi/180; b=cos(b); set(handles.edit1,String,b) % --- Executes on button press in pushbutton19. function pushbutton19_Callback(hObject, eventdata, handles) % hObject handle to pushbutton19 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) a=get(handles.edit1,String); b=eval(a) if(mod(b,90)~=0) b=b*pi/180; b=tan(b); set(handles.edit1,String,b) else set(handles.edit1,String,error:??) end % --- Executes on button press in pushbutton20. function pushbutton20_Callback(hObject, eventdata, handles) % hObject handle to pushbutton20 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) a=get(handles.edit1,String); b=eval(a) if(b~=0) b=b*pi/180; b=cot(b); set(handles.edit1,String,b) else set(handles.edit1,String,error) end % --- Executes on button press in pushbutton21. function pushbutton21_Callback(hObject, eventdata, handles) % hObject handle to pushbutton21 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(handles.edit1,String,0) % --- Executes on button press in pushbutton22. function pushbutton22_Callback(hObject, eventdata, handles) % hObject handle to pushbutton22 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj a=get(handles.edit1,String); if(strcmp(a,0.)==1)&(jj==0) set(handles.edit1,String,0.) else ss=char(a); l=length(a); a=ss(1:l-1); set(handles.edit1,String,a) end jj=0; % --- Executes on button press in pushbutton23. function pushbutton23_Callback(hObject, eventdata, handles) % hObject handle to pushbutton23 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj a=get(handles.edit1,String) if(strcmp(a,0)==1)&&(jj==0) set(handles.edit1,String,() else a=strcat(a,() set(handles.edit1,String,a) end jj=0 % --- Executes on button press in pushbutton24. function pushbutton24_Callback(hObject, eventdata, handles) % hObject handle to pushbutton24 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) a=get(handles.edit1,String) s1=strcat(a,)) set(handles.edit1,String,s1) % --- Executes on button press in pushbutton25. function pushbutton25_Callback(hObject, eventdata, handles) % hObject handle to pushbutton25 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) a=get(handles.edit1,string) b=eval(a) set(handles.edit1,string,num2str(b))- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 MATLAB GUI 简单 计算器

链接地址:https://www.zhuangpeitu.com/p-6640006.html