用matlab编程实现计算器

原创 2006年05月17日 22:32:00

    这学期开了软件工程工具及其应用这么课,任老师给我们教了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
 

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

matlab 计算器的实现

Python Qt GUI快速编程-第四章的计算器python3+pytq5+Qt Designer实现

本代码为本人通过python3+pytq5+Qt desinger实现Python Qt GUI快速编程-第四章的计算器功能,还在原有例子上增加了两个按钮,请大家参考。

C#编程实现计算器

Form1.cs 代码: using System; using System.Collections.Generic; using System.ComponentModel; using ...

【Unix/Linux编程实践】用管道实现双向通信—以bc计算器为例

前言几乎每个版本的Unix都包含bc计算器。事实上,大多数版本的bc只分析输入,然后在内部启动了dc计算器程序,并通过管道与其通信。dc是一个基于栈的计算器,它接收逆波兰表达式,执行运算后将结果送到标...

利用SLR实现可编程计算器(表达式求值)

有些计算器,只能进行单步计算,就是说只能输入数字和加减乘除四则运算符号,而不能有括号的出现。可编程计算器就是指能输入括号、数字和四则运算符号,并对所输入的四则运算表达式进行分析,得最终计算出结果的计算...

编程实现计算器

  • 2015-10-24 21:56
  • 40KB
  • 下载

用Java编程实现计算器

  • 2012-03-04 12:56
  • 15KB
  • 下载

【算法 机器学习】MATLAB、R、python三种编程语言实现简单线性回归算法比较

data:2015-10-04 author:laidefa 今天看到机器学习中的回归,一个简单的线性回归原理想想无非就是利用最小二乘法进行拟合。想着自己去实现下算法,体验以下不同的编程语...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)