基于MATLAB-GUI的简单计算器.doc

上传人:xin****828 文档编号:6640006 上传时间:2020-03-01 格式:DOC 页数:24 大小:706KB
返回 下载 相关 举报
基于MATLAB-GUI的简单计算器.doc_第1页
第1页 / 共24页
基于MATLAB-GUI的简单计算器.doc_第2页
第2页 / 共24页
基于MATLAB-GUI的简单计算器.doc_第3页
第3页 / 共24页
点击查看更多>>
资源描述
基于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 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 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; % - 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 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=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)
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 其他分类 > 大学论文


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!