用Matlab App Builder写一个简单的计算器

用Matlab App Builder写一个简单的计算器因为疫情,在家(无聊)想着看一下MatlabAppBuilder,花了一点时间,做了个简单的计算器。只做了基本的计算功能,算是对MatlabAppBuilder的一个基本的学习。MatlabAppBuilder比起成熟的一些制作app的语言来说,功能非常简陋,不过也非常好上手,如果对Matlab基本语言很熟悉的话,可以说,上手基本没有什么障碍。因为MatlabAp…

    因为疫情,在家(无聊)想着看一下Matlab App Builder,花了一点时间,做了个简单的计算器。只做了基本的计算功能,算是对Matlab App Builder的一个基本的学习。

    Matlab App Builder比起成熟的一些制作app的语言来说,功能非常简陋,不过也非常好上手,如果对Matlab基本语言很熟悉的话,可以说,上手基本没有什么障碍。因为Matlab App Builder的程序可以直接打包成exe,所以如果不想去搞window程序开发,而又想制作一些简单的程序的话,这个Matlab App Builder看起来是一个比较不错的工具。(虽然打包成exe之后效率很低,嘿嘿)

    制作这个简单的(弱智的)计算器,最大的工作量就是在判断输入逻辑上,比如说:

  1. 连续输入两个’.’,那么后面一个’.’要忽略掉;连续输入两个‘+’,后面一个‘+’要忽略掉;
  2. 按下‘+/-‘键取反的时候,需要先判断当前输入的数据,如果不是数据,则忽略,是数据的话,取反并显示;
  3. 按下’sqrt’时,后面的输入需要写进括号里面;删除的时候也要考虑这一点。

    其实大量的实践都花在这样一些输入逻辑上了。最后做成的样子是这样:

用Matlab App Builder写一个简单的计算器

    基本的功能实现了,但是也有很多的bug。

    代码如下:

classdef app2 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure          matlab.ui.Figure
        TextResult        matlab.ui.control.TextArea
        Button_0          matlab.ui.control.Button
        Button_2          matlab.ui.control.Button
        Button_3          matlab.ui.control.Button
        Button_4          matlab.ui.control.Button
        Button_5          matlab.ui.control.Button
        Button_6          matlab.ui.control.Button
        Button_7          matlab.ui.control.Button
        Button_addsub     matlab.ui.control.Button
        Button_dot        matlab.ui.control.Button
        DELButton         matlab.ui.control.Button
        Button_mult       matlab.ui.control.Button
        Button_add        matlab.ui.control.Button
        xyButton          matlab.ui.control.Button
        ACButton          matlab.ui.control.Button
        Button_div        matlab.ui.control.Button
        Button_sub        matlab.ui.control.Button
        Button_equal      matlab.ui.control.Button
        Button_9          matlab.ui.control.Button
        Button_1          matlab.ui.control.Button
        Button_left       matlab.ui.control.Button
        Button_right      matlab.ui.control.Button
        Button_log        matlab.ui.control.Button
        Button_sqrt       matlab.ui.control.Button
        Button_factorial  matlab.ui.control.Button
        Button_8          matlab.ui.control.Button
    end

    
    properties (Access = private)
        %按键index
        keyIndex = 99;
        %calState,0:初始状态;1:数据输入;2:运算符号输入;3:
        calState = 0;
        %当前显示的字符
        dispString = '';
        %
        calString = '';
        %最后一个运算符之后输入的数据
        nowInputData = 0;
        %当前按下的按键值
        nowInputButton = '';
        %'='是否刚刚按下
        equalPushed = 0;
        %sqrt是否按下
        sqrtPushed = 0;
        %'.'的输入标志
        dotFlag = 0;
        %'+/-'的输入标志
        addsubFlag = 0;
        %运算符输入标志
        calFlag = 0;
        %最后一个运算符号在输入string当中的位置
        calIndex = 0;
        %按下'='之后的计算结果
        resultData = 0;
    end
    
    
    
    methods (Access = private)
        function y = calPosition(app)
            addPosition = max(strfind(app.dispString,'+'));
            if (isempty(addPosition))
                addPosition = 0;
            end
            
            subPosition = max(strfind(app.dispString,'-'));
            if (isempty(subPosition)|subPosition==1)
                subPosition = 0;
            end
            
            multPosition = max(strfind(app.dispString,'×'));
            if (isempty(multPosition))
                multPosition = 0;
            end
            
            divPosition = max(strfind(app.dispString,'÷'));
            if (isempty(divPosition))
                divPosition = 0;
            end
            
            powerPosition = max(strfind(app.dispString,'^'));
            if (isempty(powerPosition))
                powerPosition = 0;
            end
            
            leftPosition = max(strfind(app.dispString,'('));
            if (isempty(leftPosition))
                leftPosition = 0;
            end
            
            y = max([addPosition,subPosition,multPosition,divPosition,powerPosition,leftPosition]);
        end
    end
    

    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            app.keyIndex = 99;
            app.calState = 0;
            app.equalPushed = 0;
            app.dispString = '0';
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_0
        function Button_0Pushed(app, event)
            app.keyIndex = 0;
            %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = '0';
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '0';
                %如果上一个字符是运算符号连着0
            elseif app.dispString(end)=='0'
                if (app.dispString(end-1)=='(' || app.dispString(end-1)==')' || app.dispString(end-1)=='^' || app.dispString(end-1)=='+' || app.dispString(end-1)=='-' || app.dispString(end-1)=='×' || app.dispString(end-1)=='÷')
                    app.dispString = app.dispString;
                else
                    app.dispString = [app.dispString,'0'];
                end
            else
                app.dispString = [app.dispString,'0'];
            end
            app.equalPushed = 0;
            app.calFlag = 0;
            if app.sqrtPushed == 1
                app.dispString = [app.dispString(1:end-2),app.dispString(end),app.dispString(end-1)];
            end
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_1
        function Button_1Pushed(app, event)
            %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = '1';
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '1';
            elseif app.dispString(end)=='0'
                %如果上一个字符是运算符号连着0
                if (app.dispString(end-1)=='('|| app.dispString(end-1)==')' || app.dispString(end-1)=='^' || app.dispString(end-1)=='+' || app.dispString(end-1)=='-' || app.dispString(end-1)=='×' || app.dispString(end-1)=='÷')
                    app.dispString(end) = '1';
                else
                    app.dispString = [app.dispString,'1'];
                end
            else
                app.dispString = [app.dispString,'1'];
            end
            app.equalPushed=0;
            app.calFlag = 0;
            if app.sqrtPushed == 1
                app.dispString = [app.dispString(1:end-2),app.dispString(end),app.dispString(end-1)];
            end
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_dot
        function Button_dotPushed(app, event)
            %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = '0.';
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '0.';
                %如果刚才输入了'.'
            elseif app.dotFlag == 0
                %如果前面是运算符号或者括号
                if app.dispString(end)=='('|| app.dispString(end)==')' || app.dispString(end)=='^' || app.dispString(end)=='+' || app.dispString(end)=='-' || app.dispString(end)=='×' || app.dispString(end)=='÷'
                    app.dispString = [app.dispString,'0.'];
                else
                    app.dispString = [app.dispString,'.'];
                end
            else
                app.dispString = [app.dispString,'.'];
            end
            app.dotFlag = 1;
            app.calFlag = 0;
            app.equalPushed=0;
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: ACButton
        function ACButtonPushed(app, event)
            app.equalPushed=0;
            app.dispString = '0';
            app.TextResult.Value = app.dispString;
            
        end

        % Button pushed function: Button_add
        function Button_addPushed(app, event)
            %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = [app.dispString,'+'];
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '0+';
                %如果前面是运算符号或者括号
            elseif app.calFlag == 0
                if app.dispString(end)=='(' || app.dispString(end)=='^' || app.dispString(end)=='+' || app.dispString(end)=='-' || app.dispString(end)=='×' || app.dispString(end)=='÷'
                    app.dispString = [app.dispString,'0+'];
                else
                    app.dispString = [app.dispString,'+'];
                end
            else
                app.dispString(end) = '+';
            end
            app.dotFlag = 0;
            app.equalPushed=0;
            app.calFlag = 1;
            app.sqrtPushed =0;  
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_addsub
        function Button_addsubPushed(app, event)
            app.calIndex = app.calPosition();
            %刚才显示的是上一次的计算结果,如果现在输入取反,那么将结果取反使用
            if (app.equalPushed==1)
                app.dispString = num2str(str2num(app.dispString)*(-1));
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '0';
                %如果前面是运算符号或者括号
            elseif app.dispString(end)=='('|| app.dispString(end)=='^' || app.dispString(end)=='+' || app.dispString(end)=='-' || app.dispString(end)=='×' || app.dispString(end)=='÷'
                app.dispString = app.dispString;
            elseif app.dispString(end)==')'
                if app.sqrtPushed == 1
                    app.dispString = app.dispString(1:end-1);
                    if app.addsubFlag == 1
                        app.nowInputData = app.dispString(app.calIndex+1:end);
                        app.dispString = [app.dispString(1:app.calIndex-1),app.nowInputData];
                        app.addsubFlag = 0;
                    else
                        app.nowInputData = str2num(app.dispString(app.calIndex+1:end));
                        app.nowInputData = -app.nowInputData;
                        app.dispString = [app.dispString(1:app.calIndex),num2str(app.nowInputData)];
                        app.addsubFlag = 1;
                    end
                    app.dispString = [app.dispString,')'];
                else
                    app.dispString = app.dispString;
                end
            elseif app.calIndex ~= 0
                if app.addsubFlag == 1
                    app.nowInputData = app.dispString(app.calIndex+1:end);
                    app.dispString = [app.dispString(1:app.calIndex-1),app.nowInputData];
                    app.addsubFlag = 0;
                else
                    app.nowInputData = str2num(app.dispString(app.calIndex+1:end));
                    app.nowInputData = -app.nowInputData;
                    app.dispString = [app.dispString(1:app.calIndex),num2str(app.nowInputData)];
                    app.addsubFlag = 1;
                end
            else
                app.dispString = num2str(-str2num(app.dispString));
            end
            app.dotFlag = 0;
            app.equalPushed=0;
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_equal
        function Button_equalPushed(app, event)
            app.equalPushed = 1;
            %计算结果
            app.calString = app.dispString;
            app.calString = strrep(app.calString,'×','*');
            app.calString = strrep(app.calString,'÷','/');
            app.resultData = str2num(app.calString);
            %显示
            if (isempty(app.resultData))
                app.TextResult.Value=[app.dispString,10,'输入似乎有错误'];
                app.dispString = '0';
            else
                app.TextResult.Value = [app.dispString,10,num2str(app.resultData)];
                app.dispString = num2str(app.resultData);
            end
            app.dotFlag = 0;
            app.calFlag = 0;
            app.sqrtPushed =0;
        end

        % Button pushed function: Button_sub
        function Button_subPushed(app, event)
            %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = [app.dispString,'-'];
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '0-';
                %如果前面是运算符号或者括号
            elseif app.calFlag == 0
                if app.dispString(end)=='(' || app.dispString(end)=='^' || app.dispString(end)=='+' || app.dispString(end)=='-' || app.dispString(end)=='×' || app.dispString(end)=='÷'
                    app.dispString = [app.dispString,'0-'];
                else
                    app.dispString = [app.dispString,'-'];
                end
            else
                app.dispString(end) = '-';
            end
            app.dotFlag = 0;
            app.equalPushed=0;
            app.calFlag = 1;
            app.sqrtPushed =0;
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_2
        function Button_2Pushed(app, event)
            %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = '2';
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '2';
            elseif app.dispString(end)=='0'
                %如果上一个字符是运算符号连着0
                if (app.dispString(end-1)=='('|| app.dispString(end-1)==')' || app.dispString(end-1)=='^' || app.dispString(end-1)=='+' || app.dispString(end-1)=='-' || app.dispString(end-1)=='×' || app.dispString(end-1)=='÷')
                    app.dispString(end) = '2';
                else
                    app.dispString = [app.dispString,'2'];
                end
            else
                app.dispString = [app.dispString,'2'];
            end
            app.equalPushed=0;
            app.calFlag = 0;
            if app.sqrtPushed == 1
                app.dispString = [app.dispString(1:end-2),app.dispString(end),app.dispString(end-1)];
            end
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_left
        function Button_leftPushed(app, event)
            %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = '(';
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '(';
            elseif app.dispString(end)=='0'
                %如果上一个字符是运算符号连着0
                if (app.dispString(end-1)=='('|| app.dispString(end-1)==')' || app.dispString(end-1)=='^' || app.dispString(end-1)=='+' || app.dispString(end-1)=='-' || app.dispString(end-1)=='×' || app.dispString(end-1)=='÷')
                    app.dispString(end) = '(';
                else
                    app.dispString = [app.dispString,'('];
                end
            else
                app.dispString = [app.dispString,'('];
            end
            app.equalPushed=0;
            app.calFlag = 0;
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_right
        function Button_rightPushed(app, event)
            %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = '0';
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '0';
            else
                app.dispString = [app.dispString,')'];
            end
            app.equalPushed=0;
            app.calFlag = 0;
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_mult
        function Button_multPushed(app, event)
            %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = [app.dispString,'×'];
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '0×';
                %如果前面是运算符号或者括号
            elseif app.calFlag == 0
                if app.dispString(end)=='(' || app.dispString(end)=='^' || app.dispString(end)=='+' || app.dispString(end)=='-' || app.dispString(end)=='×' || app.dispString(end)=='÷'
                    app.dispString = [app.dispString,'0×'];
                else
                    app.dispString = [app.dispString,'×'];
                end
            else
                app.dispString(end) = '×';
            end
            app.dotFlag = 0;
            app.equalPushed=0;
            app.calFlag = 1;
            app.sqrtPushed =0;
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_div
        function Button_divPushed(app, event)
            %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = [app.dispString,'÷'];
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '0÷';
                %如果前面是运算符号或者括号
            elseif app.calFlag == 0
                if app.dispString(end)=='(' || app.dispString(end)=='^' || app.dispString(end)=='+' || app.dispString(end)=='-' || app.dispString(end)=='×' || app.dispString(end)=='÷'
                    app.dispString = [app.dispString,'0÷'];
                else
                    app.dispString = [app.dispString,'÷'];
                end
            else
                app.dispString(end) = '÷';
            end
            app.dotFlag = 0;
            app.equalPushed=0;
            app.calFlag = 1;
            app.sqrtPushed =0;
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: xyButton
        function xyButtonPushed(app, event)
            %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = [app.dispString,'^'];
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '0^';
                %如果前面是运算符号或者括号
            elseif app.calFlag == 0
                if app.dispString(end)=='(' || app.dispString(end)=='^' || app.dispString(end)=='+' || app.dispString(end)=='-' || app.dispString(end)=='×' || app.dispString(end)=='÷'
                    app.dispString = [app.dispString,'0^'];
                else
                    app.dispString = [app.dispString,'^'];
                end
            else
                app.dispString(end) = '^';
            end
            app.dotFlag = 0;
            app.equalPushed=0;
            app.calFlag = 1;
            app.sqrtPushed =0;
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: DELButton
        function DELButtonPushed(app, event)
            %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = '0';
                %如果目前的显示为0
            elseif length(app.dispString) == 1
                app.dispString = '0';
                %如果前面是运算符号或者括号
            else
                if app.sqrtPushed == 1
                    app.dispString = [app.dispString(1:end-2),')'];
                else
                    app.dispString = app.dispString(1:end-1);
                end
            end
            app.dotFlag = 0;
            app.equalPushed=0;
            app.calFlag = 0;
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_sqrt
        function Button_sqrtPushed(app, event)
            %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = ['sqrt(',app.dispString,')'];
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = 'sqrt(0)';
            elseif app.dispString(end)==')'
                app.dispString = app.dispString;
            else
                app.dispString = [app.dispString,'sqrt()'];
                app.sqrtPushed = 1;
            end
            app.equalPushed=0;
            app.calFlag = 0;
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_3
        function Button_3Pushed(app, event)
             %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = '3';
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '3';
            elseif app.dispString(end)=='0'
                %如果上一个字符是运算符号连着0
                if (app.dispString(end-1)=='('|| app.dispString(end-1)==')' || app.dispString(end-1)=='^' || app.dispString(end-1)=='+' || app.dispString(end-1)=='-' || app.dispString(end-1)=='×' || app.dispString(end-1)=='÷')
                    app.dispString(end) = '3';
                else
                    app.dispString = [app.dispString,'3'];
                end
            else
                app.dispString = [app.dispString,'3'];
            end
            app.equalPushed=0;
            app.calFlag = 0;
            if app.sqrtPushed == 1
                app.dispString = [app.dispString(1:end-2),app.dispString(end),app.dispString(end-1)];
            end
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_4
        function Button_4Pushed(app, event)
             %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = '4';
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '4';
            elseif app.dispString(end)=='0'
                %如果上一个字符是运算符号连着0
                if (app.dispString(end-1)=='('|| app.dispString(end-1)==')' || app.dispString(end-1)=='^' || app.dispString(end-1)=='+' || app.dispString(end-1)=='-' || app.dispString(end-1)=='×' || app.dispString(end-1)=='÷')
                    app.dispString(end) = '4';
                else
                    app.dispString = [app.dispString,'4'];
                end
            else
                app.dispString = [app.dispString,'4'];
            end
            app.equalPushed=0;
            app.calFlag = 0;
            if app.sqrtPushed == 1
                app.dispString = [app.dispString(1:end-2),app.dispString(end),app.dispString(end-1)];
            end
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_5
        function Button_5Pushed(app, event)
             %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = '5';
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '5';
            elseif app.dispString(end)=='0'
                %如果上一个字符是运算符号连着0
                if (app.dispString(end-1)=='('|| app.dispString(end-1)==')' || app.dispString(end-1)=='^' || app.dispString(end-1)=='+' || app.dispString(end-1)=='-' || app.dispString(end-1)=='×' || app.dispString(end-1)=='÷')
                    app.dispString(end) = '5';
                else
                    app.dispString = [app.dispString,'5'];
                end
            else
                app.dispString = [app.dispString,'5'];
            end
            app.equalPushed=0;
            app.calFlag = 0;
            if app.sqrtPushed == 1
                app.dispString = [app.dispString(1:end-2),app.dispString(end),app.dispString(end-1)];
            end
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_6
        function Button_6Pushed(app, event)
             %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = '6';
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '6';
            elseif app.dispString(end)=='0'
                %如果上一个字符是运算符号连着0
                if (app.dispString(end-1)=='('|| app.dispString(end-1)==')' || app.dispString(end-1)=='^' || app.dispString(end-1)=='+' || app.dispString(end-1)=='-' || app.dispString(end-1)=='×' || app.dispString(end-1)=='÷')
                    app.dispString(end) = '6';
                else
                    app.dispString = [app.dispString,'6'];
                end
            else
                app.dispString = [app.dispString,'6'];
            end
            app.equalPushed=0;
            app.calFlag = 0;
            if app.sqrtPushed == 1
                app.dispString = [app.dispString(1:end-2),app.dispString(end),app.dispString(end-1)];
            end
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_7
        function Button_7Pushed(app, event)
             %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = '7';
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '7';
            elseif app.dispString(end)=='0'
                %如果上一个字符是运算符号连着0
                if (app.dispString(end-1)=='('|| app.dispString(end-1)==')' || app.dispString(end-1)=='^' || app.dispString(end-1)=='+' || app.dispString(end-1)=='-' || app.dispString(end-1)=='×' || app.dispString(end-1)=='÷')
                    app.dispString(end) = '7';
                else
                    app.dispString = [app.dispString,'7'];
                end
            else
                app.dispString = [app.dispString,'7'];
            end
            app.equalPushed=0;
            app.calFlag = 0;
            if app.sqrtPushed == 1
                app.dispString = [app.dispString(1:end-2),app.dispString(end),app.dispString(end-1)];
            end
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_8
        function Button_8Pushed(app, event)
             %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = '8';
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '8';
            elseif app.dispString(end)=='0'
                %如果上一个字符是运算符号连着0
                if (app.dispString(end-1)=='('|| app.dispString(end-1)==')' || app.dispString(end-1)=='^' || app.dispString(end-1)=='+' || app.dispString(end-1)=='-' || app.dispString(end-1)=='×' || app.dispString(end-1)=='÷')
                    app.dispString(end) = '8';
                else
                    app.dispString = [app.dispString,'8'];
                end
            else
                app.dispString = [app.dispString,'8'];
            end
            app.equalPushed=0;
            app.calFlag = 0;
            if app.sqrtPushed == 1
                app.dispString = [app.dispString(1:end-2),app.dispString(end),app.dispString(end-1)];
            end
            app.TextResult.Value = app.dispString;
        end

        % Button pushed function: Button_9
        function Button_9Pushed(app, event)
             %刚才显示的是上一次的计算结果,如果现在输入数字,那么重新开始
            if (app.equalPushed==1)
                app.dispString = '9';
                %如果目前的显示为0
            elseif app.dispString =='0'
                app.dispString = '9';
            elseif app.dispString(end)=='0'
                %如果上一个字符是运算符号连着0
                if (app.dispString(end-1)=='('|| app.dispString(end-1)==')' || app.dispString(end-1)=='^' || app.dispString(end-1)=='+' || app.dispString(end-1)=='-' || app.dispString(end-1)=='×' || app.dispString(end-1)=='÷')
                    app.dispString(end) = '9';
                else
                    app.dispString = [app.dispString,'9'];
                end
            else
                app.dispString = [app.dispString,'9'];
            end
            app.equalPushed=0;
            app.calFlag = 0;
            if app.sqrtPushed == 1
                app.dispString = [app.dispString(1:end-2),app.dispString(end),app.dispString(end-1)];
            end
            app.TextResult.Value = app.dispString;
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 510 560];
            app.UIFigure.Name = '弱智计算器';

            % Create TextResult
            app.TextResult = uitextarea(app.UIFigure);
            app.TextResult.HorizontalAlignment = 'right';
            app.TextResult.FontName = 'YaHei Consolas Hybrid';
            app.TextResult.FontSize = 20;
            app.TextResult.Position = [31 451 450 60];

            % Create Button_0
            app.Button_0 = uibutton(app.UIFigure, 'push');
            app.Button_0.ButtonPushedFcn = createCallbackFcn(app, @Button_0Pushed, true);
            app.Button_0.FontSize = 16;
            app.Button_0.Position = [131 131 50 50];
            app.Button_0.Text = '0';

            % Create Button_2
            app.Button_2 = uibutton(app.UIFigure, 'push');
            app.Button_2.ButtonPushedFcn = createCallbackFcn(app, @Button_2Pushed, true);
            app.Button_2.FontSize = 16;
            app.Button_2.Position = [131 211 50 50];
            app.Button_2.Text = '2';

            % Create Button_3
            app.Button_3 = uibutton(app.UIFigure, 'push');
            app.Button_3.ButtonPushedFcn = createCallbackFcn(app, @Button_3Pushed, true);
            app.Button_3.FontSize = 16;
            app.Button_3.Position = [231 211 50 50];
            app.Button_3.Text = '3';

            % Create Button_4
            app.Button_4 = uibutton(app.UIFigure, 'push');
            app.Button_4.ButtonPushedFcn = createCallbackFcn(app, @Button_4Pushed, true);
            app.Button_4.FontSize = 16;
            app.Button_4.Position = [31 291 50 50];
            app.Button_4.Text = '4';

            % Create Button_5
            app.Button_5 = uibutton(app.UIFigure, 'push');
            app.Button_5.ButtonPushedFcn = createCallbackFcn(app, @Button_5Pushed, true);
            app.Button_5.FontSize = 16;
            app.Button_5.Position = [131 291 50 50];
            app.Button_5.Text = '5';

            % Create Button_6
            app.Button_6 = uibutton(app.UIFigure, 'push');
            app.Button_6.ButtonPushedFcn = createCallbackFcn(app, @Button_6Pushed, true);
            app.Button_6.FontSize = 16;
            app.Button_6.Position = [231 291 50 50];
            app.Button_6.Text = '6';

            % Create Button_7
            app.Button_7 = uibutton(app.UIFigure, 'push');
            app.Button_7.ButtonPushedFcn = createCallbackFcn(app, @Button_7Pushed, true);
            app.Button_7.FontSize = 16;
            app.Button_7.Position = [31 371 50 50];
            app.Button_7.Text = '7';

            % Create Button_addsub
            app.Button_addsub = uibutton(app.UIFigure, 'push');
            app.Button_addsub.ButtonPushedFcn = createCallbackFcn(app, @Button_addsubPushed, true);
            app.Button_addsub.FontSize = 20;
            app.Button_addsub.Position = [31 131 50 50];
            app.Button_addsub.Text = '+/-';

            % Create Button_dot
            app.Button_dot = uibutton(app.UIFigure, 'push');
            app.Button_dot.ButtonPushedFcn = createCallbackFcn(app, @Button_dotPushed, true);
            app.Button_dot.FontSize = 16;
            app.Button_dot.FontWeight = 'bold';
            app.Button_dot.Position = [231 131 50 50];
            app.Button_dot.Text = '.';

            % Create DELButton
            app.DELButton = uibutton(app.UIFigure, 'push');
            app.DELButton.ButtonPushedFcn = createCallbackFcn(app, @DELButtonPushed, true);
            app.DELButton.FontSize = 16;
            app.DELButton.Position = [331 371 50 50];
            app.DELButton.Text = 'DEL';

            % Create Button_mult
            app.Button_mult = uibutton(app.UIFigure, 'push');
            app.Button_mult.ButtonPushedFcn = createCallbackFcn(app, @Button_multPushed, true);
            app.Button_mult.FontSize = 20;
            app.Button_mult.Position = [331 211 50 50];
            app.Button_mult.Text = '×';

            % Create Button_add
            app.Button_add = uibutton(app.UIFigure, 'push');
            app.Button_add.ButtonPushedFcn = createCallbackFcn(app, @Button_addPushed, true);
            app.Button_add.FontSize = 20;
            app.Button_add.Position = [331 291 50 50];
            app.Button_add.Text = '+';

            % Create xyButton
            app.xyButton = uibutton(app.UIFigure, 'push');
            app.xyButton.ButtonPushedFcn = createCallbackFcn(app, @xyButtonPushed, true);
            app.xyButton.FontSize = 16;
            app.xyButton.Position = [331 131 50 50];
            app.xyButton.Text = 'x^y';

            % Create ACButton
            app.ACButton = uibutton(app.UIFigure, 'push');
            app.ACButton.ButtonPushedFcn = createCallbackFcn(app, @ACButtonPushed, true);
            app.ACButton.FontSize = 16;
            app.ACButton.Position = [431 371 50 50];
            app.ACButton.Text = 'AC';

            % Create Button_div
            app.Button_div = uibutton(app.UIFigure, 'push');
            app.Button_div.ButtonPushedFcn = createCallbackFcn(app, @Button_divPushed, true);
            app.Button_div.FontSize = 20;
            app.Button_div.Position = [431 211 50 50];
            app.Button_div.Text = '÷';

            % Create Button_sub
            app.Button_sub = uibutton(app.UIFigure, 'push');
            app.Button_sub.ButtonPushedFcn = createCallbackFcn(app, @Button_subPushed, true);
            app.Button_sub.FontSize = 20;
            app.Button_sub.Position = [431 291 50 50];
            app.Button_sub.Text = '-';

            % Create Button_equal
            app.Button_equal = uibutton(app.UIFigure, 'push');
            app.Button_equal.ButtonPushedFcn = createCallbackFcn(app, @Button_equalPushed, true);
            app.Button_equal.FontSize = 20;
            app.Button_equal.Position = [431 131 50 50];
            app.Button_equal.Text = '=';

            % Create Button_9
            app.Button_9 = uibutton(app.UIFigure, 'push');
            app.Button_9.ButtonPushedFcn = createCallbackFcn(app, @Button_9Pushed, true);
            app.Button_9.FontSize = 16;
            app.Button_9.Position = [231 371 50 50];
            app.Button_9.Text = '9';

            % Create Button_1
            app.Button_1 = uibutton(app.UIFigure, 'push');
            app.Button_1.ButtonPushedFcn = createCallbackFcn(app, @Button_1Pushed, true);
            app.Button_1.FontSize = 16;
            app.Button_1.Position = [31 211 50 50];
            app.Button_1.Text = '1';

            % Create Button_left
            app.Button_left = uibutton(app.UIFigure, 'push');
            app.Button_left.ButtonPushedFcn = createCallbackFcn(app, @Button_leftPushed, true);
            app.Button_left.FontSize = 16;
            app.Button_left.Position = [31 51 50 50];
            app.Button_left.Text = '(';

            % Create Button_right
            app.Button_right = uibutton(app.UIFigure, 'push');
            app.Button_right.ButtonPushedFcn = createCallbackFcn(app, @Button_rightPushed, true);
            app.Button_right.FontSize = 16;
            app.Button_right.Position = [131 51 50 50];
            app.Button_right.Text = ')';

            % Create Button_log
            app.Button_log = uibutton(app.UIFigure, 'push');
            app.Button_log.FontSize = 16;
            app.Button_log.Position = [231 51 50 50];
            app.Button_log.Text = 'ln';

            % Create Button_sqrt
            app.Button_sqrt = uibutton(app.UIFigure, 'push');
            app.Button_sqrt.ButtonPushedFcn = createCallbackFcn(app, @Button_sqrtPushed, true);
            app.Button_sqrt.FontSize = 16;
            app.Button_sqrt.Position = [331 51 50 50];
            app.Button_sqrt.Text = 'Sqrt';

            % Create Button_factorial
            app.Button_factorial = uibutton(app.UIFigure, 'push');
            app.Button_factorial.FontSize = 16;
            app.Button_factorial.Position = [431 51 50 50];
            app.Button_factorial.Text = 'n!';

            % Create Button_8
            app.Button_8 = uibutton(app.UIFigure, 'push');
            app.Button_8.ButtonPushedFcn = createCallbackFcn(app, @Button_8Pushed, true);
            app.Button_8.FontSize = 16;
            app.Button_8.Position = [131 371 50 50];
            app.Button_8.Text = '8';

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = app2

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end

 

 

今天的文章用Matlab App Builder写一个简单的计算器分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/29506.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注