matlab計(jì)算器_第1頁
matlab計(jì)算器_第2頁
matlab計(jì)算器_第3頁
matlab計(jì)算器_第4頁
matlab計(jì)算器_第5頁
已閱讀5頁,還剩13頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、標(biāo)題:基于GUI的計(jì)算器設(shè)計(jì)姓名:劉鵬飛學(xué)號(hào):20132684班級(jí):電1305-2專業(yè):通信工程成績組成:實(shí)驗(yàn)成績+課程作業(yè)=實(shí)驗(yàn)成績課程作業(yè)期末總成績一、實(shí)驗(yàn)思想實(shí)驗(yàn)?zāi)康? 1、熟悉MATLAB的主要控件使用方法。2、熟悉MATLAB的GUI設(shè)計(jì)流程。實(shí)驗(yàn)環(huán)境: 編程軟件MATLAB7.0 實(shí)驗(yàn)內(nèi)容:使用MATLAB的GUI接口設(shè)計(jì)一個(gè)簡單的計(jì)算器。實(shí)驗(yàn)實(shí)現(xiàn)的功能:利用計(jì)算器實(shí)現(xiàn)數(shù)字的“加”、“減”、“乘”、“除”、“取相反數(shù)”、“開根號(hào)”、“取余”、“清零”、“清除”等運(yùn)算。 效果圖: 二、實(shí)驗(yàn)程序源碼部分函數(shù)及相關(guān)注釋:%09數(shù)字鍵及小數(shù)點(diǎn)按鈕代碼范例%全局變量locaval用于存儲(chǔ)用戶

2、輸入的多位數(shù)值%全局變量gloval2用于存儲(chǔ)待處理的第二位數(shù)值function pushbutton1_Callback(hObject, eventdata, handles)global locaval;a = get(handles.pushbutton1,'String');locaval=strcat(locaval,a);set(handles.text1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);%運(yùn)算符按鈕處理“+、-、*、”范例%全局變量

3、flagnum存儲(chǔ)運(yùn)算符標(biāo)志%全局變量global1用于儲(chǔ)存第一個(gè)待處理數(shù)值function pushbutton10_Callback(hObject, eventdata, handles)a = get(handles.pushbutton10,'String');b = get(handles.text1,'String');set(handles.text1,'String',a);global flagnumglobal gloval1global locavallocaval=' 'flagnum=1;gloval1

4、=b;guidata(hObject, handles);%取相反數(shù)按鈕“+-”代碼%算法實(shí)現(xiàn):用零減去文本框現(xiàn)在的值,再賦值給文本框function pushbutton14_Callback(hObject, eventdata, handles)global locaval;locaval=str2num(locaval);locaval=0-locaval;locaval=num2str(locaval);set(handles.text1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject,

5、handles);% 等號(hào)按鈕運(yùn)算實(shí)現(xiàn)%根據(jù)flagnum運(yùn)算標(biāo)志用switch決策語句實(shí)現(xiàn)相應(yīng)計(jì)算%需注意相應(yīng)數(shù)據(jù)類型的轉(zhuǎn)化function pushbutton17_Callback(hObject, eventdata, handles)global flagnumglobal gloval1global gloval2global locavallocaval=' 'gloval1=str2num(gloval1);gloval2=str2num(gloval2);case1=gloval1/gloval2;case2=gloval1*gloval2;case3=glo

6、val1-gloval2;case4=gloval1+gloval2;case1=num2str(case1);case2=num2str(case2);case3=num2str(case3);case4=num2str(case4);switch flagnum; case 1 set(handles.text1,'String',case1); case 2 set(handles.text1,'String',case2); case 3 set(handles.text1,'String',case3); case 4 set(hand

7、les.text1,'String',case4);endguidata(hObject,handles)% BackSpace按鈕函數(shù)%算法實(shí)現(xiàn):MATLAB是用矩陣存儲(chǔ)數(shù)據(jù)的,相應(yīng)的可以取文本框的前N-1實(shí)現(xiàn)其功能function pushbutton19_Callback(hObject, eventdata, handles)textString = get(handles.text1,'String');if(strcmp(textString,'0.')=1) set(handles.text1,'String',&#

8、39;0.') ;else ss=char(textString); l=length(textString); textString=ss(1:l-1);set(handles.text1,'String',textString)endguidata(hObject,handles)%C清除按鈕函數(shù)%把全局變量locaval清零function pushbutton20_Callback(hObject, eventdata, handles)global locavallocaval=' 'set(handles.text1,'String&

9、#39;,'0.');guidata(hObject,handles)%開平方函數(shù)function pushbutton22_Callback(hObject, eventdata, handles)textString = get(handles.text1,'String');textString=str2num(textString);textString=sqrt(textString);textString=num2str(textString);set(handles.text1,'String',textString);locava

10、l=' 'guidata(hObject,handles)%取1/x函數(shù) function pushbutton24_Callback(hObject, eventdata, handles)global locavallocaval=str2num(locaval);locaval=1/locaval;set(handles.text1,'String',locaval);locaval=' 'guidata(hObject,handles)源碼:function varargout = bt0(varargin)% BT0 M-file fo

11、r bt0.fig% BT0, by itself, creates a new BT0 or raises the existing% singleton*.% H = BT0 returns the handle to a new BT0 or the handle to% the existing singleton*.% BT0('Property','Value',.) creates a new BT0 using the% given property value pairs. Unrecognized properties are passed

12、via% varargin to bt0_OpeningFcn. This calling syntax produces a% warning when there is an existing singleton*.% BT0('CALLBACK') and BT0('CALLBACK',hObject,.) call the% local function named CALLBACK in BT0.M with the given input% arguments.% *See GUI Options on GUIDE's Tools menu.

13、 Choose "GUI allows only one% instance to run (singleton)".% See also: GUIDE, GUIDATA, GUIHANDLES% Edit the above text to modify the response to help bt0% Last Modified by GUIDE v2.5 04-May-2010 14:01:00% Begin initialization code - DO NOT EDITglobal gloval1;global gloval2;global flagnum;g

14、lobal locaval;gui_Singleton = 1;gui_State = struct('gui_Name', mfilename, . 'gui_Singleton', gui_Singleton, . 'gui_OpeningFcn', bt0_OpeningFcn, . 'gui_OutputFcn', bt0_OutputFcn, . 'gui_LayoutFcn', , . 'gui_Callback', );if nargin & isstr(varargin1)

15、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 EDIT% - Executes just before bt0 is made visible.function bt0_OpeningFcn(hObject, eventdata, handles, varargi

16、n)% This function has no output args, see OutputFcn.% 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 unrecognized PropertyName/PropertyValue pairs from the% command line (see VARARGIN)%

17、Choose default command line output for bt0handles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes bt0 wait for user response (see UIRESUME)% uiwait(handles.figure1);% - Outputs from this function are returned to the command line.function varargout = bt0_OutputFcn(

18、hObject, eventdata, handles)% varargout cell array for returning 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 structur

19、evarargout1 = handles.output;% - 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)g

20、lobal locaval;a = get(handles.pushbutton1,'String');locaval=strcat(locaval,a);set(handles.text1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hObject ha

21、ndle 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 locaval;a = get(handles.pushbutton2,'String');locaval=strcat(locaval,a);set(handles.text1,'String',locaval);global gloval

22、2gloval2=locaval;guidata(hObject, handles);% - 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 handles and user data

23、(see GUIDATA)global locavala = get(handles.pushbutton3,'String');locaval=strcat(locaval,a);set(handles.text1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button press in pushbutton4.function pushbutton4_Callback(hObject, eventdata, handles

24、)% 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 locavala = get(handles.pushbutton4,'String');locaval=strcat(locaval,a);set(handles.text1,'String',locaval);g

25、lobal gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button press in pushbutton5.function pushbutton5_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 an

26、d user data (see GUIDATA)global locavala = get(handles.pushbutton5,'String');locaval=strcat(locaval,a);set(handles.text1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button press in pushbutton6.function pushbutton6_Callback(hObject, eventd

27、ata, 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 locavala = get(handles.pushbutton6,'String');locaval=strcat(locaval,a);set(handles.text1,'String'

28、;,locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button press in pushbutton7.function pushbutton7_Callback(hObject, eventdata, handles)% hObject handle to pushbutton7 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure wit

29、h handles and user data (see GUIDATA)global locavala = get(handles.pushbutton7,'String');locaval=strcat(locaval,a)set(handles.text1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button press in pushbutton8.function pushbutton8_Callback(hObj

30、ect, eventdata, handles)% hObject handle to pushbutton8 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global locavala = get(handles.pushbutton8,'String');locaval=strcat(locaval,a);set(handles.text1,'

31、;String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button press in pushbutton9.function pushbutton9_Callback(hObject, eventdata, handles)% hObject handle to pushbutton9 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles st

32、ructure with handles and user data (see GUIDATA)global locavala = get(handles.pushbutton9,'String');locaval=strcat(locaval,a);set(handles.text1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button press in pushbutton10.function pushbutton10

33、_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)a = get(handles.pushbutton10,'String');b = get(handles.text1,'String');set(ha

34、ndles.text1,'String',a);global flagnumglobal gloval1global locavallocaval=' 'flagnum=1;gloval1=b;guidata(hObject, handles);% - Executes on button press in pushbutton11.function pushbutton11_Callback(hObject, eventdata, handles)% hObject handle to pushbutton11 (see GCBO)% eventdata re

35、served - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)a = get(handles.pushbutton11,'String');b = get(handles.text1,'String');set(handles.text1,'String',a);global flagnumglobal gloval1global locavallocaval=' 'fl

36、agnum=2;gloval1=b;guidata(hObject, handles);% - Executes on button press in pushbutton12.function pushbutton12_Callback(hObject, eventdata, handles)% hObject handle to pushbutton12 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user d

37、ata (see GUIDATA)a = get(handles.pushbutton12,'String');b = get(handles.text1,'String');set(handles.text1,'String',a);global flagnumglobal gloval1global locavallocaval=' 'flagnum=3;gloval1=b;guidata(hObject, handles);% - If Enable = 'on', executes on mouse pre

38、ss in 5 pixel border.% - Otherwise, executes on mouse press in 5 pixel border or over pushbutton13.function pushbutton13_ButtonDownFcn(hObject, eventdata, handles)% hObject handle to pushbutton13 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with han

39、dles and user data (see GUIDATA)global locavala = get(handles.pushbutton13,'String');locaval=strcat(locaval,a);set(handles.text1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button press in pushbutton13.function pushbutton13_Callback(hObje

40、ct, 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 locavala = get(handles.pushbutton13,'String');locaval=strcat(locaval,a);set(handles.text1,

41、9;String',locaval);global gloval2gloval2=a;guidata(hObject, handles);% - 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 stru

42、cture with handles and user data (see GUIDATA)global locaval;locaval=str2num(locaval);locaval=0-locaval;locaval=num2str(locaval);set(handles.text1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Executes on button press in pushbutton15.function pushbutton15_Call

43、back(hObject, eventdata, handles)% hObject handle to pushbutton15 (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.pushbutton15,'String');set(handles.text1,'String',a);guidata(hObje

44、ct, handles);% - Executes on button press in pushbutton16.function 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(handle

45、s.pushbutton16,'String');b = get(handles.text1,'String');set(handles.text1,'String',a);global flagnumglobal gloval1global locavallocaval=' 'flagnum=4;gloval1=b;guidata(hObject, handles);% - Executes on button press in pushbutton17.function pushbutton17_Callback(hObjec

46、t, 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)global flagnumglobal gloval1global gloval2global locavallocaval=' 'gloval1=str2num(gloval1);gloval2=s

47、tr2num(gloval2);case1=gloval1/gloval2;case2=gloval1*gloval2;case3=gloval1-gloval2;case4=gloval1+gloval2;case1=num2str(case1);case2=num2str(case2);case3=num2str(case3);case4=num2str(case4);switch flagnum; case 1 set(handles.text1,'String',case1); case 2 set(handles.text1,'String',case

48、2); case 3 set(handles.text1,'String',case3); case 4 set(handles.text1,'String',case4);endguidata(hObject,handles)% - Executes on button press in pushbutton18.function pushbutton18_Callback(hObject, eventdata, handles)% hObject handle to pushbutton18 (see GCBO)% eventdata reserved -

49、to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)global locaval;a = get(handles.pushbutton18,'String');locaval=strcat(locaval,a);set(handles.text1,'String',locaval);global gloval2gloval2=locaval;guidata(hObject, handles);% - Execu

50、tes 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 GUIDATA)textString = get(handles.text1,'

51、String');if(strcmp(textString,'0.')=1) set(handles.text1,'String','0.') ;else ss=char(textString); l=length(textString); textString=ss(1:l-1);set(handles.text1,'String',textString)endguidata(hObject,handles)% - Executes on button press in pushbutton20.function pus

52、hbutton20_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)global locavallocaval=' 'set(handles.text1,'String','0.');guidata(hObject,handles)% - Executes on button press in pushbutton21.fun

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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)論