基于MATLAB-GUI的簡(jiǎn)單計(jì)算器_第1頁(yè)
基于MATLAB-GUI的簡(jiǎn)單計(jì)算器_第2頁(yè)
基于MATLAB-GUI的簡(jiǎn)單計(jì)算器_第3頁(yè)
基于MATLAB-GUI的簡(jiǎn)單計(jì)算器_第4頁(yè)
基于MATLAB-GUI的簡(jiǎn)單計(jì)算器_第5頁(yè)
已閱讀5頁(yè),還剩19頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、姓名: 曹慈航 班級(jí): 12級(jí)電氣自動(dòng)化2班 學(xué)號(hào):1208441044 基于MATLAB-GUI的簡(jiǎn)單計(jì)算器設(shè)計(jì)題目:計(jì)算器 完成一個(gè)簡(jiǎn)單的計(jì)算器。 要求(但不限于): GUI上具有數(shù)字鍵盤輸入?yún)^(qū)域,能夠進(jìn)行加、減、乘、除、三角函數(shù)等基礎(chǔ)運(yùn)算。 界面簡(jiǎn)潔、美觀 可能需要的控件: Push Button Edit Text Pop-up Menu1 功能介紹本程序是一個(gè)簡(jiǎn)單的計(jì)算器程序,使用MATLAB軟件編寫完成。主要具有加、減、乘、除、三角函數(shù)等基礎(chǔ)運(yùn)算,并通過(guò)GUI進(jìn)行程序使用的交互。程序交互界面如下:圖1 程序的交互界面2 功能實(shí)現(xiàn)首先用MATLAB GUI功能,在繪制一個(gè)動(dòng)

2、態(tài)文本框和一個(gè)文本編輯框,以及25個(gè)命令按鈕,調(diào)整好各控件大小、顏色,整體布局如圖所示:備注:軟件版本:MATLAB R2011b2.1 布局GUI1、打開(kāi)MATLAB,在Guide中新建FIG文件2、然后雙擊“Blank GUI(Default)”或單擊OK鍵出現(xiàn)GUI窗口3、添加按鈕和動(dòng)態(tài)文本框4、根據(jù)按鈕的作用及視覺(jué)效果做一定的修改 對(duì)按鈕的字符串大小、顏色進(jìn)行設(shè)置,對(duì)按鈕的位置進(jìn)行排布,盡量使按鈕集中在動(dòng)態(tài)文本框下面。最終設(shè)置的動(dòng)態(tài)文本框?yàn)榛疑渌粹o均為藍(lán)色。 5、保存、添加功能函數(shù)    將做好的按鈕及動(dòng)態(tài)文本框保存后自

3、動(dòng)彈出Editor的M文本,然后對(duì)相應(yīng)的pushbutton添加功能函數(shù)。以下是相應(yīng)按鈕的功能函數(shù)。  (1)數(shù)字按鍵編寫。 在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指向?qū)ο骵dit1,并以字符串形式來(lái)存儲(chǔ)數(shù)據(jù)文本框edit1的內(nèi)容,并存儲(chǔ)數(shù)個(gè)“0”, 然后由set(handles.edit1,'String',a)在edit1中輸出。 同理,分別在function pushbutton210_Callback(hObject, eventdata, handles)下給19數(shù)字按鍵下編寫此類程序。 (2)符號(hào)鍵:

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的作用是將兩個(gè)字符串連接起來(lái),就是在已輸入的存儲(chǔ)數(shù)據(jù)a后添加“+”進(jìn)行運(yùn)算。 然后執(zhí)行set(handles.edit1,&

6、#39;String',a)。符號(hào)鍵-、*、/、.與+的運(yùn)算函數(shù)類似, “平方運(yùn)算”主要是由“2”功能實(shí)現(xiàn)。 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)運(yùn)算符“=”的編程: a=get(handles.edit1,'string')b=eval(a)set(handles.edit1,'string',num2str(b) “eval”的作用是將符號(hào)表達(dá)式轉(zhuǎn)換成數(shù)值表達(dá)式再由set(handles.edit1,'string',num2str(b)輸出。 (4)按鍵“back”編程:即顯

10、示一個(gè)空字符: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)按鍵“清零”:把動(dòng)態(tài)文本框的字符清空

11、,返回一個(gè)空格。 set(handles.edit1,'String','0')(6)三角函數(shù)的編輯 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是把角度轉(zhuǎn)換為弧度,這樣在編程環(huán)境中才能識(shí)別,sin才能起作用。然后執(zhí)行set函數(shù),把結(jié)果輸出來(lái)。同

12、理在cos,tan,cot的回調(diào)函數(shù)中也輸入相應(yīng)的函數(shù),只需把b=sin(b);中的sin改為cos,tan,cot即可 (7)按鍵“()”:在輸入數(shù)據(jù)時(shí)添加括號(hào),以便數(shù)據(jù)的優(yōu)先計(jì)算。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 計(jì)算器的使用加法運(yùn)算(+):按“=”后顯示:減法(-)、乘法(*)、除法(/)運(yùn)算與加法(+)運(yùn)算類似。點(diǎn)號(hào)(.)、括號(hào)():平方(X2)運(yùn)算:按下(X2)后三角函數(shù)(sin、cos、tan、cot)運(yùn)算:按下(sin、cos、tan、cot)后back、清零功能:3 程序總結(jié)本程序?qū)崿F(xiàn)簡(jiǎn)單的科學(xué)運(yùn)算功能及便捷的圖形化交互界面。具有以下優(yōu)點(diǎn):優(yōu)點(diǎn):1、GUI內(nèi)數(shù)據(jù)傳遞非常簡(jiǎn)便。非常

14、簡(jiǎn)便的實(shí)現(xiàn)了前臺(tái)與后臺(tái)、前臺(tái)與前臺(tái)、后臺(tái)與后臺(tái)之間的參數(shù)傳遞。2、圖形化用戶交互界面簡(jiǎn)潔明了。在制作計(jì)算器界面時(shí)操作簡(jiǎn)單,制作完成后程序的輸入框直接彈出,可以直接寫入,即可運(yùn)行計(jì)算器。簡(jiǎn)單的實(shí)現(xiàn)了設(shè)計(jì)與編程的數(shù)據(jù)傳遞。4 課程總結(jié)1、通過(guò)MATLAB簡(jiǎn)單計(jì)算器的設(shè)計(jì),初步了解了關(guān)于MATLAB圖形用戶界面的部分控件的使用方法。 2、MATLAB的GUI提供的很多實(shí)用的控件,方便用于設(shè)計(jì)屬于自己的圖形界面。 3、Matlab具有強(qiáng)大、豐富的內(nèi)置函數(shù)和工具箱,界面設(shè)計(jì)時(shí)更加簡(jiǎn)潔、快捷與直觀。5 參考文獻(xiàn)1 MATLAB語(yǔ)言及其在電子信息工程中的應(yīng)用 王洪元主編 清華大學(xué)出版社2

15、 MATLAB中GUI的應(yīng)用 王洪元主編 清華大學(xué)出版社 附錄(主要程序)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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論