关闭

用matlab编程实现计算器

标签: matlab编程stringcallbackfunction语言
10040人阅读 评论(6) 收藏 举报
分类:

    这学期开了软件工程工具及其应用这么课,任老师给我们教了matlab程序设计语言,做了课程实验,自己用matlab实现了计算器,发现MATLAB确实是一门很有用得计算机编程语言,它把数值计算和可视化环境集成到一起,非常直观,而且提供了大量的函数,功能强大,工应用范围广泛。

 

 

附代码,大家交流家流!

calculate.m函数文件,实现基本计算,可以重用
function answer=calculate(Number1,Number2,opvalue)
switch (opvalue)
    case '+'
       answer=Number1+Number2;%加法
    case '-'
        answer=Number1-Number2;%减法
    case '*'
        answer=Number1*Number2;%乘法
    case '/'
       answer=Number1/Number2;%除法
    case '^'
        answer=Number1^Number2;%求幂次
    case 'log'
        answer=log(Number1)/log(Number2); %取对数
    otherwise
end
 
界面主要回调函数
% --- 开启键盘
function btnStart_Callback(hObject, eventdata, handles)
global   Number1;% --- 操作数1
global   Number2;% --- 操作数2
global opvalue;% --- 运算符
global IsStart;% --- 开启键
global IsNew;% --- 操作数新得开始
Number1='';
Number2='';
opvalue = '';
 IsNew='true';
 IsStart='true';
set(handles.editDisplay,'string','0');
 
% --- 清除全部按键响应
function btnClearAll_Callback(hObject, eventdata, handles)
global   Number1;% --- 操作数1
global   Number2;% --- 操作数2
global opvalue;% --- 运算符
global IsNew;% --- 操作数新得开始
global IsStart;
 if strcmp(IsStart,'true')==1
Number1='';
Number2='';
opvalue = '';
IsNew='true';
set(handles.editDisplay,'string','0');
end
 
% --- 按键清屏响应
function btnClear_Callback(hObject, eventdata, handles)
global   Number2;% --- 操作数2
global IsNew;% --- 操作数新得开始
global IsStart;
 if strcmp(IsStart,'true')==1
Number2='';
IsNew='true';
set(handles.editDisplay,'string','0');
end
 
% --- 回退字符响应
function btnBack_Callback(hObject, eventdata, handles)
global IsNew;% --- 操作数新得开始
global IsStart;
 if strcmp(IsStart,'true')==1
temp=get(handles.editDisplay,'String');
 if strcmp(temp,'0')~=1
     if(max(size(temp))==1)
          set(handles.editDisplay,'String',0);
            IsNew='true';
      else
    set(handles.editDisplay,'String',temp(1:end-1));
     end
else
    set(handles.editDisplay,'String',0);
      IsNew='false';
end
end
 
% --- 按键0响应
function btn0_Callback(hObject, eventdata, handles)
global IsNew;
global IsStart;
 if strcmp(IsStart,'true')==1
temp=get(handles.editDisplay,'String');
 if strcmp(temp,'0')~=1     
     if strcmp(IsNew,'true')==1
        set(handles.editDisplay,'String','0');
        IsNew='false';
     else
        num=strcat(get(handles.editDisplay,'String'),'0');
        set(handles.editDisplay,'String',num);
     end  
 end
end
% --- 按键1响应
function btn1_Callback(hObject, eventdata, handles)
global IsNew;
global IsStart;
 if strcmp(IsStart,'true')==1
 if strcmp(IsNew,'true')==1
        set(handles.editDisplay,'String','1');
        IsNew=0;
    else
        num=strcat(get(handles.editDisplay,'String'),'1');
        set(handles.editDisplay,'String',num);
end  
end
 
 
% --- 按键2响应
function btn2_Callback(hObject, eventdata, handles)
global IsNew;
global IsStart;
 if strcmp(IsStart,'true')==1
 if strcmp(IsNew,'true')==1
        set(handles.editDisplay,'String','2');
        IsNew='false',;
    else
        num=strcat(get(handles.editDisplay,'String'),'2');
        set(handles.editDisplay,'String',num);
 end  
end
 
% --- 按键4响应
function btn4_Callback(hObject, eventdata, handles)
global IsNew;
global IsStart;
 if strcmp(IsStart,'true')==1
 if strcmp(IsNew,'true')==1
        set(handles.editDisplay,'String','4');
        IsNew='false',;
    else
        num=strcat(get(handles.editDisplay,'String'),'4');
        set(handles.editDisplay,'String',num);
end  
end
 
% --- 按键5响应
function btn5_Callback(hObject, eventdata, handles)
global IsNew;
global IsStart;
 if strcmp(IsStart,'true')==1
 if strcmp(IsNew,'true')==1
        set(handles.editDisplay,'String','5');
        IsNew='false',;
    else
        num=strcat(get(handles.editDisplay,'String'),'5');
        set(handles.editDisplay,'String',num);
end  
end
 
% --- 按键6响应
function btn6_Callback(hObject, eventdata, handles)
global IsNew;
global IsStart;
 if strcmp(IsStart,'true')==1
 if strcmp(IsNew,'true')==1
        set(handles.editDisplay,'String','6');
        IsNew='false',;
    else
        num=strcat(get(handles.editDisplay,'String'),'6');
        set(handles.editDisplay,'String',num);
end  
end
 
 
% --- 按键3响应
function btn3_Callback(hObject, eventdata, handles)
global IsNew;
global IsStart;
 if strcmp(IsStart,'true')==1
 if strcmp(IsNew,'true')==1
        set(handles.editDisplay,'String','3');
        IsNew='false',;
    else
        num=strcat(get(handles.editDisplay,'String'),'3');
        set(handles.editDisplay,'String',num);
end  
 
end
 
% --- 按键7响应
function btn7_Callback(hObject, eventdata, handles)
global IsNew;
global IsStart;
 if strcmp(IsStart,'true')==1
 if strcmp(IsNew,'true')==1
        set(handles.editDisplay,'String','7');
        IsNew='false',;
    else
        num=strcat(get(handles.editDisplay,'String'),'7');
        set(handles.editDisplay,'String',num);
end  
end  
 
% --- 按键8响应
function btn8_Callback(hObject, eventdata, handles)
global IsNew;
global IsStart;
 if strcmp(IsStart,'true')==1
 if strcmp(IsNew,'true')==1
        set(handles.editDisplay,'String','8');
        IsNew='false',;
    else
        num=strcat(get(handles.editDisplay,'String'),'8');
        set(handles.editDisplay,'String',num);
end  
end
% --- 按键9响应
function btn9_Callback(hObject, eventdata, handles)
global IsNew;
global IsStart;
 if strcmp(IsStart,'true')==1
 if strcmp(IsNew,'true')==1
        set(handles.editDisplay,'String','9');
        IsNew='false',;
    else
        num=strcat(get(handles.editDisplay,'String'),'9');
        set(handles.editDisplay,'String',num);
end  
end
 
% --- 按键+响应
function btnAdd_Callback(hObject, eventdata, handles)
global   Number1;% --- 操作数1
global   Number2;% --- 操作数2
global opvalue;% --- 运算符
global IsNew;% --- 操作数新得开始
global IsStart;
 if strcmp(IsStart,'true')==1
 if strcmp(Number1,'')~=1
      Number2=str2num(get(handles.editDisplay,'String'));
      Number1=calculate(Number1,Number2,opvalue);
      set(handles.editDisplay,'String',num2str(Number1));  
      opvalue='+';
      Number2='';
    else
        opvalue='+';
        Number1=str2num(get(handles.editDisplay,'String')); 
end
     IsNew='true';
 
 end
% --- 按键-响应
function btnMinus_Callback(hObject, eventdata, handles)
global   Number1;% --- 操作数1
global   Number2;% --- 操作数2
global opvalue;% --- 运算符
global IsNew;% --- 操作数新得开始
global IsStart;
 if strcmp(IsStart,'true')==1
 if strcmp(Number1,'')~=1
      Number2=str2num(get(handles.editDisplay,'String'));
      Number1=calculate(Number1,Number2,opvalue);
      set(handles.editDisplay,'String',num2str(Number1));  
      opvalue='-';
      Number2='';
    else
        opvalue='-';
        Number1=str2num(get(handles.editDisplay,'String')); 
end
     IsNew='true';
 end
 
 % --- 按键×响应
function btnMul_Callback(hObject, eventdata, handles)
global   Number1;% --- 操作数1
global   Number2;% --- 操作数2
global opvalue;% --- 运算符
global IsNew;% --- 操作数新得开始
global IsStart;
 if strcmp(IsStart,'true')==1
 if strcmp(Number1,'')~=1
      Number2=str2num(get(handles.editDisplay,'String'));
      Number1=calculate(Number1,Number2,opvalue);
      set(handles.editDisplay,'String',num2str(Number1));  
      opvalue='*';
      Number2='';
    else
        opvalue='*';
        Number1=str2num(get(handles.editDisplay,'String')); 
end
     IsNew='true';
 end
 
% --- 按键/响应
function btnDiv_Callback(hObject, eventdata, handles)
global   Number1;% --- 操作数1
global   Number2;% --- 操作数2
global opvalue;% --- 运算符
global IsNew;% --- 操作数新得开始
global IsStart;
 if strcmp(IsStart,'true')==1
 if strcmp(Number1,'')~=1
      Number2=str2num(get(handles.editDisplay,'String'));
      Number1=calculate(Number1,Number2,opvalue);
      set(handles.editDisplay,'String',num2str(Number1));  
      opvalue='/';
      Number2='';
    else
        opvalue='/';
        Number1=str2num(get(handles.editDisplay,'String')); 
end
     IsNew='true';
 end
 
% --- 按键开方响应
function btnSqrt_Callback(hObject, eventdata, handles)
global   Number1;% --- 操作数1
global   Number2;% --- 操作数2
global opvalue;% --- 运算符
global IsNew;% --- 操作数新得开始
global IsStart;
 if strcmp(IsStart,'true')==1
    temp=str2num(get(handles.editDisplay,'String'));
   if temp~=0
        sqrt(temp)
       set(handles.editDisplay,'String',num2str( sqrt(temp)));
        Number1='';
        Number2='';
   end
       IsNew='true';
 end 
 
 
% --- 按键求幂响应
function btnPow_Callback(hObject, eventdata, handles)
global   Number1;% --- 操作数1
global   Number2;% --- 操作数2
global opvalue;% --- 运算符
global IsNew;% --- 操作数新得开始
global IsStart;
 if strcmp(IsStart,'true')==1
 if strcmp(Number1,'')~=1
      Number2=str2num(get(handles.editDisplay,'String'));
      Number1=calculate(Number1,Number2,opvalue);
      set(handles.editDisplay,'String',num2str(Number1));  
      opvalue='^';
      Number2='';
    else
        opvalue='^';
        Number1=str2num(get(handles.editDisplay,'String')); 
end
     IsNew='true';
 end
 
% --- 按键Log响应,取对数
function btnLog_Callback(hObject, eventdata, handles)
global   Number1;% --- 操作数1
global   Number2;% --- 操作数2
global opvalue;% --- 运算符
global IsNew;% --- 操作数新得开始
global IsStart;
 if strcmp(IsStart,'true')==1
 if strcmp(Number1,'')~=1
      Number2=str2num(get(handles.editDisplay,'String'));
      Number1=calculate(Number1,Number2,opvalue);
      set(handles.editDisplay,'String',num2str(Number1));  
      opvalue='log';
      Number2='';
    else
        opvalue='log';
        Number1=str2num(get(handles.editDisplay,'String')); 
end
     IsNew='true';
 
 end
 
% --- 按键=响应
function btnEqual_Callback(hObject, eventdata, handles)
global   Number1;
global   Number2;
global opvalue;
global IsStart;
 if strcmp(IsStart,'true')==1
 if strcmp(Number1,'')~=1
      Number2=str2num(get(handles.editDisplay,'String'));
      Number1=calculate(Number1,Number2,opvalue);
      set(handles.editDisplay,'String',num2str(Number1));  
      Number1='';
      Number2='';
     IsNew='true';
end
end
% --- 按键 +/-(数字) 响应
function btnSign_Callback(hObject, eventdata, handles)
global IsStart;
 if strcmp(IsStart,'true')==1
temp=get(handles.editDisplay,'String');
if temp(1:1)=='-'
    temp=temp(2:end)
    set(handles.editDisplay,'String',temp);
else
    temp=0-str2num(temp);
    set(handles.editDisplay,'String',temp);
end
 
end
 
% --- 按键 .(小数点) 响应
function btnDot_Callback(hObject, eventdata, handles)
global IsStart;
 if strcmp(IsStart,'true')==1
temp=get(handles.editDisplay,'String');
if find(temp=='.')>0
    ;
else
    temp=strcat(temp,'.');
    set(handles.editDisplay,'String',temp);
end
end
 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:47028次
    • 积分:608
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:2篇
    • 译文:0篇
    • 评论:45条
    最新评论