版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、標題:基于GUI的計算器設計姓名:劉鵬飛學號:20132684班級:電1305-2專業(yè):通信工程成績組成:實驗成績+課程作業(yè)=實驗成績課程作業(yè)期末總成績一、實驗思想實驗目的: 1、熟悉MATLAB的主要控件使用方法。2、熟悉MATLAB的GUI設計流程。實驗環(huán)境: 編程軟件MATLAB7.0 實驗內容:使用MATLAB的GUI接口設計一個簡單的計算器。實驗實現的功能:利用計算器實現數字的“加”、“減”、“乘”、“除”、“取相反數”、“開根號”、“取余”、“清零”、“清除”等運算。 效果圖: 二、實驗程序源碼部分函數及相關注釋:%09數字鍵及小數點按鈕代碼范例%全局變量locaval用于存儲用戶
2、輸入的多位數值%全局變量gloval2用于存儲待處理的第二位數值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);%運算符按鈕處理“+、-、*、”范例%全局變量
3、flagnum存儲運算符標志%全局變量global1用于儲存第一個待處理數值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);%取相反數按鈕“+-”代碼%算法實現:用零減去文本框現在的值,再賦值給文本框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);% 等號按鈕運算實現%根據flagnum運算標志用switch決策語句實現相應計算%需注意相應數據類型的轉化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按鈕函數%算法實現:MATLAB是用矩陣存儲數據的,相應的可以取文本框的前N-1實現其功能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清除按鈕函數%把全局變量locaval清零function pushbutton20_Callback(hObject, eventdata, handles)global locavallocaval=' 'set(handles.text1,'String&
9、#39;,'0.');guidata(hObject,handles)%開平方函數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函數 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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 沙漠治理財政所施工合同
- 食品廠消防設施安裝協議
- 高層建筑觀光游覽升降機租賃合同
- 校園劇組指導老師聘用合同
- 教育展覽活動包車租賃合同
- 2024礦山經營承包的合同范本
- 2024苗木購銷協議:品質保障與配送版
- 2024年高端裝備制造技術轉讓合同(標的:數控機床)
- 二零二五年度綠色建筑節(jié)能改造工程合同12篇
- 2024科技部技術合同示范文本
- 校服服務方案投標方案
- 養(yǎng)老機構安全管理培訓課件
- (附答案)2024公需課《百縣千鎮(zhèn)萬村高質量發(fā)展工程與城鄉(xiāng)區(qū)域協調發(fā)展》試題廣東公需科
- T-CAME 59-2023 醫(yī)院消毒供應中心建設與運行管理標準
- 4s店財務工作總結
- 2024外研版初中英語單詞表匯總(七-九年級)中考復習必背
- 《海上風電場工程巖土試驗規(guī)程》(NB/T 10107-2018)
- 高中新校區(qū)辦學規(guī)劃方案
- 腎積水護理查房
- 無人機駕駛培訓班合作協議
- 五年級上冊小數乘法豎式計算練習400題及答案
評論
0/150
提交評論