基于MATLAB-GUI的简单计算器.doc_第1页
基于MATLAB-GUI的简单计算器.doc_第2页
基于MATLAB-GUI的简单计算器.doc_第3页
基于MATLAB-GUI的简单计算器.doc_第4页
基于MATLAB-GUI的简单计算器.doc_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于MATLAB-GUI的简单计算器设计题目:计算器 完成一个简单的计算器。 要求(但不限于): GUI上具有数字键盘输入区域,能够进行加、减、乘、除、三角函数等基础运算。 界面简洁、美观 可能需要的控件: Push Button Edit Text Pop-up Menu1 功能介绍本程序是一个简单的计算器程序,使用MATLAB软件编写完成。主要具有加、减、乘、除、三角函数等基础运算,并通过GUI进行程序使用的交互。程序交互界面如下:图1 程序的交互界面2 功能实现首先用MATLABGUI功能,在绘制一个动态文本框和一个文本编辑框,以及25个命令按钮,调整好各控件大小、颜色,整体布局如图所示:备注:软件版本:MATLAB R2011b2.1 布局GUI1、打开MATLAB,在Guide中新建FIG文件2、然后双击“BlankGUI(Default)”或单击OK键出现GUI窗口3、添加按钮和动态文本框4、根据按钮的作用及视觉效果做一定的修改对按钮的字符串大小、颜色进行设置,对按钮的位置进行排布,尽量使按钮集中在动态文本框下面。最终设置的动态文本框为灰色,其他按钮均为蓝色。5、保存、添加功能函数 将做好的按钮及动态文本框保存后自动弹出Editor的M文本,然后对相应的pushbutton添加功能函数。以下是相应按钮的功能函数。(1)数字按键编写。在functionpushbutton1_Callback(hObject,eventdata,handles)下输入:global jja=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)endjj=0这是使用句柄handles指向对象edit1,并以字符串形式来存储数据文本框edit1的内容,并存储数个“0”,然后由set(handles.edit1,String,a)在edit1中输出。同理,分别在functionpushbutton210_Callback(hObject,eventdata,handles)下给19数字按键下编写此类程序。(2)符号键:在functionpushbutton11_Callback(hObject,eventdata,handles)下输入:global jjglobal ja=get(handles.edit1,String)a=strcat(a,+)if(jj=0)set(handles.edit1,String,a)jj=1;endj=0;strcat的作用是将两个字符串连接起来,就是在已输入的存储数据a后添加“+”进行运算。然后执行set(handles.edit1,String,a)。符号键-、*、/、.与+的运算函数类似,“平方运算”主要是由“2”功能实现。function pushbutton12_Callback(hObject, eventdata, handles)global jjglobal ja=get(handles.edit1,String)a=strcat(a,-)if(jj=0)set(handles.edit1,String,a)jj=1;endj=0;function pushbutton13_Callback(hObject, eventdata, handles)global jjglobal ja=get(handles.edit1,String)a=strcat(a,*)if(jj=0)set(handles.edit1,String,a)jj=1;endj=0; function pushbutton14_Callback(hObject, eventdata, handles)global jjglobal ja=get(handles.edit1,String)a=strcat(a,/)if(jj=0)set(handles.edit1,String,a)jj=1;endj=0;function pushbutton15_Callback(hObject, eventdata, handles)global jjglobal ja=get(handles.edit1,String)a=strcat(a,.)if(jj=0)set(handles.edit1,String,a)jj=1;endj=0;function pushbutton16_Callback(hObject, eventdata, handles)a=get(handles.edit1,String)b=eval(a)set(handles.edit1,String,num2str(b2)(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 jja=get(handles.edit1,String);if(strcmp(a,0.)=1)&(jj=0)set(handles.edit1,String,0.)elsess=char(a);l=length(a);a=ss(1:l-1);set(handles.edit1,String,a)endjj=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 jja=get(handles.edit1,String)if(strcmp(a,0)=1)&(jj=0) set(handles.edit1,String,()elsea=strcat(a,()set(handles.edit1,String,a)endjj=0a=get(handles.edit1,String)s1=strcat(a,)set(handles.edit1,String,s1)2.2 计算器的使用加法运算(+):按“=”后显示:减法(-)、乘法(*)、除法(/)运算与加法(+)运算类似。点号(.)、括号():平方(X2)运算:按下(X2)后三角函数(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 EDITgui_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(varargin1) gui_State.gui_Callback = str2func(varargin1);end if nargout varargout1: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 untitled66handles.output = hObject; % Update handles structureguidata(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 structurevarargout1 = 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 jja=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)endjj=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 jja=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)endjj=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 jja=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)endjj=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 jja=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)endjj=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 jja=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)endjj=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 jja=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)endjj=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 jja=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)endjj=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 jja=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)endjj=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 jja=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)endjj=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 jja=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)endjj=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 jjglobal ja=get(handles.edit1,String)a=strcat(a,+)if(jj=0)set(handles.edit1,String,a)jj=1;endj=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 jjglobal ja=get(handles.edit1,String)a=strcat(a,-)if(jj=0)set(handles.edit1,String,a)jj=1;endj=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 jjglobal ja=get(handles.edit1,String)a=strcat(a,*)if(jj=0)set(handles.edit1,String,a)jj=1;endj=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 jjglobal ja=get(handles.edit1,String)a=strcat(a,/)if(jj=0)set(handles.edit1,String,a)jj=1;endj=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 jjglobal ja=get(handles.edit1,String)a=strcat(a,.)if(jj=0)set(handles.edit1,String,a)jj=1;endj=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(b2) % - 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)elseset(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)elseset(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 de

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论