1、1.需求分析本次的实验要求是设计一个计算器,主要功能如下:(1)实现基本数学运算(加减乘除等),而且要能进行混合运算(2)实现部分函数功能,如求平方根、求倒数等(3)能实现小数运算界面与标准计算器界面类似根据要求以及以前的学习情况,决定使用matlab进行编程。Matlab强大的计算功能以及便捷的GUI设计,可以较为简便的实现所要求的功能。按照要求,数据输入和输出支持小数点,支持四则混合运算,决定使用如下几个数据进行分析:(1+3)*5Sqrt(4)1/2Sin4用以检验是否可以进行加减乘除四则运算、平方根、倒数、正弦的运算。2.程序设计 Matlab的程序设计较为简便,用GUI设计出一个计算
2、器的模型,然后系统会自动生成一个框架,在框架中,写入每一个按键对应的程序就可以实现功能。3.调式分析编程的过程中遇到的问题不是很多,基本就是找要实现各个功能的子程序,通过上网和去图书馆,加上自己的编写,终于实现了实验要求的功能。但是有一点很重要,matlab不支持中文,所以从路径到文件名必须是全英文的,不然就无法识别。此外,给每个按键命名也是很重要的,不然在生成的程序框架里面,就无法识别各个按键的作用,编写程序的时候也就无法做到一一对应。4.使用说明程序的使用比较简单,由于是可视化界面,直接打开matlab,然后建立一个GUI工程,再打开生成的fig文件,就是一个计算器的界面,直接按照市面上卖
3、的计算器的方法,按键使用即可。5.测试结果计算结果为204sqrt=2Sin4结果为1/2=0.5经过计算,这些结果均与实际结果相吻合,计算器的功能实现的较为完好。6.心得体会本次试验由于不限制语言,于是计算功能强大,操作简便的matlab变成了首选,matlab的GUI设计,操作是较为简单的,首先建立一个GUI工程,然后用可视化界面,设计出计算器的大致外观,为每一个按键设置预计的功能,然后就是将对应的函数写到框架生成的程序中,然后每个按键就可以实现对应的功能。在设计的过程中还是遇到很多的困难的,首先就是程序的问题,搭建出计算器的外观以后,生成的框架程序是一个看似非常杂乱的程序,每个按键对应什
4、么功能根本看不出来,于是我在原来的工程中修改了每一个按键的名称。编程的过程中,主要就是对全局变量和句柄的控制,对相应的值赋值以后,要返回给主程序,然后就可以在static text中显示出来的。句柄是一个很重要的概念,基本所有的操作都是通过句柄实现的,MATLAB在创建每一个图形对象时,都为该对象分配唯一的一个值,称其为图形对象句柄(Handle)。句柄是图形对象的唯一标识符,不同对象的句柄不可能重复和混淆。例如:set(句柄,属性名1,属性值1,属性名2,属性值2,),基本所有关联到图形操作的函数语句都是这样的格式:函数名(句柄,进行的操作),因此句柄是一个图形的名称,是对图形进行操作的媒介
5、。此外,由于matlab是全英文的操作环境,所以所有的中文路径和中文的文件夹名称均不支持,不然就会在程序中报错,虽然可以打开,但是按键不可以实现功能,这点是要特别留意的,一般情况下,直接将文件保存在默认的work路径下即可。经过老师的指点,我修改了界面,使其更加美观,可惜的是matla无法实现一个窗口的单独计算器,这不得不说是一个遗憾吧。本次试验本来打算使用VB进行编程的,但是VB的计算功能不够强大,所以在处理四则运算的时候比较麻烦,于是最后还是决定选择matlab,经过这次实习,我对matlab的操作与GUI界面射界有了更深一步的理解,知道了回调函数的基本写法以及各个功能的基本函数的内容,感
6、觉受益匪浅。附录A程序以及注释(注:前面带有%的句子是程序自动生成的框架程序)%定义函数function varargout = untitled(varargin)% Last Modified by GUIDE v2.5 29-Jun-2009 16:06:25% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct(gui_Name, mfilename, . gui_Singleton, gui_Singleton, . gui_OpeningFcn, untitled_OpeningFc
7、n, . gui_OutputFcn, untitled_OutputFcn, . gui_LayoutFcn, , . gui_Callback, );if nargin & ischar(varargin1) gui_State.gui_Callback = str2func(varargin1);endif nargout varargout1:nargout = gui_mainfcn(gui_State, varargin:);else gui_mainfcn(gui_State, varargin:);end% End initialization code - DO NOT ED
8、ITfunction untitled_OpeningFcn(hObject, eventdata, handles, varargin)handles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes untitled wait for user response (see UIRESUME)% uiwait(handles.figure1);% 定义全局变量jj,shift用于数字的设定global jj shift %设置句柄,用于将按键接收的值返回给主程序set(han
9、dles.text1,String,0.); jj=0;shift=0;% - Outputs from this function are returned to the command line.function varargout = untitled_OutputFcn(hObject, eventdata, handles) varargout1 = handles.output;% - Executes during object creation, after setting all properties.function edit1_CreateFcn(hObject, eve
10、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 CreateFcns called% - 按键2响应function pushbutton2_Callback(hObject, eventdata, handles)global jjtextString = get(handles.text1,Stri
11、ng);if(strcmp(textString,0.)=1)&(jj=0) set(handles.text1,String,2) ;elsetextString =strcat(textString,2);set(handles.text1,String,textString)endjj=0;% - 按键3响应function pushbutton3_Callback(hObject, eventdata, handles)global jjtextString = get(handles.text1,String);if(strcmp(textString,0.)=1)&(jj=0) s
12、et(handles.text1,String,3) ;elsetextString =strcat(textString,3);set(handles.text1,String,textString)endjj=0;% - 按键4响应function pushbutton4_Callback(hObject, eventdata, handles)global jjtextString = get(handles.text1,String);if(strcmp(textString,0.)=1)&(jj=0) set(handles.text1,String,4) ;elsetextStri
13、ng =strcat(textString,4);set(handles.text1,String,textString)endjj=0;% - 按键5响应function pushbutton5_Callback(hObject, eventdata, handles)global jjtextString = get(handles.text1,String);if(strcmp(textString,0.)=1)&(jj=0) set(handles.text1,String,5) ;elsetextString =strcat(textString,5);set(handles.tex
14、t1,String,textString)endjj=0;% - 按键6响应function pushbutton6_Callback(hObject, eventdata, handles)global jjtextString = get(handles.text1,String);if(strcmp(textString,0.)=1)&(jj=0) set(handles.text1,String,6) ;elsetextString =strcat(textString,6);set(handles.text1,String,textString)endjj=0;% - 按键7响应fu
15、nction pushbutton7_Callback(hObject, eventdata, handles)global jjtextString = get(handles.text1,String);if(strcmp(textString,0.)=1)&(jj=0) set(handles.text1,String,7) ;elsetextString =strcat(textString,7);set(handles.text1,String,textString)endjj=0;% - 按键8响应function pushbutton8_Callback(hObject, eve
16、ntdata, handles)global jjtextString = get(handles.text1,String);if(strcmp(textString,0.)=1)&(jj=0) set(handles.text1,String,8) ;elsetextString =strcat(textString,8);set(handles.text1,String,textString)endjj=0;% - 按键9响应function pushbutton9_Callback(hObject, eventdata, handles)global jjtextString = ge
17、t(handles.text1,String);if(strcmp(textString,0.)=1)&(jj=0) set(handles.text1,String,9) ;elsetextString =strcat(textString,9);set(handles.text1,String,textString)endjj=0;% - 按键0响应function pushbutton0_Callback(hObject, eventdata, handles)global jjtextString = get(handles.text1,String);if(strcmp(textSt
18、ring,0.)=1)&(jj=0) set(handles.text1,String,0.) ;elsetextString =strcat(textString,0);set(handles.text1,String,textString)endjj=0;% - 按键1响应function pushbutton1_Callback(hObject, eventdata, handles)global jjtextString = get(handles.text1,String);if(strcmp(textString,0.)=1)&(jj=0) set(handles.text1,St
19、ring,1) ;elsetextString =strcat(textString,1);set(handles.text1,String,textString)endjj=0;% - 按键+响应function pushbuttonadd_Callback(hObject, eventdata, handles)textString = get(handles.text1,String);textString =strcat(textString,+);set(handles.text1,String,textString)% - 按键-响应function pushbuttonsub_C
20、allback(hObject, eventdata, handles)textString = get(handles.text1,String);textString =strcat(textString,-);set(handles.text1,String,textString)% - 按*响应function pushbuttonmul_Callback(hObject, eventdata, handles)% hObject handle to pushbuttonmul (see GCBO)% eventdata reserved - to be defined in a fu
21、ture version of MATLAB% handles structure with handles and user data (see GUIDATA)textString = get(handles.text1,String);textString =strcat(textString,*);set(handles.text1,String,textString)% - 按键/响应function pushbuttonchu_Callback(hObject, eventdata, handles)textString = get(handles.text1,String);te
22、xtString =strcat(textString,/);set(handles.text1,String,textString)% - 按键=响应function pushbuttonequal_Callback(hObject, eventdata, handles)textString = get(handles.text1,String);% if(strcmp(textString,0.)=1)% set(handles.text1,String,0.) ;% else% pushbutton20 b c = strread(textString, %f %c %f);% cle
23、ar ans;% % switch b% case *% ans=pushbutton20*c;% case /% if (c=0)% ans=Error Divided by zero;% else% ans=pushbutton20/c;% end% case -% ans=pushbutton20-c;% case +% ans=pushbutton20+c;% % end%这是系统自带的一个回调函数,回调函数是已经编写好的,提供给系统调用的函数关于eval函数的理解就是 EVAL函数相当于将函数中的字符串,放到命令行中执行。写eval的字符串的时候注意单引号的表达就OK了textStr
24、ing = get(handles.text1,String);ans =eval(textString);set(handles.text1,String,ans)% - 按键(响应function pushbutton18_Callback(hObject, eventdata, handles)textString = get(handles.text1,String);if(strcmp(textString,0.)=1) set(handles.text1,String,() ;elsetextString =strcat(textString,();set(handles.text
25、1,String,textString)end% - 按键)响应function pushbutton19_Callback(hObject, eventdata, handles)textString = get(handles.text1,String);if(strcmp(textString,0.)=1) set(handles.text1,String,) ;elsetextString =strcat(textString,);set(handles.text1,String,textString)end% - 按键sin响应function pushbuttonsin_Callb
26、ack(hObject, eventdata, handles)textString = get(handles.text1,String);%strcmp(textString,0.)if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ;elsea = strread(textString, %f);a=sin(a);set(handles.text1,String,a)end% - 按键cos响应function pushbuttoncos_Callback(hObject, eventdata, handles)textStr
27、ing = get(handles.text1,String);if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ;elsea = strread(textString, %f);a=cos(a);set(handles.text1,String,a)end% - Executes on button press in pushbutton22.textString = get(handles.text1,String);%strcmp(textString,0.)if(strcmp(textString,0.)=1) set(h
28、andles.text1,String,0.) ;elsea = strread(textString, %f);a=0-a;set(handles.text1,String,a)end% - 按键clear响应function pushbuttoncl_Callback(hObject, eventdata, handles)set(handles.text1,String,0.) ;% - 按键x2响应function pushbuttonchengfang_Callback(hObject, eventdata, handles)textString = get(handles.text
29、1,String);if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ;elsea = strread(textString, %f);a=a*a;set(handles.text1,String,a)end% - 按键sqrt响应function pushbuttonsqrt_Callback(hObject, eventdata, handles)textString = get(handles.text1,String);if(strcmp(textString,0.)=1) set(handles.text1,String
30、,0.) ;elsea = strread(textString, %f);a=sqrt(a);set(handles.text1,String,a)end% - 按键.响应function pushbuttonnode_Callback(hObject, eventdata, handles)global jj textString = get(handles.text1,String);if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ; jj=1;elsetextString =strcat(textString,.);se
31、t(handles.text1,String,textString)end% 按键backspace响应function back_Callback(hObject, eventdata, handles)global jjtextString = get(handles.text1,String);if(strcmp(textString,0.)=1)&(jj=0) set(handles.text1,String,0.) ;else ss=char(textString); l=length(textString); textString=ss(1:l-1);set(handles.tex
32、t1,String,textString)endjj=0;% - Executes on button press in pushbutton28.function pushbutton28_Callback(hObject, eventdata, handles)textString = get(handles.text1,String);%strcmp(textString,0.)if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ;elsea = strread(textString, %f);a=1/a;set(handle
33、s.text1,String,a)end% - 按键%响应% - Executes on button press in quyu.function quyu_Callback(hObject, eventdata, handles)shift=1;% - 按键log响应function pushbuttonlog_Callback(hObject, eventdata, handles)textString = get(handles.text1,String);if(strcmp(textString,0.)=1) set(handles.text1,String,0.) ;elsea = strread(textString, %f);a=log(a);set(handles.text1,String,a)end附录B计算器的最终实现以及编译环境:编译环境:附录C修改以后的计算器界面: .21
版权声明:以上文章中所选用的图片及文字来源于网络以及用户投稿,由于未联系到知识产权人或未发现有关知识产权的登记,如有知识产权人并不愿意我们使用,如有侵权请立即联系:2622162128@qq.com ,我们立即下架或删除。
Copyright© 2022-2024 www.wodocx.com ,All Rights Reserved |陕ICP备19002583号-1
陕公网安备 61072602000132号 违法和不良信息举报:0916-4228922