《简单计算器的GUI设计.doc》由会员分享,可在线阅读,更多相关《简单计算器的GUI设计.doc(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精品文档,仅供学习与交流,如有侵权请联系网站删除 简单计算器的GUI设计运行结果:源代码:function varargout = jisuanji(varargin)gui_Singleton = 1;gui_State = struct(gui_Name, mfilename, . gui_Singleton, gui_Singleton, . gui_OpeningFcn, jisuanji_OpeningFcn, . gui_OutputFcn, jisuanji_OutputFcn, . gui_LayoutFcn, , . gui_Callback, );if nargin & i
2、schar(varargin1) gui_State.gui_Callback = str2func(varargin1);endif nargout varargout1:nargout = gui_mainfcn(gui_State, varargin:);else gui_mainfcn(gui_State, varargin:);endfunction jisuanji_OpeningFcn(hObject, eventdata, handles, varargin)handles.output = hObject;guidata(hObject, handles);function
3、varargout = jisuanji_OutputFcn(hObject, eventdata, handles) varargout1 = handles.output;% - Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)%乘号“”键的回调函数;signs=get(handles.edit1,string);set(handles.edit1,string,strcat(signs,*);% - Executes on button pr
4、ess in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)%除号“”键的回调函数;signs=get(handles.edit1,string);set(handles.edit1,string,strcat(signs,/);% - Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)% - Executes on button press in push
5、button4.function pushbutton4_Callback(hObject, eventdata, handles)%加号“+”键的回调函数;signs=get(handles.edit1,string);set(handles.edit1,string,strcat(signs,+);% - Executes on button press in pushbutton6.function pushbutton6_Callback(hObject, eventdata, handles)%减号“”键的回调函数;signs=get(handles.edit1,string);se
6、t(handles.edit1,string,strcat(signs,-);% - Executes on button press in pushbutton7.function pushbutton7_Callback(hObject, eventdata, handles)%数字键7的回调函数:numbers=get(handles.edit1,string);set(handles.edit1,string,strcat(numbers,7);% - Executes on button press in pushbutton8.function pushbutton8_Callba
7、ck(hObject, eventdata, handles)%数字键4的回调函数:numbers=get(handles.edit1,string);set(handles.edit1,string,strcat(numbers,4);% - Executes on button press in pushbutton9.function pushbutton9_Callback(hObject, eventdata, handles)%数字键1的回调函数:numbers=get(handles.edit1,string);set(handles.edit1,string,strcat(nu
8、mbers,1);% - Executes on button press in pushbutton10.function pushbutton10_Callback(hObject, eventdata, handles)%数字键0的回调函数: %get函数获取输入文本框edit1的string属性,赋值给numbers,数值类型为char;numbers=get(handles.edit1,string); %字符串合并函数strcat()将按键0与numbers的值合并,将合并后的字符串返回到输入文本框edit1的string属性;set(handles.edit1,string,st
9、rcat(numbers,0);%同理编写其余数字键、小数点键、小括号键、运算符号键;% - Executes on button press in pushbutton11.function pushbutton11_Callback(hObject, eventdata, handles)%数字键8的回调函数:numbers=get(handles.edit1,string);set(handles.edit1,string,strcat(numbers,8);% - Executes on button press in pushbutton12.function pushbutton1
10、2_Callback(hObject, eventdata, handles)%数字键5的回调函数:numbers=get(handles.edit1,string);set(handles.edit1,string,strcat(numbers,5);% - Executes on button press in pushbutton13.function pushbutton13_Callback(hObject, eventdata, handles)%数字键2的回调函数:numbers=get(handles.edit1,string);set(handles.edit1,string
11、,strcat(numbers,2);% - Executes on button press in pushbutton14.function pushbutton14_Callback(hObject, eventdata, handles)%左括号“(”键的回调函数;signs=get(handles.edit1,string);set(handles.edit1,string,strcat(signs,();% - Executes on button press in pushbutton15.function pushbutton15_Callback(hObject, event
12、data, handles)%右括号“)”键的回调函数;signs=get(handles.edit1,string);set(handles.edit1,string,strcat(signs,);% - Executes on button press in pushbutton16.function pushbutton16_Callback(hObject, eventdata, handles)%数字键9的回调函数:numbers=get(handles.edit1,string);set(handles.edit1,string,strcat(numbers,9);% - Exec
13、utes on button press in pushbutton17.function pushbutton17_Callback(hObject, eventdata, handles)%数字键6的回调函数:numbers=get(handles.edit1,string);set(handles.edit1,string,strcat(numbers,6);% - Executes on button press in pushbutton18.function pushbutton18_Callback(hObject, eventdata, handles)%数字键3的回调函数:n
14、umbers=get(handles.edit1,string);set(handles.edit1,string,strcat(numbers,3);% - Executes on button press in pushbutton19.function pushbutton19_Callback(hObject, eventdata, handles)%小数点的回调函数;point=get(handles.edit1,string);set(handles.edit1,string,strcat(point,.);% - Executes on button press in pushb
15、utton20.function pushbutton20_Callback(hObject, eventdata, handles)%等号“=”号键的回调函数;%get函数获取输入文本框edit1的string属性,并赋值与x;x=get(handles.edit1,string);%利用执行字符串命令eval()计算用户输入的表达式,得到的结果为char型;y1=eval(x);%利用字符串输出格式控制函数sprintf()将字符型常量精确到小数点后十位;y=sprintf(%.10f,y1);%利用属性设置函数set()将字符型结果返回到输出文本框edit2的string属性;set(h
16、andles.edit2,string,y);% - Executes on button press in pushbutton21.function pushbutton21_Callback(hObject, eventdata, handles)%Delete键的回调函数;%get()函数获取当前输入文本框的string属性,并赋值与s1;s1=get(handles.edit1,string);%length(s1)函数获取s1字符串的长度,并利用s1(1:length(s1)-1)获取s1的第一个到第(length(s1)-1)个字符串,已达到删除前一个字符的目的;s2=s1(1:
17、(length(s1)-1);%利用属性设置函数set()将字符型结果返回到输入文本框edit1的string属性;set(handles.edit1,string,s2);% - Executes on button press in pushbutton22.function pushbutton22_Callback(hObject, eventdata, handles)%CLC键的回调函数;%清空输入文本框edit1的string属性;set(handles.edit1,String,);%清空输出文本框edit2的string属性;set(handles.edit2,String,
18、0);% - Executes on button press in pushbutton23.function pushbutton23_Callback(hObject, eventdata, handles)%Exit键的回调函数;close;function edit1_Callback(hObject, eventdata, handles)% - Executes during object creation, after setting all properties.function edit1_CreateFcn(hObject, eventdata, handles)if i
19、spc & isequal(get(hObject,BackgroundColor), get(0,defaultUicontrolBackgroundColor) set(hObject,BackgroundColor,white);endfunction edit2_Callback(hObject, eventdata, handles)% - Executes during object creation, after setting all properties.function edit2_CreateFcn(hObject, eventdata, handles)if ispc & isequal(get(hObject,BackgroundColor), get(0,defaultUicontrolBackgroundColor) set(hObject,BackgroundColor,white);end【精品文档】第 4 页