前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Matlab制作一个计算器

Matlab制作一个计算器

作者头像
用户9925864
发布2022-07-27 09:23:31
1.1K0
发布2022-07-27 09:23:31
举报
文章被收录于专栏:算法工程师的学习日志

参考代码如下:

代码语言:javascript
复制

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;

效果如图

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-10-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 算法工程师的学习日志 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档