您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据库 > 用MATLAB做一个计算器_包括加减乘除_平方_清空等功能
广西科技大学MATLAB课程设计说明书课题:设计简易计算器开发环境:MATLABGUIDE作者:系别:专业:时间:实验目的:1、熟悉MATLAB的主要控件使用方法。2、熟悉MATLAB的GUI设计流程。实验环境:编程软件:MATLAB7.0实验内容与结果:使用MATLAB的GUI接口设计一个简单的计算器。效果图:一、布局GUI。1.打开Matlab,输入Guide回车或者在工具栏上点击图标出现Guide窗口:2.然后双击“BlankGUI(Default)”出现GUI窗口3.添加按钮4.根据按钮的作用及视觉效果做一定的修改把按钮的字符串大小、颜色进行设置,对按钮的位置进行排布,尽量使按钮集中在静态文本框下面。最终设置的静态文本框为白色,其他按钮均为分红色。5.保存、添加功能函数把做好的按钮及静态文本框保存后自动弹出Editor的M文本,对然后对相应的pushbutton添加功能函数。以下是相应按钮的功能函数。(1)数字按键编写。在functionpushbutton1_Callback(hObject,eventdata,handles)下输入:textString=get(handles.text1,'String');textString=strcat(textString,'0');set(handles.text1,'String',textString)这是使用句柄handles指向对象text1,并以字符串形式来存储数据文本框text1的内容,并存储数个“0”,然后由set(handles.text1,'String','textString'在text1中输出。同理,分别在functionpushbutton2~10_Callback(hObject,eventdata,handles)下给1~9数字按键下编写此类程序。(2).符号键:functionpushbutton12_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String');textString=strcat(textString,'+');set(handles.text1,'String',textString)strcat的作用是将两个字符串连接起来,就是在已输入的存储数据textString后添加“+”进行运算。然后执行set(handles.text1,'String',textString)。符号键‘-’、‘*’、‘/’与‘+’的运算函数类似。“平方运算”,主要是由“^2”功能实现。(3)运算符“=”的编程:textString=get(handles.text1,'String');ans=eval(textString);“eval”的作用是将符号表达式转换成数值表达式。再由set(handles.text1,'String',ans)输出。(4)按键“back”编程:即显示一个空字符:set(handles.text1,'String','');按键“back”编程:textString=get(handles.text1,'String');set(handles.text1,'String','');ss=char(textString);n=length(textString);textString=ss(1:w-1);%将w个字符串转成w-1个,即删除了末位字符set(handles.text1,'String',textString)(5)按键“清空”:把静态文本框的字符清空,返回一个空格set(handles.text1,'String','')(6)按键“退出”:关闭计算器close(gcf);二、计算器的使用加法运算(+):按“=”后显示平方运算:按“=”后显示附各按键的程序源代码:functionpushbutton1_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')textString=strcat(textString,'0')set(handles.text1,'String',textString)functionpushbutton2_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')textString=strcat(textString,'1')set(handles.text1,'String',textString)functionpushbutton3_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')textString=strcat(textString,'2')set(handles.text1,'String',textString)functionpushbutton4_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')textString=strcat(textString,'3')set(handles.text1,'String',textString)functionpushbutton5_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')textString=strcat(textString,'4')set(handles.text1,'String',textString)functionpushbutton6_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')textString=strcat(textString,'5')set(handles.text1,'String',textString)functionpushbutton7_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')textString=strcat(textString,'6')set(handles.text1,'String',textString)functionpushbutton8_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')textString=strcat(textString,'7')set(handles.text1,'String',textString)functionpushbutton9_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')textString=strcat(textString,'8')set(handles.text1,'String',textString)functionpushbutton10_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')textString=strcat(textString,'9')set(handles.text1,'String',textString)functionpushbutton11_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')textString=strcat(textString,'+')set(handles.text1,'String',textString)functionpushbutton12_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')textString=strcat(textString,'-')set(handles.text1,'String',textString)functionpushbutton13_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')textString=strcat(textString,'*')set(handles.text1,'String',textString)functionpushbutton14_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')textString=strcat(textString,'/')set(handles.text1,'String',textString)functionpushbutton15_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')textString=strcat(textString,'.')set(handles.text1,'String',textString)functionpushbutton16_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')s=eval(textString)set(handles.text1,'String',s)functionpushbutton17_Callback(hObject,eventdata,handles)textString=get(handles.text1,'String')textString=strcat(textString,'^2')set(handles.text1,'String',textString)functionpushbutton18_Callback(hObject,eventdata,handles)%hObjecthandletopushbutton18(seeGCBO)%eventdatareserved-tobedefinedinafutureversionofMATLAB%handlesstructurewithhandlesanduserdata(seeGUIDATA)textString=get(handles.text1,'String')w=length(textString)t=char(textString)textString=t(1:w-1)set(handles.text1,'String',textString)functionpushbutton19_Callback(hObject,eventdata,handles)set(handles.text1,'String','')functionpushbutton20_Callback(hObject,eventdata,handles)%hObjecthandletopushbutton20(seeGCBO)%eventdatareserved-tobedefinedinafutureversionofMATLAB%handlesstructurewithhandlesanduserdata(seeGU
本文标题:用MATLAB做一个计算器_包括加减乘除_平方_清空等功能
链接地址:https://www.777doc.com/doc-4989932 .html