参考代码如下:
h0=figure('toolbar','none',...
'position',[200 60 220 240],...
'name','计算器');
b0=uicontrol('parent',h0,...
'units','points',...
'tag','b0',...
'style','pushbutton',...
'string','0',...
'fontsize',12,...
'position',[5 15 35 20],...
'backgroundcolor',[0.75 0.75 0.75],...
'callback',[...
'if k==''0''&i==0,',...
'errordlg(''数字首位不能为0''),',...
'else,',...
'k=[k,''0''];,',...
'if k==''00'',',...
'k=''0'';,',...
'end,',...
'set(e1,''string'',k);,',...
'end']);
b15=uicontrol('parent',h0,...
'units','points',...
'tag','b15',...
'style','pushbutton',...
'string','=',...
'fontsize',12,...
'position',[45 15 35 20],...
'backgroundcolor',[0.75 0.75 0.75],...
'callback',[...
'k=get(e1,''string'');,',...
'if g==''+'',',...
'm=m+str2num(k);,',...
'end,',...
'if g==''-'',',...
'm=m-str2num(k);,',...
'end,',...
'if g==''*'',',...
'm=m*str2num(k);,',...
'end,',...
'if g==''/'',',...
'if k==''0'',',...
'errordlg(''除数不能为0'');,',...
'end,',...
'm=m/str2num(k);,',...
'end,',...
'set(e1,''string'',num2str(m));,',...
'i=0;']);
b11=uicontrol('parent',h0,...
'units','points',...
'tag','b11',...
'style','pushbutton',...
'string','+',...
'fontsize',12,...
'position',[85 15 35 20],...
'backgroundcolor',[0.75 0.75 0.75],...
'callback',[...
'i=i+1;,',...
'if i==1,',...
'm=str2num(k);,',...
'set(e1,''string'',''0'');,',...
'end,',...
'if i>1,',...
'k=get(e1,''string'');,',...
'if g==''+'',',...
'm=m+str2num(k);,',...
'end,',...
'if g==''-'',',...
'm=m-str2num(k);,',...
'end,',...
'if g==''*'',',...
'm=m*str2num(k);,',...
'end,',...
'if g==''/'',',...
'if k==''0'',',...
'errordlg(''除数不能为0'');,',...
'end,',...
'm=m/str2num(k);,',...
'end,',...
'set(e1,''string'',num2str(m));,',...
'i=1;,',...
'end,',...
'k=''0'';,',...
'g=''+'';']);
b16=uicontrol('parent',h0,...
'units','points',...
'tag','b16',...
'style','pushbutton',...
'string','关闭',...
'fontsize',12,...
'position',[125 15 35 20],...
'backgroundcolor',[0.75 0.75 0.75],...
'callback','close');
b1=uicontrol('parent',h0,...
'units','points',...
'tag','b1',...
'style','pushbutton',...
'string','1',...
'fontsize',12,...
'position',[5 45 35 20],...
'backgroundcolor',[0.75 0.75 0.75],...
'callback',[...
'if k==''0'',',...
'k='''';,',...
'end,',...
'k=[k,''1''];,',...
'set(e1,''string'',k);']);
b2=uicontrol('parent',h0,...
'units','points',...
'tag','b2',...
'style','pushbutton',...
'string','2',...
'fontsize',12,...
'position',[45 45 35 20],...
'backgroundcolor',[0.75 0.75 0.75],...
'callback',[...
'if k==''0'',',...
'k='''';,',...
'end,',...
'k=[k,''2''];,',...
'set(e1,''string'',k);']);
b3=uicontrol('parent',h0,...
'units','points',...
'tag','b3',...
'style','pushbutton',...
'string','3',...
'fontsize',12,...
'position',[85 45 35 20],...
'backgroundcolor',[0.75 0.75 0.75],...
'callback',[...
'if k==''0'',',...
'k='''';,',...
'end,',...
'k=[k,''3''];,',...
'set(e1,''string'',k);']);
b14=uicontrol('parent',h0,...
'units','points',...
'tag','b14',...
'style','pushbutton',...
'string','/',...
'fontsize',12,...
'position',[125 45 35 20],...
'backgroundcolor',[0.75 0.75 0.75],...
'callback',[...
'i=i+1;,',...
'if i==1,',...
'm=str2num(k);,',...
'set(e1,''string'',''0'');,',...
'end,',...
'if i>1,',...
'k=get(e1,''string'');,',...
'if k==''0'',',...
'errordlg(''除数不能为0'');,',...
'end,',...
'if ~(k==''0''),',...
'if g==''+'',',...
'm=m+str2num(k);,',...
'end,',...
'if g==''-'',',...
'm=m-str2num(k);,',...
'end,',...
'if g==''*'',',...
'm=m*str2num(k);,',...
'end,',...
'if g==''/'',',...
'm=m/str2num(k);,',...
'end,',...
'set(e1,''string'',num2str(m));,',...
'end,',...
'i=1;,',...
'end,',...
'k=''0'';,',...
'g=''/'';']);
b4=uicontrol('parent',h0,...
'units','points',...
'tag','b4',...
'style','pushbutton',...
'string','4',...
'fontsize',12,...
'position',[5 75 35 20],...
'backgroundcolor',[0.75 0.75 0.75],...
'callback',[...
'if k==''0'',',...
'k='''';,',...
'end,',...
'k=[k,''4''];,',...
'set(e1,''string'',k);']);
b5=uicontrol('parent',h0,...
'units','points',...
'tag','b5',...
'style','pushbutton',...
'string','5',...
'fontsize',12,...
'position',[45 75 35 20],...
'backgroundcolor',[0.75 0.75 0.75],...
'callback',[...
'if k==''0'',',...
'k='''';,',...
'end,',...
'k=[k,''5''];,',...
'set(e1,''string'',k);']);
b6=uicontrol('parent',h0,...
'units','points',...
'tag','b6',...
'style','pushbutton',...
'string','6',...
'fontsize',12,...
'position',[85 75 35 20],...
'backgroundcolor',[0.75 0.75 0.75],...
'callback',[...
'if k==''0'',',...
'k='''';,',...
'end,',...
'k=[k,''6''];,',...
'set(e1,''string'',k);']);
b13=uicontrol('parent',h0,...
'units','points',...
'tag','b13',...
'style','pushbutton',...
'string','*',...
'fontsize',12,...
'position',[125 75 35 20],...
'backgroundcolor',[0.75 0.75 0.75],...
'callback',[...
'i=i+1;,',...
'if i==1,',...
'm=str2num(k);,',...
'set(e1,''string'',''0'');,',...
'end,',...
'if i>1,',...
'k=get(e1,''string'');,',...
'if g==''+'',',...
'm=m+str2num(k);,',...
'end,',...
'if g==''-'',',...
'm=m-str2num(k);,',...
'end,',...
'if g==''*'',',...
'm=m*str2num(k);,',...
'end,',...
'if g==''/'',',...
'if k==''0'',',...
'errordlg(''除数不能为0'');,',...
'end,',...
'm=m/str2num(k);,',...
'end,',...
'set(e1,''string'',num2str(m));,',...
'i=1;,',...
'end,',...
'k=''0'';,',...
'g=''*'';']);
b7=uicontrol('parent',h0,...
'units','points',...
'tag','b7',...
'style','pushbutton',...
'string','7',...
'fontsize',12,...
'position',[5 105 35 20],...
'backgroundcolor',[0.75 0.75 0.75],...
'callback',[...
'if k==''0'',',...
'k='''';,',...
'end,',...
'k=[k,''7''];,',...
'set(e1,''string'',k);']);
b8=uicontrol('parent',h0,...
'units','points',...
'tag','b8',...
'style','pushbutton',...
'string','8',...
'fontsize',12,...
'position',[45 105 35 20],...
'backgroundcolor',[0.75 0.75 0.75],...
'callback',[...
'if k==''0'',',...
'k='''';,',...
'end,',...
'k=[k,''8''];,',...
'set(e1,''string'',k);']);
b9=uicontrol('parent',h0,...
'units','points',...
'tag','b9',...
'style','pushbutton',...
'string','9',...
'fontsize',12,...
'position',[85 105 35 20],...
'backgroundcolor',[0.75 0.75 0.75],...
'callback',[...
'if k==''0'',',...
'k='''';,',...
'end,',...
'k=[k,''9''];,',...
'set(e1,''string'',k);']);
b12=uicontrol('parent',h0,...
'units','points',...
'tag','b12',...
'style','pushbutton',...
'string','-',...
'fontsize',12,...
'position',[125 105 35 20],...
'backgroundcolor',[0.75 0.75 0.75],...
'callback',[...
'i=i+1;,',...
'if i==1,',...
'm=str2num(k);,',...
'set(e1,''string'',''0'');,',...
'end,',...
'if i>1,',...
'k=get(e1,''string'');,',...
'if g==''+'',',...
'm=m+str2num(k);,',...
'end,',...
'if g==''-'',',...
'm=m-str2num(k);,',...
'end,',...
'if g==''*'',',...
'm=m*str2num(k);,',...
'end,',...
'if g==''/'',',...
'if k==''0'',',...
'errordlg(''除数不能为0'');,',...
'end,',...
'm=m/str2num(k);,',...
'end,',...
'set(e1,''string'',num2str(m));,',...
'i=1;,',...
'end,',...
'k=''0'';,',...
'g=''-'';']);
e1=uicontrol('parent',h0,...
'units','points',...
'tag','e1',...
'style','edit',...
'horizontalalignment','right',...
'fontsize',12,...
'string','0',...
'position',[45 135 115 20],...
'backgroundcolor',[1 1 1]);
k=get(e1,'string');
i=0;
m=0;
效果如图