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

📄 cofc.m

📁 这是在MATLAB7.0.1版本上实现的一个简单的模拟计算器的界面和功能的程序
💻 M
字号:
tag=get(gcbo,'tag');
global cal
global dot1
global wrong1
global end1
switch tag
    case '1'
        if wrong1==0               %判断是否出错,即上次运算的除数是否为0,如是,则不进行操作
        if end1==1                   %判断当前值是否为按过=之后的结果,如是,则输入的是新的数据
            cstr='1';
            end1=0;
        else
        cstr=get(he,'string');
        switch cstr
            case {'0.','0'}           %如过当前值是0,则输入数据时把首位无效的0忽略
                if dot1==1           %判断是否已经有小数点
                    cstr=[cstr,'1'];
                    dot1=0;
                else
                    cstr='1';
                end
            otherwise
                if cal==1            %判断是否已经按过运算符
                    cstr='1';
                    cal=0;
                else
                    cstr=[cstr,'1'];
                end;    end;   end
        set(he,'string',cstr);
        else
         end
    case '2'
         if wrong1==0
        if end1==1
            cstr='2';
            end1=0;
        else
        cstr=get(he,'string');
        switch cstr
            case {'0.','0'}
                if dot1==1
                    cstr=[cstr,'2'];
                    dot1=0;
                else
                    cstr='2';
                end
            otherwise
                if cal==1
                    cstr='2';
                    cal=0;
                else
                    cstr=[cstr,'2'];
                end;    end;   end
         set(he,'string',cstr);
         else
         end
    case '3'
         if wrong1==0
        if end1==1
            cstr='3';
            end1=0;
        else
        cstr=get(he,'string');
        switch cstr
            case {'0.','0'}
               if dot1==1
                    cstr=[cstr,'3'];
                    dot1=0;
                else
                    cstr='3';
                end
            otherwise
                if cal==1
                    cstr='3';
                    cal=0;
                else
                    cstr=[cstr,'3'];
                end;    end;   end
        set(he,'string',cstr);
         else
         end
    case '4'
        if wrong1==0
        if end1==1
            cstr='4';
            end1=0;
        else
        cstr=get(he,'string');
        switch cstr
            case {'0.','0'}
                if dot1==1
                    cstr=[cstr,'4'];
                    dot1=0;
                else
                    cstr='4';
                end
            otherwise
                if cal==1
                    cstr='4';
                    cal=0;
                else
                    cstr=[cstr,'4'];
                end;    end;   end
        set(he,'string',cstr);
        else
        end
       case '5'
           if wrong1==0
           if end1==1
            cstr='5';
            end1=0;
        else
        cstr=get(he,'string');
        switch cstr
            case {'0.','0'}
                if dot1==1
                    cstr=[cstr,'5'];
                    dot1=0;
                else
                    cstr='5';
                end
            otherwise
                if cal==1
                    cstr='5';
                    cal=0;
                else
                    cstr=[cstr,'5'];
                end;    end;   end
        set(he,'string',cstr);
           else
           end
     case '6'
         if wrong1==0
         if end1==1
            cstr='6';
            end1=0;
        else
        cstr=get(he,'string');
        switch cstr
            case {'0.','0'}
                if dot1==1
                    cstr=[cstr,'6'];
                    dot1=0;
                else
                    cstr='6';
                end
            otherwise
                if cal==1
                    cstr='6';
                    cal=0;
                else
                    cstr=[cstr,'6'];
                end;    end;   end
        set(he,'string',cstr);
         else
         end
    case '7'
        if wrong1==0
        if end1==1
            cstr='7';
            end1=0;
        else
        cstr=get(he,'string');
        switch cstr
            case {'0.','0'}
                if dot1==1
                    cstr=[cstr,'7'];
                    dot1=0;
                else
                    cstr='7';
                end
            otherwise
                if cal==1
                    cstr='7';
                    cal=0;
                else
                    cstr=[cstr,'7'];
               end;    end;   end
        set(he,'string',cstr);
        else
        end
    case '8'
        if wrong1==0
        if end1==1
            cstr='8';
            end1=0;
        else
        cstr=get(he,'string');
        switch cstr
            case {'0.','0'}
               if dot1==1
                    cstr=[cstr,'8'];
                    dot1=0;
                else
                    cstr='8';
                end
            otherwise
                if cal==1
                    cstr='8';
                    cal=0;
                else
                    cstr=[cstr,'8'];
               end;    end;   end
        set(he,'string',cstr);
        else
        end
    case '9'
        if wrong1==0
        if end1==1
            cstr='9';
            end1=0;
        else
        cstr=get(he,'string');
        switch cstr
            case {'0.','0'}
                if dot1==1
                    cstr=[cstr,'9'];
                    dot1=0;
                else
                    cstr='9';
                end
            otherwise
                if cal==1
                    cstr='9';
                    cal=0;
                else
                    cstr=[cstr,'9'];
                end;    end;   end
        set(he,'string',cstr);
        else
        end
        case '0'
            if wrong1==0
            if end1==1
            cstr='0';
            end1=0;
        else
        cstr=get(he,'string');
        switch cstr
            case {'0.','0'}
                if dot1==1
                    cstr=[cstr,'0'];
                    dot1=0;
                else
                    cstr='0';
                end
            otherwise
                if cal==1
                    cstr='0';
                    cal=0;
                else
                    cstr=[cstr,'0'];
                end;    end;   end
        set(he,'string',cstr);
            else
            end
    case '+'
        if wrong1==0
        cal=1;
        val1=str2num(get(he,'string'));
        sign='+';
        else
        end
    case '-'
        if wrong1==0
        cal=1;
        val1=str2num(get(he,'string'));
        sign='-';
        else
        end
    case '*'
        if wrong1==0
        cal=1;
        val1=str2num(get(he,'string'));
        sign='*';
        else
        end
    case '/'
        if wrong1==0
        cal=1;
        val1=str2num(get(he,'string'));
        sign='/';
        else
        end
    case '='
        if wrong1==0
        cal=0;
        val2=str2num(get(he,'string'));
        switch sign
            case '+'
                res=val1+val2;
                end1=1;
            case '-'
                res=val1-val2;
                end1=1;
            case '*'
                res=val1*val2;
                end1=1;
            case '/'
                if val2==0             %判断除数是否为0
                    wrong1=1;
                    end1=1;
                else
                res=val1/val2;
                end1=1;
                end
        end
        if wrong1==1
            set(he,'string','ERROR');
        else
        cstr=num2str(res);
        set(he,'string',cstr);
        end
        else
        end
    case '1/x'
        if wrong1==0
        cstr=get(he,'string');
        temp=str2num(cstr);
        if temp==0                              %判断分母是否为0
            set(he,'string','ERROR');
            wrong1=1;
        else
        temp=1/temp;
        end1=1;
        cstr=num2str(temp);
        set(he,'string',cstr);
        end
        else
        end
    case '%'
        if wrong1==0
        temp=str2num(get(he,'string'));
        temp=temp*100;
        end1=1;
        cstr=num2str(temp);
        set(he,'string',cstr);
        else
        end
    case 'sqrt'
        if wrong1==0
        temp=str2num(get(he,'string'));
        temp=sqrt(temp);
        end1=1;
        cstr=num2str(temp);
        set(he,'string',cstr);
        else
        end
    case '+/-'
        if wrong1==0
            temp=str2num(get(he,'string'));
            temp=-temp;
            cstr=num2str(temp);
            set(he,'string',cstr);
        else
        end
    case 'c'
        cstr='0.';
        wrong1=0;                   %取消出错状态,恢复正常运算
        end1=1;
        set(he,'string',cstr);
    case 'Backspace'
        if wrong1==0
        cstr=get(he,'string');
        cstr=cstr(1:length(cstr)-1);
        switch cstr
            case '0'                 %判断是否删除到只剩一个0
                cstr='0.';
            otherwise
            if length(cstr)>=1
            set(he,'string',cstr);
           else
            set(he,'string','0.');
           end
        end
        else
        end
    case '.'
        if wrong1==0
        cstr=get(he,'string');
        switch cstr
            case '0'                   %判断,如当前值只是一个0,则也可以正常输入小数点
             cstr=[cstr,'.'];  
             dot1=1;
             otherwise
        if findstr(cstr,'.') 
            dot1=1;
        else
            cstr=[cstr,'.'];
        end
        end
        set(he,'string',cstr);        
        else
        end
            
end

⌨️ 快捷键说明

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