收藏 分销(赏)

matlab简易计算器设计步骤与回调函数设计.doc

上传人:a199****6536 文档编号:2560672 上传时间:2024-05-31 格式:DOC 页数:19 大小:908.04KB
下载 相关 举报
matlab简易计算器设计步骤与回调函数设计.doc_第1页
第1页 / 共19页
matlab简易计算器设计步骤与回调函数设计.doc_第2页
第2页 / 共19页
matlab简易计算器设计步骤与回调函数设计.doc_第3页
第3页 / 共19页
matlab简易计算器设计步骤与回调函数设计.doc_第4页
第4页 / 共19页
matlab简易计算器设计步骤与回调函数设计.doc_第5页
第5页 / 共19页
点击查看更多>>
资源描述

1、matlab简易计算器设计步骤与回调函数设计 作者: 日期:19 个人收集整理 勿做商业用途简易算术计算器的设计一、算术计算器的功能:能进行简单的加、减、乘、除法运算,输入文本框显示为常量多项式,输出文本框显示为精确到小数点后十位的数值.计算器包含十个数字键09、小数点键“.、小括号键“(”“)、运算符号、清屏键、退格键、退出键等。二、设计思路:每按下一个数字键或者符号键的时候,利用get(handles.edit1,string)获取当前输入文本框edit1的string属性,并通过字符串合并函数strcat()将当前输入文本框edit1的字符型string属性与按键的字符型string属性

2、合并成字符串,然后利用属性设置函数set(); 将合并后的字符串返回到输入文本框edit1的string属性.当输入结束,用户按下等号的时候,采用x=get(handles.edit1,string)获取当前输入文本框edit1的string属性,然后利用字符串执行命令y1=eval(x)计算输入文本框的表达式,得到的结果是字符型常量;然后利用字符串输出格式控制函数y=sprintf(。10f,y1)使输出结果精确到小数点后十位;最后利用属性设置函数set(handles.edit2,string,y)将转换后的字符串返回到输出文本框edit2的string属性。三、设计步骤:1、 运行mat

3、lab 7。13版,进入主界面.2、 点击工具栏上的GUIDE图标进入GUI开发环境。3、 创建控件:Push Button包括数字09、运算符“+ - ”、功能键等十九个,输入和动态静态文本框Edit Text,显示文字的静态文本框Static Text四个等.4、 对控件单击右键弹出菜单,选择Property Inspector,设置控件属性.控件属性表控件Style类型TagStringFontSizeFontUnitsFontWeightVisibleBackgroundColorForegroundColorredgreenblueredgreenbluepushbuttonPush

4、button1116pointsboldon00。4980110Pushbutton22Pushbutton33Pushbutton44Pushbutton55Pushbutton66Pushbutton77Pushbutton88Pushbutton99pushbutton100pushbutton1143pushbutton12pushbutton13pushbutton14pushbutton15=16pushbutton16。pushbutton17Exitpushbutton18Deletepushbutton19ClcPushbutton20(Pushbutton21)textte

5、xt1制作:*16pointsboldon0.8310。8160.784001text2Inputtext3Outputtext4算术计算器200。74900.749editedit1空白字符串10pointson111000edit2空白字符串5、 选择工具栏上的Align Objects按键,调整控件布局.6、 选择工具栏上的Tab Oder Editor按键,通过和设置各控件的Tab顺序。7、设计菜单:在GUI开发环境下,选择工具栏上的进入菜单编辑器,分别选择和创建主菜单和子菜单。8、编写回调函数。(1) 数字键0的回调函数: - Executes on button press in

6、pushbutton3。function pushbutton10_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)%数字键0的回调函数: %get函数获取输入文本框edit1的string属性,赋值给numbers,数值类型为char;numb

7、ers=get(handles。edit1,string); %字符串合并函数strcat()将按键”0与numbers的值合并,将合并后的字符串返回到输入文本框edit1的string属性;set(handles。edit1,string,strcat(numbers,0);同理编写其余数字键、小数点键、小括号键、运算符号键;(2) 数字键1的回调函数:% - Executes on button press in pushbutton7。function pushbutton1_Callback(hObject, eventdata, handles) hObject handle to

8、pushbutton7 (see GCBO)% eventdata reserved to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%数字键1的回调函数:numbers=get(handles。edit1,string);set(handles。edit1,string,strcat(numbers,1));(3) 数字键2的回调函数:% - Executes on button press in pushbutton7.functio

9、n pushbutton2_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)%数字键2的回调函数:numbers=get(handles.edit1,string);set(handles.edit1,string,strcat(numb

10、ers,2);(4) 数字键3的回调函数:% - Executes on button press in pushbutton10.function pushbutton3_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)%数字键3的回调函数

11、:numbers=get(handles。edit1,string);set(handles。edit1,string,strcat(numbers,3));(5) 数字键4的回调函数:% - Executes on button press in pushbutton9。function pushbutton4_Callback(hObject, eventdata, handles) hObject handle to pushbutton9 (see GCBO) eventdata reserved - to be defined in a future version of MATLA

12、B% handles structure with handles and user data (see GUIDATA)数字键4的回调函数:numbers=get(handles.edit1,string);set(handles。edit1,string,strcat(numbers,4);(6) 数字键5的回调函数: Executes on button press in pushbutton7。function pushbutton5_Callback(hObject, eventdata, handles) hObject handle to pushbutton7 (see GCB

13、O) eventdata reserved to be defined in a future version of MATLAB handles structure with handles and user data (see GUIDATA)数字键5的回调函数:numbers=get(handles.edit1,string);set(handles。edit1,string,strcat(numbers,5);(7) 数字键6的回调函数: - Executes on button press in pushbutton6。function pushbutton6_Callback(hO

14、bject, 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)%数字键6的回调函数:numbers=get(handles。edit1,string);set(handles.edit1,string,strcat(numbers,6));(8) 数字键7的回调函数:% - Exe

15、cutes on button press in pushbutton5。function pushbutton7_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)数字键7的回调函数:numbers=get(handles.edit1,stri

16、ng);set(handles。edit1,string,strcat(numbers,7);(9) 数字键8的回调函数: - 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

17、 and user data (see GUIDATA)数字键8的回调函数:numbers=get(handles。edit1,string);set(handles。edit1,string,strcat(numbers,8);(10) 数字键9的回调函数: - Executes on button press in pushbutton4.function pushbutton9_Callback(hObject, eventdata, handles) hObject handle to pushbutton4 (see GCBO)% eventdata reserved to be d

18、efined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%数字键9的回调函数:numbers=get(handles.edit1,string);set(handles.edit1,string,strcat(numbers,9));(11) 小数点“。”键的回调函数: - Executes on button press in pushbutton17。function pushbutton16_Callback(hObject, eventdata, ha

19、ndles) 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)%小数点的回调函数;point=get(handles。edit1,string);set(handles.edit1,string,strcat(point,。));(12) 加号“+”键的回调函数:% - Executes on button pre

20、ss in pushbutton10。function pushbutton11_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)加号“+”键的回调函数;signs=get(handles.edit1,string);set(handles.

21、edit1,string,strcat(signs,+);(13) 减号“”键的回调函数: - Executes on button press in pushbutton9.function pushbutton12_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

22、(see GUIDATA)减号“”键的回调函数;signs=get(handles。edit1,string);set(handles。edit1,string,strcat(signs,);(14) 乘号“”键的回调函数:% - Executes on button press in pushbutton7。function pushbutton13_Callback(hObject, eventdata, handles)% hObject handle to pushbutton7 (see GCBO) eventdata reserved to be defined in a futu

23、re version of MATLAB handles structure with handles and user data (see GUIDATA)%乘号“”键的回调函数;signs=get(handles。edit1,string);set(handles。edit1,string,strcat(signs,);(15) 除号“”键的回调函数: - Executes on button press in pushbutton8.function pushbutton14_Callback(hObject, eventdata, handles)% hObject handle to

24、 pushbutton8 (see GCBO) eventdata reserved to be defined in a future version of MATLAB handles structure with handles and user data (see GUIDATA)除号“键的回调函数;signs=get(handles.edit1,string);set(handles。edit1,string,strcat(signs,/);(16) 左括号“(”键的回调函数: - Executes on button press in pushbutton21。function p

25、ushbutton21_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)左括号“(”键的回调函数;signs=get(handles。edit1,string);set(handles。edit1,string,strcat(signs,())

26、;(17) 右括号“)键的回调函数:% - 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)%右括号“)”键的回调函

27、数;signs=get(handles.edit1,string);set(handles.edit1,string,strcat(signs,);(18) 等号“=”号键的回调函数:% - 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 MATL

28、AB handles structure with handles and user data (see GUIDATA)%等号“=”号键的回调函数;get函数获取输入文本框edit1的string属性,并赋值与x;x=get(handles。edit1,string);利用执行字符串命令eval()计算用户输入的表达式,得到的结果为char型;y1=eval(x);%利用字符串输出格式控制函数sprintf()将字符型常量精确到小数点后十位;y=sprintf(%.10f,y1);%利用属性设置函数set()将字符型结果返回到输出文本框edit2的string属性;set(handles。e

29、dit2,string,y);(19) CLC键的回调函数:% - 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 GUIDAT

30、A)CLC键的回调函数;清空输入文本框edit1的string属性;set(handles。edit1,String,);%清空输出文本框edit2的string属性;set(handles.edit2,String,0);(20) Delete键的回调函数: - Executes on button press in pushbutton18。function pushbutton18_Callback(hObject, eventdata, handles) hObject handle to pushbutton18 (see GCBO) eventdata reserved - to

31、be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)Delete键的回调函数;%get()函数获取当前输入文本框的string属性,并赋值与s1;s1=get(handles.edit1,string);length(s1)函数获取s1字符串的长度,并利用s1(1:length(s1)-1)获取s1的第一个到第(length(s1)1)个字符串,已达到删除前一个字符的目的;s2=s1(1:(length(s1)1));利用属性设置函数set()将字

32、符型结果返回到输入文本框edit1的string属性;set(handles.edit1,string,s2);(21) Exit键的回调函数: - 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 structu

33、re with handles and user data (see GUIDATA)Exit键的回调函数;close;(22) 菜单“关于”的回调函数: -function Untitled_8_Callback(hObject, eventdata, handles) hObject handle to Untitled_8 (see GCBO) eventdata reserved to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)显示的版权信息;message1=sprintf(Copyright(C) 2012 *nn,Version1.0nn,MyCalculatornn);读取图像,供关于消息框显示图标;IconData,IconCMap=imread(MyCalculator。png);弹出消息框;about=msgbox(message1,版权信息,custom,IconData,IconCMap);在按下确定按钮,关闭关于消息框前,不做其他操作;waitfor(about);7、调试运行程序:

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服