




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、姓名: 曹慈航 班级: 12级电气自动化2班 学号:1208441044 基于MATLAB-GUI的简单计算器设计题目:计算器 完成一个简单的计算器。 要求(但不限于): GUI上具有数字键盘输入区域,能够进行加、减、乘、除、三角函数等基础运算。 界面简洁、美观 可能需要的控件: Push Button Edit Text Pop-up Menu1 功能介绍本程序是一个简单的计算器程序,使用MATLAB软件编写完成。主要具有加、减、乘、除、三角函数等基础运算,并通过GUI进行程序使用的交互。程序交互界面如下:图1 程序的交互界面2 功能实现首先用MATLAB GUI功能,在绘制一个动
2、态文本框和一个文本编辑框,以及25个命令按钮,调整好各控件大小、颜色,整体布局如图所示:备注:软件版本:MATLAB R2011b2.1 布局GUI1、打开MATLAB,在Guide中新建FIG文件2、然后双击“Blank GUI(Default)”或单击OK键出现GUI窗口3、添加按钮和动态文本框4、根据按钮的作用及视觉效果做一定的修改 对按钮的字符串大小、颜色进行设置,对按钮的位置进行排布,尽量使按钮集中在动态文本框下面。最终设置的动态文本框为灰色,其他按钮均为蓝色。 5、保存、添加功能函数 将做好的按钮及动态文本框保存后自
3、动弹出Editor的M文本,然后对相应的pushbutton添加功能函数。以下是相应按钮的功能函数。 (1)数字按键编写。 在function pushbutton1_Callback(hObject, eventdata, handles)下输入:global jja=get(handles.edit1,'String');if(strcmp(a,'0.')=1)&&(jj=0) set(handles.edit1,'String','0.')else
4、 a=strcat(a,'0')set(handles.edit1,'String',a)endjj=0这是使用句柄handles指向对象edit1,并以字符串形式来存储数据文本框edit1的内容,并存储数个“0”, 然后由set(handles.edit1,'String',a)在edit1中输出。 同理,分别在function pushbutton210_Callback(hObject, eventdata, handles)下给19数字按键下编写此类程序。 (2)符号键:
5、0;在function pushbutton11_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,&
6、#39;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
7、, 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)
8、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
9、)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”编程:即显
10、示一个空字符: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)按键“清零”:把动态文本框的字符清空
11、,返回一个空格。 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函数,把结果输出来。同
12、理在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)en
13、djj=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内数据传递非常简便。非常
14、简便的实现了前台与后台、前台与前台、后台与后台之间的参数传递。2、图形化用户交互界面简洁明了。在制作计算器界面时操作简单,制作完成后程序的输入框直接弹出,可以直接写入,即可运行计算器。简单的实现了设计与编程的数据传递。4 课程总结1、通过MATLAB简单计算器的设计,初步了解了关于MATLAB图形用户界面的部分控件的使用方法。 2、MATLAB的GUI提供的很多实用的控件,方便用于设计属于自己的图形界面。 3、Matlab具有强大、丰富的内置函数和工具箱,界面设计时更加简洁、快捷与直观。5 参考文献1 MATLAB语言及其在电子信息工程中的应用 王洪元主编 清华大学出版社2
15、 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 singlet
16、on*.% 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 va
17、lue 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 GUIDE's Tools menu. Choose "GUI allows only one% in
18、stance 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, .
19、9;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 varargo
20、ut1: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
21、.% 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; % Updat
22、e 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 re
23、turning 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(hO
24、bject, 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
25、9;) 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
26、 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 %
27、 - 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,
28、39;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
29、 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=str
30、cat(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 ha
31、ndles 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 pus
32、hbutton4_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)&&(j
33、j=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 de
34、fined 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)en
35、djj=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.e
36、dit1,'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 h
37、andle 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
38、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 wi
39、th 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.functio
40、n 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)&&a
41、mp;(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 -
42、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
43、39;,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 jjglob
44、al 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 de
45、fined 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_Cal
46、lback(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.
47、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 (s
48、ee 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)% event
49、data 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.f
50、unction 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,'Stri
51、ng',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(h
52、andles.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 versio
53、n 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
54、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,'Str
55、ing','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 d
56、ata (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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 政策效果评估的方法与技术研究及答案
- 机电工程新知试题及答案
- 机电工程专业发展试题及答案
- 数据中心网络架构解析与试题及答案
- 机电工程技术新趋势2025年试题及答案
- 管理变更对项目影响的评估试题及答案
- 自查自纠2025年管理师试题及答案
- 网络投资回报分析模型试题及答案
- 项目团队建设中的信任管理试题及答案
- 软件设计师考试经验分享与试题及答案
- 2025年湖南长沙穗城轨道交通限公司社会招聘261人高频重点模拟试卷提升(共500题附带答案详解)
- 应急药品知识培训课件
- 差分进化算法研究
- 2025年湖北省武汉城市职业学院面向社会招聘人事代理人员27人历年高频重点提升(共500题)附带答案详解
- 国家开放大学《经济学(本)》形考任务1-6答案
- 职业教育与成人教育科2024年工作总结
- T-CNAS 12─2020 成人经口气管插管机械通气患者口腔护理
- T∕CACM 1021.92-2018 中药材商品规格等级 独活
- 车位租赁协议
- DB11T 1382-2022 空气源热泵系统应用技术规程
- 气压传动课件 项目六任务二 吸吊机气动系统回路
评论
0/150
提交评论