今天我们就来分享一下如何用MATLAB中的guide框架设计一款简易+科学型的计算器,下次分享基于APP Designer框架的。其实,这款计算器去年这个时候已经做好了,没来得及分享。

简易计算器的一些思想,最早在这里分享过 基于MATLAB程序设计的计算器(上)。

我们用GUI Layout Toolbox布局管理器构建计算器的界面,用MVC设计模式,将界面和逻辑分离,基本的做法就是这样。下图是构建的简易计算器和科学型计算器的界面示意图。

简易计算器

科学型计算器

一. 简易型计算器

这是构建科学型计算器的基础,有了它可以将简易计算器的功能集成到科学型计算器,已达到简化的目的。下面我们聊一聊主要的实现过程吧。

(1)屏幕显示和数字按键

我们每按一个键就会在屏幕上显示当前按下的数字,数字0区别于其他的数字键1~9,定义一个bool类型的变量isInition,初始化为true,对于数字0,如果显示器中的数字是0的话,继续操作还是0,否则末尾追加'0',这时isInition=true,而对于其他按键, 如果是输入的新数值,屏幕显示0,因为输入了新的数值,所以后面的操作是在此基础上进行的,故isInition作为记录新数值的boolean值为false,否则就末尾追加该数字。

(2)一,二元操作符按键

这个实现容易,屏幕上显示的数值作为第一个运算值,当按键二元操作符(+,-,*,/)后,随后输入的值作为第二个值,再进行数学操作。一元操作符(1/x等)就更简单了,直接对屏幕的数值进行运算,再显示到屏幕上。效果如下:

(3)其它按键

MS: 存储显示屏上的数字到存储器.

MC: 清除存储器内容.

MR: 调用存储器内容.

M+: 把目前显示的值放在存储器中,并与存储器中的数值相加.

M-: 把目前显示的值放在存储器中,并与存储器中的数值相减.

二. 科学型计算器

科学型计算器只是多了一些功能,主要集中在左边部分的一些按钮,右半部分是简易型的。

(1)角度,弧度,百分度,三角和反三角函数

默认情况下是角度制的计算,可以选择弧度和梯度,反三角函数等计算时点击Inv按键,效果如下图所示:


(2)dms/deg

dms表示Degree-Minute-Second,对一个以小数表述的角度用度分秒的形式来表示,比如26.12,用dms表示就是26.0712

deg可以看作是dms的反向计算,即26.0712可以转换成26.12


(3)(, )

括弧可以优先计算,主要的实现思想是按下左括弧后将括弧外边的数值进行存储为第一个数并记录运算符,直到按下右括弧时,将中间值存为第二个值再操作计算, 最后按等号得结果。

(4)其他按键

其他的按键都相对容易,都是一元或者二元操作了。

三. 工具下载

这里提供免安装的exe文件和mlappinstall文件,第二个可以直接双击安装在MATLAB APP—>My APP栏目中,想要下载的朋友可以在后台直接回复 简易计算器 获得下载链接。

这里再分享一下构建简易计算器界面的源码:

  • 菜单栏的构建
classdef MenuBarView < handle%MENUBARVIEW is a figure's menu bar view%   obj = MenuBarView(thisContainer, vObj, mObj) produces a object of MenuBarView%   class, where thisContainer is the main container for it.%   vObj is the object of StdCalculatorView class, mObj is the subject%   of CalculatorModel class.%   Writen by: Poor Prince%%   See also BasicView, ShowPanelView, StdCalculatorView, SciCalculatorView.%   Copyright 2018-2019 The Poor science and technology.%propertiesstdMenusciMenumObj  % CalculatorModel's objectcObj  % MenuBarController's objectend  % end propertiesmethodsfunction obj = MenuBarView(thisContainer, vObj, mObj)lookMenu = uimenu('Parent', thisContainer, 'Label', '查看(V)');calculatorMenu = uimenu('Parent', lookMenu, 'Label', '计算器');obj.stdMenu = uimenu('Parent', calculatorMenu, 'Label', '标准', 'Checked', 'on');obj.sciMenu = uimenu('Parent', calculatorMenu, 'Label', '科学');obj.mObj = mObj;obj.cObj = obj.makeController(vObj);obj.attachToController(obj.cObj);end  % end constructorfunction controllerObj = makeController(obj, vObj)controllerObj = MenuBarController(vObj, obj.mObj, obj);end  % end makeControllerfunction attachToController(obj, controller)set(obj.stdMenu, 'Callback', @controller.stdMenu_callback);set(obj.sciMenu, 'Callback', @controller.sciMenu_callback);end  % end attachToControllerend  % end methodsend  % end classdef
  • 功能按键区域的构建
classdef KeyPanelView < handle%KEYPANELVIEW is the all key pushbutton for the calculator%   obj = KeyPanelView(thisContainer, vObj, mObj) produces a object of KeyPanelView%   class, where thisContainer is the main container for it. vObj is the object of StdCalculatorView class, mObj is the subject%   of CalculatorModel class.%   Writen by: Poor Prince%   See also BasicView, ShowPanelView, MenubarView, StdCalculatorView, SciCalculatorView.%   Copyright 2018-2019 The Poor science and technology.%propertiesmcBtn;mrBtn;msBtn;maddBtn;msubBtn;ceBtn;cBtn;addorsubBtn;leftarrowBtn;zerosBtn;oneBtn;twoBtn;threeBtn;fourBtn;fiveBtn;sixBtn;sevenBtn;eightBtn;nineBtn;dotBtn;addBtn;subBtn;divBtn;mulBtn;sqrtBtn;modBtn;invBtn;equalBtn;mObj;  % CalculatorModel's objectcObj;  % KeyPanelController's objectend  % end propertiesmethodsfunction obj = KeyPanelView(thisContainer, vObj, mObj)obj.buildGUI(thisContainer);obj.mObj = mObj;obj.cObj = obj.makeController(vObj);end  % end constructorfunction controllerObj = makeController(obj, vObj)controllerObj = KeyPanelController(vObj, obj.mObj, obj);end  % end makeController% buildGUIfunction buildGUI(obj, thisContainer)% Construct the layout manager%firstColLay = uiextras.VBox('Parent', thisContainer);secondColLay = uiextras.Grid('Parent', thisContainer);thirdColLay = uiextras.VBox('Parent', thisContainer);set(thisContainer, 'Sizes', [-2, -2, -1]);%firstColTopLay = uiextras.Grid('Parent', firstColLay);firstColBotLay = uiextras.HBox('Parent', firstColLay);set(firstColLay, 'Sizes', [-5, -1]);%%% Adds a control to the layout manager%controlFontsize = 10;%%------------------------------------------%             firstColTopLay%------------------------------------------% first column%% MC Keyobj.mcBtn = uicontrol('Parent', firstColTopLay, 'String', '<html>MC</html>', ...'Fontsize', controlFontsize);% ← Keyobj.leftarrowBtn = uicontrol('Parent', firstColTopLay, 'String', '<html>←</html>', ...'Fontsize', controlFontsize);% 7 Keyobj.sevenBtn = uicontrol('Parent', firstColTopLay, 'String', '<html>7</html>', ...'Fontsize', controlFontsize);% 4 Keyobj.fourBtn = uicontrol('Parent', firstColTopLay, 'String', '<html>4</html>', ...'Fontsize', controlFontsize);% 1 Keyobj.oneBtn = uicontrol('Parent', firstColTopLay, 'String', '<html>1</html>', ...'Fontsize', controlFontsize);%% second column%% MR Keyobj.mrBtn = uicontrol('Parent', firstColTopLay, 'String', '<html>MR</html>', ...'Fontsize', controlFontsize);% CE Keyobj.ceBtn = uicontrol('Parent', firstColTopLay, 'String', '<html>CE</html>', ...'Fontsize', controlFontsize);% 8 Keyobj.eightBtn = uicontrol('Parent', firstColTopLay, 'String', '<html>8</html>', ...'Fontsize', controlFontsize);% 5 Keyobj.fiveBtn = uicontrol('Parent', firstColTopLay, 'String', '<html>5</html>', ...'Fontsize', controlFontsize);% 2 Keyobj.twoBtn = uicontrol('Parent', firstColTopLay, 'String', '<html>2<html>', ...'Fontsize',controlFontsize);%set(firstColTopLay, 'Rowsizes', [-1, -1, -1, -1, -1]);%%------------------------------------------%             firstColBotLay%------------------------------------------%% 0 Keyobj.zerosBtn = uicontrol('Parent', firstColBotLay, 'String', '<html>0</html>', ...'Fontsize', controlFontsize);%%------------------------------------------%             secondColLay%------------------------------------------%% first column%% MS Keyobj.msBtn = uicontrol('Parent', secondColLay, 'String', '<html>MS<html>', ...'Fontsize', controlFontsize);% C Keyobj.cBtn = uicontrol('Parent', secondColLay, 'String', '<html>C</html>', ...'Fontsize', controlFontsize);% 9 Keyobj.nineBtn = uicontrol('Parent', secondColLay, 'String', '<html>9</html>', ...'Fontsize', controlFontsize);% 6 Keyobj.sixBtn = uicontrol('Parent', secondColLay, 'String', '<html>6</html>', ...'Fontsize', controlFontsize);% 3 Keyobj.threeBtn = uicontrol('Parent', secondColLay, 'String', '<html>3</html>', ...'Fontsize', controlFontsize);% . Keyobj.dotBtn = uicontrol('Parent', secondColLay, 'String', '<html>.</html>', ...'Fontsize', controlFontsize);%% second column%% M+ Keyobj.maddBtn = uicontrol('Parent', secondColLay, 'String', '<html>M+</html>', ...'Fontsize', controlFontsize);% ± Keyobj.addorsubBtn = uicontrol('Parent', secondColLay, 'String', '<html>±</html>', ...'Fontsize', controlFontsize);% ÷ Keyobj.divBtn = uicontrol('Parent', secondColLay, 'String', '<html>÷</html>', ...'Fontsize', controlFontsize);% × Keyobj.mulBtn = uicontrol('Parent', secondColLay, 'String', '<html>×</html>', ...'Fontsize', controlFontsize);% - Keyobj.subBtn = uicontrol('Parent', secondColLay, 'String', '<html>-</html>', ...'Fontsize', controlFontsize);% + Keyobj.addBtn = uicontrol('Parent', secondColLay, 'String', '<html>+</html>', ...'Fontsize', controlFontsize);%set(secondColLay, 'Rowsizes', [-1, -1, -1, -1, -1, -1]);%%------------------------------------------%             thirdColLay%------------------------------------------%% M- Keyobj.msubBtn = uicontrol('Parent', thirdColLay, 'String', '<html>M-<html>', ...'Fontsize', controlFontsize);% √ Keyobj.sqrtBtn = uicontrol('Parent', thirdColLay, 'String', '<html>√<html>', ...'Fontsize', controlFontsize);% '%' Keyobj.modBtn = uicontrol('Parent', thirdColLay, 'String', '<html>%</html>', ...'Fontsize', controlFontsize);% '1/x' Keyobj.invBtn = uicontrol('Parent',thirdColLay,'String', '<html>1/x</html>', ...'Fontsize',controlFontsize);% = Keyobj.equalBtn = uicontrol('Parent', thirdColLay, 'String', '<html>=</html>', ...'Fontsize', controlFontsize);%set(thirdColLay, 'Sizes', [-1, -1, -1, -1, -2]);%end %end buildGUIend  % end methodsend  % end classdef
  • 显示面板的构建
classdef ShowPanelView < handle%SHOWPANELVIEW is the result show for the calculator%   obj = ShowPanelView(thisContainer) produces a object of ShowPanelView%   class, where thisContainer is the main container for it.%   Writen by: Poor Prince%   See also BasicView, MenubarView, StdCalculatorView, SciCalculatorView.%   Copyright 2018-2019 The Poor science and technology.%propertiesshowText;historyText;end  % end propertiesmethodsfunction obj = ShowPanelView(thisContainer) showPanel = uipanel(thisContainer);mainLayout = uiextras.VBox('Parent', showPanel);% historyTextobj.historyText = uicontrol('Parent', mainLayout, ...'Style', 'text', ...'String', ' ', ...'FontSize', 10, ...'Enable', 'on', ...'HorizontalAlignment', 'right', ...'BackgroundColor', [1, 1, 1], ...'ForegroundColor', [0.5, 0.5, 0.5]);% displayerobj.showText = uicontrol('Parent', mainLayout, ...'Style', 'text', ...'String', '0', ...'FontSize', 17, ...'FontWeight', 'bold', ...'Enable', 'on', ...'HorizontalAlignment', 'right', ...'BackgroundColor', 'w');set(mainLayout, 'Sizes', [-2, -3]);end  % end constructorend  % end methodsend  % end classdef
  • 基础窗口的构建
classdef BasicView < handle%BASICVIEW  is the base container for calculator%   obj = BasicView(figWidth, figHeight) produces a object of BasicView class%   Input arguments:%   viewsize is the size of GUI%   Writen by: Poor Prince%   See also CalculatorView StdCalculatorView SciCalculatorView%   Copyright 2018-2019 The Poor science and technology.properties(Access = public)hfig;figWidth;figHeight;showContainer;funcContainer;end  % end propertiesmethods% Constructorfunction obj = BasicView(figWidth, figHeight)obj.figWidth = figWidth;obj.figHeight = figHeight;scrSize = get(0, 'ScreenSize');cenX = scrSize(3) / 2;cenY = scrSize(4) / 2;obj.hfig = figure('Menubar', 'none', ...'Numbertitle', 'off', ...'Name','计算器', ...'Resize', 'on', ...'Visible', 'off');set(obj.hfig, 'Position', [cenX-115, cenY-140, figWidth, figHeight]);thisContainer = uiextras.VBox('Parent', obj.hfig);obj.showContainer = uiextras.VBox('Parent', thisContainer);obj.funcContainer = uiextras.HBox('Parent', thisContainer);set(thisContainer, 'Sizes', [-1, -4], 'Padding', 5, 'Spacing', 5);set(obj.hfig, 'Visible', 'on');endend  % end methodsend  % end classdef
  • 简易计算器的构建
classdef StdCalculatorView < BasicView%StdCalculatorView build GUI of standard form.%   obj = StdCalculatorView() produces a object of StdCalculatorView%   class, where modelObj is the object of CalculatorModel class%   Writen by: Poor Prince%%   See also SciCalculatorView%   Copyright 2018-2019 The Poor science and technology.%propertiesmenubarViewshowpanelViewkeypanelViewmObj  % the object of the Model classstdConObj  % the object of the StdCalculatorController classend  % end propertiesmethods% Constructorfunction obj = StdCalculatorView(mObj)figWidth = 230;figHeight = 290;obj = obj@BasicView(figWidth, figHeight);obj.mObj = mObj;obj.menubarView = MenuBarView(obj.hfig, obj, obj.mObj);obj.showpanelView = ShowPanelView(obj.showContainer);obj.keypanelView = KeyPanelView(obj.funcContainer, obj, obj.mObj);%obj.attachToController(obj.stdConObj);       end  % end constructorend  % end methodsend  % end classdef
  • 模型类
classdef CalculatorModel < handle%CALCULATORMODEL Model of this calculator%   obj = CalculatorModel() produces a object of CalculatorModel class%   Writen by: Poor Prince%%   See also%   Copyright 2018-2019 The Poor science and technology.%%%
end %end classdef
  • 控制器类
classdef StdCalculatorController < handle%STDCALCULATORCONTROLLER Controller of this calculator for%   StdCalculatorView%   obj = StdCalculatorController(vObj, mObj) produces a object of StdCalculatorController%   class. where mObj and vObj is the object of CalculatorModel class%   and StdCalculatorView class respectively.%   Writen by: Poor Prince%%   See also .%   Copyright 2018-2019 The Poor science and technology.%propertiesvObj;  % StdCalculatorView objectmObj;  % CalculatorModel objectendmethods%function obj = StdCalculatorController(vObj, mObj)obj.vObj = vObj;obj.mObj = mObj;endend % end methodsend % end classdef
  • 主程序
clear;
clc;
close force all;mObj = CalculatorModel();
StdCalculatorView(mObj);

当然,这里主要省略了模型和控制器的代码,提供了一个空盒子,目的仅仅是构建计算器的界面,如果有需要的朋友可以直接在微信公众号后台回复消息,我们可以交流。


基于MATLAB程序设计的计算器(中)相关推荐

  1. 基于matlab的SAR图像中自动目标识别

    一.前言 此示例演示如何使用深度学习工具箱和并行计算工具箱™™训练基于区域的卷积神经网络 (R-CNN) 以识别大场景合成孔径雷达 (SAR) 图像中的目标. 深度学习工具箱提供了一个框架,用于设计和 ...

  2. matlab固定床反应器,基于MATLAB在反应器优化设计中的应用

    引言:在反应器优化设计过程中,经常会遇到选择设计参数,使得设计方案既满足工艺要求,又能降低设备投资及产品成本的问题.而对于一个优化项目而言,需解决两个问题,一是根据实际问题建立合理的数学模型,二是数学 ...

  3. matlab gui 图像增强,基于MATLAB GUI的图像增强技术的实现

    基于MATLAB GUI的图像增强技术的实现 英英 [期刊名称]<内蒙古广播与电视技术> [年(卷),期]2017(034)003 [摘要]随着数字化时代的到来,人们对图像处理的要求越来越 ...

  4. MATLAB#183;提取图像中多个目标

    基于matlab工具箱提取图像中的多目标特征(代码如下): 代码前面部分为提取图像的边界信息,调用了后面的遍历函数Pixel_Search,函数实现方法见后~ %%ROI Testing close ...

  5. 【老生谈算法】matlab实现车牌识别中值滤波算法——车牌识别中值滤波算法

    基于Matlab的车牌识别中值滤波算法的研究与实现 1.原文下载: 本算法原文如下,有需要的朋友可以点击进行下载 序号 原文(点击下载) 本项目原文 [老生谈算法]基于Matlab的车牌识别中值滤波算 ...

  6. matlab求解外弹道,基于MATLAB∕Simulink的通用质点外弹道程序设计.pdf

    2017.01 设计与研发 基于 MATLAB/Simulink 的通用质点外弹道程序设计 崔 瀚 (沈阳工学院兵器类虚拟仿真实验教学中心, 辽宁抚顺 ,113122 ) 摘要:本文以弹丸质点外弹道学 ...

  7. Matlab语音采集与读写程序,基于MATLAB的语音信号录制采集和分析的程序设计

    理 论广 角 ● I 基于 MATLAB的语音信号录制采集和分析的程序设计 刘 晓炯 (西北民族大学电气工程学院 甘肃 兰州I 730030) [摘 要]语音信号处理技术是语音处理领域中新近发展起来的 ...

  8. matlab高斯投影坐标,基于matlab的高斯投影正反算与相邻带坐标换算程序设计

    第 15 卷 第 2 期 中 国 水 运 Vol.15 No.2 2015 年 2 月 China Water Transport February 2015 收稿日期:2014-01-15 作者简介 ...

  9. 基于MATLAB手写体数字识别程序设计

    基于MATLAB手写体数字识别程序设计 手写体识别由于其实用性,一直处于研究进步的阶段,本文主要针对的是对0-9十个手写数字体脱机识别,在Matlab中对样本部分为进行16特征的提取,分别采用最小距离 ...

最新文章

  1. matlab用diag直接使用错误_你真的用对了卫生巾吗?这6个错误的使用方法,你占了几个...
  2. 重装WIN7之后使用Ubuntu LiveCD修复grub2双系统引导
  3. DeepLearning tutorial(1)Softmax回归原理简介+代码详解
  4. Response 输出文件流过程中的等待效果
  5. 无法启动此程序因为计算机中丢失msvcr110,无法启动此程序因为计算机中丢失msvcr110,教您无法运行程序提示计算机中丢失...
  6. Java EE之旅02 CSS基础
  7. 南京大学计算机科学系照片,欧拉图-南京大学计算机科学与技术系.pdf
  8. Hystrix面试 - 深入 Hystrix 断路器执行原理
  9. 【Vue】—计算属性缓存VS方法以及侦听器的区别
  10. angularjs路由监听,uirouter感知路由变化,解决uirouter路由监听不生效的问题
  11. virtualmin修改php.ini,virtualmin安装和配置使用
  12. 数据科学常用Python库介绍--Numpy、Scipy、Pandas、Matplotlib、Plotly、SciKit-Learn等
  13. MagicKey - 魔兽世界工具(双开工具,类似按键精灵)
  14. 最新出炉!java私塾下载
  15. python网络爬虫网易云音乐guihub_Github获8300星!用Python开发的一个命令行的网易云音乐...
  16. 036多级节点实现层叠展开与收缩的功能
  17. Linux误删文件恢复
  18. 微信小程序判断手机号码格式正确与否的代码
  19. 我从非科班转到图像算法工程师(图像识别、机器学习、深度学习)(3个月)的经历
  20. 实现边坍塌的网格简化方法

热门文章

  1. Andoird conflicts with another tag that has the same ID
  2. Oracle 实例恢复--转自沙弥的世界
  3. 单片机 指令SETB EA
  4. 电路板Layout爬电距离、电气间隙的确定
  5. 阿里云服务器cpu连续n天使用率为100%问题解决方案!
  6. 2021年中国互联网广告市场规模、竞争格局与发展趋势分析,行业发展稳中向好「图」
  7. TigerGraph首将模式匹配与高效图计算相结合,为欺诈检测、网络安全保护、人工智能等应用增砖加瓦!
  8. 【信号调理】ADC保护电路/ADC缓冲器
  9. C#内存释放(垃圾回收)
  10. .Net中如何释放内存?