首页
学习
活动
专区
圈层
工具
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

MATLAB大作业选登-任意进制的便捷转换

[编者注:]正如陈钦宇同学在设计报告与功能介绍中所说,进制转换是计算机爱好者训练编程的选题之一,也是信息技术基本知识,因此也经常成为选课同学的大作业选题目标,但必须有创新,这一点陈钦宇同学有说明,做到了创新。既然有创新,所以,就有发布的价值。同时,考虑到还是有同学后台索取源码,其实本公众号的贴子已经是源码发布,就像QwQ-32B一样,只提供功能函数,界面自己设计,就不会再有索取源码的必要,所以,下期开始题目或改为“MATLAB大作业选登与QwQ-32B(或deepSeek-R1)实现”,指导粉丝自己完成创建“源码”。

一、设计报告与功能介绍

本程序用于实现进制的便捷转换。老师在布置大作业的时候要求最好编出的程序能解决学习生活中的问题。而本人物理专业,在实验以外的情况下,目前很少会需要用上MATLAB。我学习MATLAB是为了未来的需求。在没有实际需求的情况下,我没有想到其他能够作为题目的程序。

而在看到老师发的几个例子时,我发现有一个将十进制转换为二进制和十六进制的程序。但这个程序只能十进制转二和十六,而且需要手动点击按键转换。于是我着手设计了一个自动且可以相互转换的进制转换器。该程序功能更多且可以应对其他情况。

本程序主体由四个文本框构成。其中二进制、十进制、十六进制的框可以输入字符。输入后自动转换成其他进制。剩余一个框可以自选2到36进制显示。同时如果输入错误的字符(例如在二进制框中输入2,十进制框中输入字母)则会弹出一个提示窗口。考虑到如果2到36进制的框中要检查错误并弹出窗口的话,以我的现有能力,可能需要用大量的代码进行。因此我没有将2到36进制框设为可编辑的文本框。因此只能实现二、十、十六转成其他进制。不支持其他进制转过来。

在本次程序设计中我发现并没有我预料的难度大。在暑期学校时我学习过使用qt进行程序的设计。但qt需要我们自己建构代码的主题,有很多我难以理解的代码。而MATLAB的GUIDE和APP设计工具都已经将主体的结构生成了(APP设计工具甚至将无需我们操作的代码设成了无法修改的状态,进一步防止了误操作和多余操作)。可以说是十分的便捷。同时我在编程中用到的代码也并不难,主要由格式转换类的代码构成。这些代码一部分虽然没学过但是很容易查询到相关用法。除此之外就是一个检查输入文本框内容的代码。这是我查到的用法。因为如果不加入这个判断,在框中输入错误的内容则会报错。在完成编程后,这个程序的使用是我认为足够方便且实用的。

二、程序运行情况

1、启动后的界面

2、在“二进制”编辑框输入0101的情况

3、在“二进制”编辑框输入010101的情况

4、在“十六进制”编辑框输入A的情况

5、在“十制”编辑框输入100的情况

6、在“十制”编辑框输入1000的情况,注意此时任意进制输出的是21进制。

三、程序设计界面

四、程序代码

classdef work < matlab.apps.AppBase

  % Properties that correspond to app components

  properties (Access = public)

      UIFigure  matlab.ui.Figure

      Spinner   matlab.ui.control.Spinner

      Label_4   matlab.ui.control.Label

      Label_3   matlab.ui.control.Label

      Label_2   matlab.ui.control.Label

      Label     matlab.ui.control.Label

      Edit10    matlab.ui.control.EditField

      Edit16    matlab.ui.control.EditField

      Edit2     matlab.ui.control.EditField

      Editany   matlab.ui.control.EditField

  end

  % Callbacks that handle component events

  methods (Access = private)

      % Value changed function: Edit2

      function Edit2ValueChanged(app, event)

          value = app.Edit2.Value;

          if ~isempty(value) && ischar(value) && ~isempty(regexp(value, '^[01]+$','once'))

              x = bin2dec(value);

              app.Editany.Value = dec2base(x,app.Spinner.Value);

              app.Edit10.Value = num2str(x);

              app.Edit16.Value = dec2hex(x);

          else

              msgbox('请输入正确的进制','输入错误');

          end

      end

      % Value changed function: Edit10

      function Edit10ValueChanged(app, event)

          value = app.Edit10.Value;

          if ~isempty(value) && ischar(value) && ~isempty(regexp(value, '^\d+$','once'))

              x = str2double(value);

              app.Edit2.Value = dec2bin(x);

              app.Editany.Value = dec2base(x,app.Spinner.Value);

              app.Edit16.Value = dec2hex(x);

          else

              msgbox('请输入正确的进制','输入错误');

          end

      end

      % Value changed function: Edit16

      function Edit16ValueChanged(app, event)

          value = app.Edit16.Value;

          if ~isempty(value) && ischar(value) && ~isempty(regexp(value, '^[0-9A-Fa-f]+$','once'))

              x = hex2dec(value);

              app.Edit2.Value = dec2bin(x);

              app.Editany.Value = dec2base(x,app.Spinner.Value);

              app.Edit10.Value = num2str(x);

          else

              msgbox('请输入正确的进制','输入错误');

          end

      end

      % Value changing function: Spinner

      function SpinnerValueChanging(app, event)

          changingValue = event.Value;

          if ~isempty(app.Edit10.Value) && ischar(app.Edit10.Value) && ~isempty(regexp(app.Edit10.Value, '^\d+$','once'))

              num10 = str2double(app.Edit10.Value);

              app.Editany.Value = dec2base(num10,changingValue);

          end

      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 797 611];

          app.UIFigure.Name = 'MATLAB App';

          % Create Editany

          app.Editany = uieditfield(app.UIFigure, 'text');

          app.Editany.Editable = 'off';

          app.Editany.Position = [517 244 146 78];

          % Create Edit2

          app.Edit2 = uieditfield(app.UIFigure, 'text');

          app.Edit2.ValueChangedFcn = createCallbackFcn(app, @Edit2ValueChanged, true);

          app.Edit2.Position = [128 422 146 78];

          % Create Edit16

          app.Edit16 = uieditfield(app.UIFigure, 'text');

          app.Edit16.ValueChangedFcn = createCallbackFcn(app, @Edit16ValueChanged, true);

          app.Edit16.Position = [128 244 146 78];

          % Create Edit10

          app.Edit10 = uieditfield(app.UIFigure, 'text');

          app.Edit10.ValueChangedFcn = createCallbackFcn(app, @Edit10ValueChanged, true);

          app.Edit10.Position = [517 422 146 78];

          % Create Label

          app.Label = uilabel(app.UIFigure);

          app.Label.HorizontalAlignment = 'center';

          app.Label.FontSize = 24;

          app.Label.Position = [163 499 77 31];

          app.Label.Text = '二进制';

          % Create Label_2

          app.Label_2 = uilabel(app.UIFigure);

          app.Label_2.HorizontalAlignment = 'center';

          app.Label_2.FontSize = 24;

          app.Label_2.Position = [588 321 53 31];

          app.Label_2.Text = '进制';

          % Create Label_3

          app.Label_3 = uilabel(app.UIFigure);

          app.Label_3.HorizontalAlignment = 'center';

          app.Label_3.FontSize = 24;

          app.Label_3.Position = [550 499 77 31];

          app.Label_3.Text = '十进制';

          % Create Label_4

          app.Label_4 = uilabel(app.UIFigure);

          app.Label_4.HorizontalAlignment = 'center';

          app.Label_4.FontSize = 24;

          app.Label_4.Position = [149 321 101 31];

          app.Label_4.Text = '十六进制';

          % Create Spinner

          app.Spinner = uispinner(app.UIFigure);

          app.Spinner.ValueChangingFcn = createCallbackFcn(app, @SpinnerValueChanging, true);

          app.Spinner.Limits = [2 36];

          app.Spinner.Position = [535 325 55 27];

          app.Spinner.Value = 2;

          % 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 = work

          % Create UIFigure and components

          createComponents(app)

          % Register the app with App Designer

          registerApp(app, app.UIFigure)

          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

五、代码形式程序代码

.rtcContent { padding: 30px; } .lineNode {font-size: 10pt; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-style: normal; font-weight: normal; }classdef work < matlab.apps.AppBase   % Properties that correspond to app components   properties (Access = public)       UIFigure  matlab.ui.Figure       Spinner   matlab.ui.control.Spinner       Label_4   matlab.ui.control.Label       Label_3   matlab.ui.control.Label       Label_2   matlab.ui.control.Label       Label     matlab.ui.control.Label       Edit10    matlab.ui.control.EditField       Edit16    matlab.ui.control.EditField       Edit2     matlab.ui.control.EditField       Editany   matlab.ui.control.EditField   end   % Callbacks that handle component events   methods (Access = private)       % Value changed function: Edit2       function Edit2ValueChanged(app, event)           value = app.Edit2.Value;           if ~isempty(value) && ischar(value) && ~isempty(regexp(value, '^[01]+$','once'))               x = bin2dec(value);               app.Editany.Value = dec2base(x,app.Spinner.Value);               app.Edit10.Value = num2str(x);               app.Edit16.Value = dec2hex(x);           else               msgbox('请输入正确的进制','输入错误');           end       end       % Value changed function: Edit10       function Edit10ValueChanged(app, event)           value = app.Edit10.Value;           if ~isempty(value) && ischar(value) && ~isempty(regexp(value, '^\d+$','once'))               x = str2double(value);               app.Edit2.Value = dec2bin(x);               app.Editany.Value = dec2base(x,app.Spinner.Value);               app.Edit16.Value = dec2hex(x);           else               msgbox('请输入正确的进制','输入错误');           end       end       % Value changed function: Edit16       function Edit16ValueChanged(app, event)           value = app.Edit16.Value;           if ~isempty(value) && ischar(value) && ~isempty(regexp(value, '^[0-9A-Fa-f]+$','once'))               x = hex2dec(value);               app.Edit2.Value = dec2bin(x);               app.Editany.Value = dec2base(x,app.Spinner.Value);               app.Edit10.Value = num2str(x);           else               msgbox('请输入正确的进制','输入错误');           end       end       % Value changing function: Spinner       function SpinnerValueChanging(app, event)           changingValue = event.Value;           if ~isempty(app.Edit10.Value) && ischar(app.Edit10.Value) && ~isempty(regexp(app.Edit10.Value, '^\d+$','once'))               num10 = str2double(app.Edit10.Value);               app.Editany.Value = dec2base(num10,changingValue);           end       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 797 611];           app.UIFigure.Name = 'MATLAB App';           % Create Editany           app.Editany = uieditfield(app.UIFigure, 'text');           app.Editany.Editable = 'off';           app.Editany.Position = [517 244 146 78];           % Create Edit2           app.Edit2 = uieditfield(app.UIFigure, 'text');           app.Edit2.ValueChangedFcn = createCallbackFcn(app, @Edit2ValueChanged, true);           app.Edit2.Position = [128 422 146 78];           % Create Edit16           app.Edit16 = uieditfield(app.UIFigure, 'text');           app.Edit16.ValueChangedFcn = createCallbackFcn(app, @Edit16ValueChanged, true);           app.Edit16.Position = [128 244 146 78];           % Create Edit10           app.Edit10 = uieditfield(app.UIFigure, 'text');           app.Edit10.ValueChangedFcn = createCallbackFcn(app, @Edit10ValueChanged, true);           app.Edit10.Position = [517 422 146 78];           % Create Label           app.Label = uilabel(app.UIFigure);           app.Label.HorizontalAlignment = 'center';           app.Label.FontSize = 24;           app.Label.Position = [163 499 77 31];           app.Label.Text = '二进制';           % Create Label_2           app.Label_2 = uilabel(app.UIFigure);           app.Label_2.HorizontalAlignment = 'center';           app.Label_2.FontSize = 24;           app.Label_2.Position = [588 321 53 31];           app.Label_2.Text = '进制';           % Create Label_3           app.Label_3 = uilabel(app.UIFigure);           app.Label_3.HorizontalAlignment = 'center';           app.Label_3.FontSize = 24;           app.Label_3.Position = [550 499 77 31];           app.Label_3.Text = '十进制';           % Create Label_4           app.Label_4 = uilabel(app.UIFigure);           app.Label_4.HorizontalAlignment = 'center';           app.Label_4.FontSize = 24;           app.Label_4.Position = [149 321 101 31];           app.Label_4.Text = '十六进制';           % Create Spinner           app.Spinner = uispinner(app.UIFigure);           app.Spinner.ValueChangingFcn = createCallbackFcn(app, @SpinnerValueChanging, true);           app.Spinner.Limits = [2 36];           app.Spinner.Position = [535 325 55 27];           app.Spinner.Value = 2;           % 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 = work           % Create UIFigure and components           createComponents(app)           % Register the app with App Designer           registerApp(app, app.UIFigure)           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   endend

六、大作业文件夹

  • 发表于:
  • 原文链接https://page.om.qq.com/page/OBxvOEzu1I-R8zsl5MyHqZJw0
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券