基于某MATLAB-GUI地简单计算器

上传人:沈*** 文档编号:85652460 上传时间:2022-05-06 格式:DOC 页数:24 大小:704.50KB
收藏 版权申诉 举报 下载
基于某MATLAB-GUI地简单计算器_第1页
第1页 / 共24页
基于某MATLAB-GUI地简单计算器_第2页
第2页 / 共24页
基于某MATLAB-GUI地简单计算器_第3页
第3页 / 共24页
资源描述:

《基于某MATLAB-GUI地简单计算器》由会员分享,可在线阅读,更多相关《基于某MATLAB-GUI地简单计算器(24页珍藏版)》请在装配图网上搜索。

1、文档基于MATLAB-GUI的简单计算器设计题目:计算器 完成一个简单的计算器。 要求但不限于: GUI上具有数字键盘输入区域,能够进展加、减、乘、除、三角函数等根底运算。 界面简洁、美观 可能需要的控件: Push Button Edit Text Pop-up Menu1 功能介绍本程序是一个简单的计算器程序,使用MATLAB软件编写完成。主要具有加、减、乘、除、三角函数等根底运算,并通过GUI进展程序使用的交互。程序交互界面如下:图1 程序的交互界面2 功能实现首先用MATLABGUI功能,在绘制一个动态文本框和一个文本编辑框,以与25个命令按钮,调整好各控件大小、颜色,整体布局如下列图

2、:备注:软件版本:MATLAB R2011b2.1 布局GUI1、打开MATLAB,在Guide中新建FIG文件2、然后双击“BlankGUI(Default)或单击OK键出现GUI窗口3、添加按钮和动态文本框4、根据按钮的作用与视觉效果做一定的修改对按钮的字符串大小、颜色进展设置,对按钮的位置进展排布,尽量使按钮集中在动态文本框下面。最终设置的动态文本框为灰色,其他按钮均为蓝色。5、保存、添加功能函数 将做好的按钮与动态文本框保存后自动弹出Editor的M文本,然后对相应的pushbutton添加功能函数。以下是相应按钮的功能函数。1数字按键编写。在functionpushbutton1_C

3、allback(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中输出。同理,分别在functionpushbutton21

4、0_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)。符号键

5、-、*、/、.与+的运算函数类似,“平方运算主要是由“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

6、=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=

7、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)“ev

8、al的作用是将符号表达式转换成数值表达式再由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按键“清零:把动态文本框的字符清空,

9、返回一个空格。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,

10、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运算:按下X

11、2后三角函数sin、cos、tan、cot运算:按下(sin、cos、tan、cot)后back、清零功能:3 程序总结本程序实现简单的科学运算功能与便捷的图形化交互界面。具有以下优点:优点:1、GUI数据传递非常简便。非常简便的实现了前台与后台、前台与前台、后台与后台之间的参数传递。2、图形化用户交互界面简洁明了。在制作计算器界面时操作简单,制作完成后程序的输入框直接弹出,可以直接写入,即可运行计算器。简单的实现了设计与编程的数据传递。4 课程总结1、通过MATLAB简单计算器的设计,初步了解了关于MATLAB图形用户界面的局部控件的使用方法。2、MATLAB的GUI提供的很多实用的控件,方

12、便用于设计属于自己的图形界面。3、Matlab具有强大、丰富的置函数和工具箱,界面设计时更加简洁、快捷与直观。5 参考文献1 MATLAB语言与其在电子信息工程中的应用 王洪元主编 清华大学2 MATLAB中GUI的应用 王洪元主编 清华大学附录主要程序function varargout = untitled66(varargin)% UNTITLED66, by itself, creates a new UNTITLED66 or raises the existing% singleton*.% H = UNTITLED66 returns the handle to a new UN

13、TITLED66 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

14、left, property value pairs are% applied to the GUI before untitled66_OpeningF gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to untitled66_OpeningF via varargin.% *See GUI Options on GUIDEs Tools menu. Choose GUI allows only one%

15、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_Si

16、ngleton, .gui_OpeningF, untitled66_OpeningF, .gui_OutputF, untitled66_OutputF, .gui_LayoutF, , .gui_Callback, );if nargin & ischar(varargin1) gui_State.gui_Callback = str2func(varargin1);endif nargout varargout1:nargout = gui_mainf(gui_State, varargin:);else gui_mainf(gui_State, varargin:);end% End

17、initialization code - DO NOT EDIT% - Executes just before untitled66 is made visible.function untitled66_OpeningF(hObject, eventdata, handles, varargin)% This function has no output args, see OutputF.% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handle

18、s structure with handles and user data (see GUIDATA)% varargin mand line arguments to untitled66 (see VARARGIN)% Choose default mand line output for untitled66handles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes untitled66 wait for user response (see UIRESUME)%

19、 uiwait(handles.figure1);% - Outputs from this function are returned to the mand line.function varargout = untitled66_OutputF(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT);% hObject handle to figure% eventdata reserved - to be defined in a future versi

20、on of MATLAB% handles structure with handles and user data (see GUIDATA)% Get default mand 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

21、 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_CreateF(hObject, eve

22、ntdata, 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 CreateFs called% Hint: edit controls usually have a white background on Windows.% See ISPC and PUTER.if ispc & isequal(get(hObje

23、ct,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 o

24、f 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, eventd

25、ata, 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.

26、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 j

27、ja=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 -

28、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 pus

29、hbutton5_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

30、,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 an

31、d 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

32、(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

33、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

34、)&(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%

35、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, handl

36、es)% 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,Str

37、ing,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 jjgloba

38、l 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 MAT

39、LAB% 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 pushbutto

40、n13 (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 pu

41、shbutton14_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,

42、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

43、 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 versio

44、n 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)% eventdat

45、a 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, hand

46、les)% 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 pushbutton1

47、9.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

48、(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 struct

49、ure 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 pus

50、hbutton21 (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 pus

51、hbutton22 (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,

52、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(ha

53、ndles.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 de

54、fined 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)24 / 24

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

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

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


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