⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculator2.m

📁 在matlab中使用m函数实现简单计算器功能
💻 M
字号:
h0=figure('toolbar','none',...
    'position',[103.8 39.231 280 250],...
    'name','用M文件制作计算器');

%生成自己的菜单
set(h0,'Menubar','none');
menuitem_file=uimenu(h0,'Label','&File');
menuitem_exit=uimenu(menuitem_file,'Label','&Exit','callback','close');
menuitem_help=uimenu(h0,'Label','&Help');
menuitem_about=uimenu(menuitem_help,'Label','&About');
 
%数字键的输入
b0=uicontrol('parent',h0,...
    'units','points',...
    'tag','button0',...
    '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']);
    
b1=uicontrol('parent',h0,...
     'units','points',...
     'tag','button1',...
      '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','button2',...
      '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','button3',...
      '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);']);
    
    
  b4=uicontrol('parent',h0,...
     'units','points',...
     'tag','button4',...
      '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','button5',...
      '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','button6',...
      '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);']);
       
  b7=uicontrol('parent',h0,...
     'units','points',...
     'tag','button7',...
      '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','button8',...
      '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','button9',...
      '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);']);
    
%正负号转换的实现
 b10=uicontrol('parent',h0,...
     'units','points',...
     'tag','button_sign',...
      'style','pushbutton',...
    'string','+/-',...
    'fontsize',12,...
     'position',[45 15 35 20],...
      'backgroundcolor',[0.75 0.75 0.75],...
       'callback',[...
]);
    
%小数点的实现
 b11=uicontrol('parent',h0,...
     'units','points',...
     'tag','button_dot',...
      'style','pushbutton',...
    'string','.',...
    'fontsize',12,...
     'position',[85 15 35 20],...
      'backgroundcolor',[0.75 0.75 0.75],...
       'callback',[...
]);
    
 %加号的实现
b12=uicontrol('parent',h0,...
     'units','points',...
     'tag','button_add',...
      'style','pushbutton',...
    'string','+',...
    'fontsize',12,...
     'position',[125 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=''+'';']);
   
   %等号的实现 
b13=uicontrol('parent',h0,...
     'units','points',...
     'tag','button_equal',...
      'style','pushbutton',...
    'string','=',...
    'fontsize',12,...
     'position',[165 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;']);
    
   %减号的实现
  b14=uicontrol('parent',h0,...
     'units','points',...
     'tag','button_minus',...
      '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 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=''-'';']);
    
    %开平方的实现
 b15=uicontrol('parent',h0,...
     'units','points',...
     'tag','button_sqrt',...
      'style','pushbutton',...
    'string','sqrt',...
    'fontsize',12,...
     'position',[165 45 35 20],...
      'backgroundcolor',[0.75 0.75 0.75],...
       'callback',[...
       ]);
        
    %乘法的实现
 b16=uicontrol('parent',h0,...
     'units','points',...
     'tag','button_times',...
      '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=''*'';']);
        
    %求倒数的实现
 b17=uicontrol('parent',h0,...
     'units','points',...
     'tag','button_reciprocal',...
      'style','pushbutton',...
    'string','1/x',...
    'fontsize',12,...
     'position',[165 75 35 20],...
      'backgroundcolor',[0.75 0.75 0.75],...
       'callback',[...
]);
        
    %除法的实现
  b18=uicontrol('parent',h0,...
     'units','points',...
     'tag','button_divide',...
      '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 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=''/'';']);
        
    %清除键的实现
 b19=uicontrol('parent',h0,...
     'units','points',...
     'tag','button_clear',...
      'style','pushbutton',...
    'string','C',...
    'fontsize',12,...
     'position',[165 105 35 20],...
      'backgroundcolor',[0.75 0.75 0.75],...
       'callback',[...
        ]);
    

        
      %编辑框 
     e1=uicontrol('parent',h0,...
    'units','points',...
    'tag','e1',...
    'style','edit',...
    'horizontalalignment','right',...
    'fontsize',12,...
    'string','0',...
    'position',[45 135 155 18],...
    'backgroundcolor',[1 1 1]);

k=get(e1,'string');
i=0;
m=0;
    
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -