matlab实验报告

  • 格式:doc
  • 大小:103.00 KB
  • 文档页数:14

班级:姓名:学号:指导教师:刘影成绩:电子与信息工程学院信息与通信工程系MATLAB课程设计1.问题一从100开始,顺次搜寻回文数,按照从小到大排列,显示前100个回文数。

所谓回文数是指:一个正整数如果顺着和反过来都是一样的(如13431,反过来也是13431,就称为回文数)1.1源代码lntu01.mi = 100;count = 1;result = 0;while count <= 100if IsPN(i) == 1result(count) = i;i = i + 1;count = count + 1;elsei = i + 1;endendIsPN.mfunction [result] = IsPN(n)k = 0;p = n;while p ~= 0k = k * 10 + rem(p, 10);p = fix(p/10);endif k == nresult = 1;elseresult = 0;end1.2结果>> lntu01101 111 121 131 141 151 161 171 181 191 202 212 222 232 242 252 262 272 282 292 303 313 323 333 343 353 363 373 383 393 404 414 424 434 444 454 464 474 484 494 505 515 525 535 545 555 565 575 585 595 606 616 626 636 646 656 666 676 686 696 707 717 727 737 747 757 767 777 787 797 808 818 828 838 848 858 868 878 888 898 909 919 929 939 949 959 969 979 989 999 1001 1111 1221 1331 1441 1551 1661 1771 1881 19912. 问题二从1开始,顺次搜寻质数,按照从小到大排列,显示前100个质数。

2.1源代码count = 0;i = 1;while count < 100if isprime(i)fprintf('%d ', i)count = count + 1;endi = i + 1;end2.2结果>> lntu022 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 5413. 问题三判断输入的正整数是否既是5的倍数,又是7的倍数,如果是,则显示yes,否则显示no3.1源代码a = input('Please input a number: ');if rem(a, 5) == 0 && rem(a, 7) == 0fprintf('yes\n')elsefprintf('no\n')end3.2结果>> lntu03Please input a number: 35Yes>> lntu03Please input a number: 34No4. 问题四求一个不超过五位的十进制整数各位数值的和。

(如输入2634,输出15)4.1源代码a = input('Please input a number: ');re = 0;while a >= 10decimal = rem(a, 10);a = fix(a/10);re = re + decimal;endre = re + a;fprintf('%d\n', re)4.2结果>> lntu04Please input a number: 15395. 问题五从100开始,顺次搜寻水仙花数,按照从小到大排列,显示前10个水仙花数。

水仙花数是指一个n 位数( n≥3 ),它的每个位上的数字的n 次幂之和等于它本身。

(例如:1^3 + 5^3 + 3^3 = 153)5.1源代码lntu05.mcount = 0;i = 100;while count < 10if GetSXH(i) == ifprintf('%d\n', i)count = count + 1;endi = i + 1;endGetSXH.mfunction [result] = GetSXH(n)digit = 1;n1 = n;while n >= 10digit = digit + 1;n = fix(n/10);endsum = 0;while n1 >= 10decimal = rem(n1, 10);n1 = fix(n1/10);sum = sum + decimal.^digit; endresult = sum + n1.^digit;5.2结果>>lntu05153 370 371 407 1634 8208 9474 54748 92727 930846. 问题六利用GUI编程设计一个计算器,功能越丰富越好,写出必要的流程图,设计思路,算法注释,运行状况示例,必要的截图等等。

6.1页面简介图形界面如图6-1-1,可以点按,可以手动输入。

图6-1-1 计算器界面6.2源代码lntu06.mfunction varargout = lntu06(varargin)% LNTU06 MATLAB code for lntu06.fig% LNTU06, by itself, creates a new LNTU06 or raises the existing % singleton*.%% H = LNTU06 returns the handle to a new LNTU06 or the handle to % the existing singleton*.%% LNTU06('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in LNTU06.M with the given input arguments.%% LNTU06('Property','Value',...) creates a new LNTU06 or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before lntu06_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application% stop. All inputs are passed to lntu06_OpeningFcn via varargin. %% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one% instance to run (singleton)".%% See also: GUIDE, GUIDATA, GUIHANDLES% Edit the above text to modify the response to help lntu06% Last Modified by GUIDE v2.5 10-Jul-2015 21:01:00% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @lntu06_OpeningFcn, ...'gui_OutputFcn', @lntu06_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before lntu06 is made visible.function lntu06_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn.% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% varargin command line arguments to lntu06 (see VARARGIN)% Choose default command line output for lntu06handles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes lntu06 wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line. function varargout = lntu06_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% Get default command line output from handles structurevarargout{1} = handles.output;% --- Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)set(handles.edit1,'String','');% --- Executes on button press in pushbutton5.function pushbutton5_Callback(hObject, eventdata, handles)% hObject handle to pushbutton5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in pushbutton6.function pushbutton6_Callback(hObject, eventdata, handles)% hObject handle to pushbutton6 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% --- Executes on button press in pushbutton7.function pushbutton7_Callback(hObject, eventdata, handles) textString = get(handles.edit1,'String');textString = strcat(textString,'/');set(handles.edit1,'String',textString);% --- Executes on button press in pushbutton12.function pushbutton12_Callback(hObject, eventdata, handles) textString = get(handles.edit1,'String');textString = strcat(textString,'7');set(handles.edit1,'String',textString);% --- Executes on button press in pushbutton13.function pushbutton13_Callback(hObject, eventdata, handles) textString = get(handles.edit1,'String');textString = strcat(textString,'8');set(handles.edit1,'String',textString);% --- Executes on button press in pushbutton14.function pushbutton14_Callback(hObject, eventdata, handles) textString = get(handles.edit1,'String');textString = strcat(textString,'9');set(handles.edit1,'String',textString);% --- Executes on button press in pushbutton15.function pushbutton15_Callback(hObject, eventdata, handles) textString = get(handles.edit1,'String');textString = strcat(textString,'*');set(handles.edit1,'String',textString);% --- Executes on button press in pushbutton16.function pushbutton16_Callback(hObject, eventdata, handles) textString = get(handles.edit1,'String');textString = strcat(textString,'4');set(handles.edit1,'String',textString);% --- Executes on button press in pushbutton17.function pushbutton17_Callback(hObject, eventdata, handles) textString = get(handles.edit1,'String');textString = strcat(textString,'5');set(handles.edit1,'String',textString);% --- Executes on button press in pushbutton18.function pushbutton18_Callback(hObject, eventdata, handles) textString = get(handles.edit1,'String');textString = strcat(textString,'7');set(handles.edit1,'String',textString);% --- Executes on button press in pushbutton19.function pushbutton19_Callback(hObject, eventdata, handles) textString = get(handles.edit1,'String');textString = strcat(textString,'-');set(handles.edit1,'String',textString);% --- Executes on button press in pushbutton20.function pushbutton20_Callback(hObject, eventdata, handles) textString = get(handles.edit1,'String');textString = strcat(textString,'1');set(handles.edit1,'String',textString);% --- Executes on button press in pushbutton21.function pushbutton21_Callback(hObject, eventdata, handles) textString = get(handles.edit1,'String');textString = strcat(textString,'2');set(handles.edit1,'String',textString);% --- Executes on button press in pushbutton22.function pushbutton22_Callback(hObject, eventdata, handles) textString = get(handles.edit1,'String');textString = strcat(textString,'3');set(handles.edit1,'String',textString);% --- Executes on button press in pushbutton23.function pushbutton23_Callback(hObject, eventdata, handles) textString = get(handles.edit1,'String');textString = strcat(textString,'+');set(handles.edit1,'String',textString);% --- Executes on button press in pushbutton24.function pushbutton24_Callback(hObject, eventdata, handles) textString = get(handles.edit1,'String');if textString ~= 0textString = strcat(textString,'0');set(handles.edit1,'String',textString);end% --- Executes on button press in pushbutton26.function pushbutton26_Callback(hObject, eventdata, handles) textString = get(handles.edit1,'String');textString = strcat(textString,'.');set(handles.edit1,'String',textString);% --- Executes on button press in pushbutton27.function pushbutton27_Callback(hObject, eventdata, handles) textString = get(handles.edit1,'String');set(handles.edit1,'String',eval(textString));function edit1_Callback(hObject, eventdata, handles)% hObject handle to edit1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit1 as text% str2double(get(hObject,'String')) returns contents of edit1 as a double% --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles)% hObject handle to edit1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'),get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end% --- Executes on key press with focus on pushbutton27 and none of its controls.function pushbutton27_KeyPressFcn(hObject, eventdata, handles)% hObject handle to pushbutton27 (see GCBO)% eventdata structure with the following fields (seeMATLAB.UI.CONTROL.UICONTROL)% Key: name of the key that was pressed, in lower case% Character: character interpretation of the key(s) that was pressed % Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed% handles structure with handles and user data (see GUIDATA)% --- Executes during object creation, after setting all properties. function pushbutton23_CreateFcn(hObject, eventdata, handles)% hObject handle to pushbutton23 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called% --- Executes during object creation, after setting all properties. function pushbutton19_CreateFcn(hObject, eventdata, handles)% hObject handle to pushbutton19 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns calledlntu06.m。