?? caishuzi.m
字號(hào):
function varargout = caishuzi(varargin)%CAISHUZI M-file for caishuzi.fig% CAISHUZI, by itself, creates a new CAISHUZI or raises the existing% singleton*.%% H = CAISHUZI returns the handle to a new CAISHUZI or the handle to% the existing singleton*.%% CAISHUZI('Property','Value',...) creates a new CAISHUZI using the% given property value pairs. Unrecognized properties are passed via% varargin to caishuzi_OpeningFcn. This calling syntax produces a% warning when there is an existing singleton*.%% CAISHUZI('CALLBACK') and CAISHUZI('CALLBACK',hObject,...) call the% local function named CALLBACK in CAISHUZI.M with the given input% arguments.%% *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 caishuzi% Last Modified by GUIDE v2.5 08-Mar-2008 22:02:40% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @caishuzi_OpeningFcn, ... 'gui_OutputFcn', @caishuzi_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{:});else gui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before caishuzi is made visible.function caishuzi_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 unrecognized PropertyName/PropertyValue pairs from the% command line (see VARARGIN)% Choose default command line output for caishuzihandles.output = hObject;handles.source = rand4; %生成一個(gè)隨機(jī)的四位數(shù)handles.pc = 0; %用來保存按‘ok’鍵的次數(shù),但不成功% Update handles structureguidata(hObject, handles);% UIWAIT makes caishuzi wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = caishuzi_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;function edit_input_Callback(hObject, eventdata, handles)% hObject handle to edit_input (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 edit_input as text% str2double(get(hObject,'String')) returns contents of edit_input as a doublehandles.instr = str2double(get(hObject,'String')); %從編輯框取輸入值guidata(hObject, handles);% --- Executes during object creation, after setting all properties.function edit_input_CreateFcn(hObject, eventdata, handles)% hObject handle to edit_input (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 button press in pushbutton_clo.function pushbutton_clo_Callback(hObject, eventdata, handles)% hObject handle to pushbutton_clo (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)delete(handles.figure1) %關(guān)閉界面% --- Executes on selection change in popupmenu.function popupmenu_Callback(hObject, eventdata, handles)% hObject handle to popupmenu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: contents = get(hObject,'String') returns popupmenu contents as cell array% contents{get(hObject,'Value')} returns selected item from popupmenuhandles.bushu = get(hObject,'value'); %獲得指定步數(shù)限制,還未完成該功能% --- Executes during object creation, after setting all properties.function popupmenu_CreateFcn(hObject, eventdata, handles)% hObject handle to popupmenu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: popupmenu 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 button press in pushbutton_ok.function pushbutton_ok_Callback(hObject, eventdata, handles)% hObject handle to pushbutton_ok (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)A = handles.source; %A為生成的隨機(jī)4位數(shù)instr = handles.instr; %輸入的4位數(shù)handles.pc = handles.pc + 1; %按鍵次數(shù)b = 0; %將輸入的4位數(shù)拆開,組成矩陣B for i=1:4 B(5-i) = mod(instr,10); for j=1:4 if isequal(B(5-i),A(j)) %判斷B與A是否有相同元素,并將其個(gè)數(shù)存入b b=b+1; end end instr = fix(instr/10);end%判斷輸入是否合法f=0; for i=1:4 C(1:4)=B(i); if sum(C==B)>=2 set(handles.text_rez, 'string','請(qǐng)重新輸入4位不重復(fù)數(shù)!'); f=1; break endendif f==0 %輸入合法a=sum(A==B); %A與B的元素及其位置均相同的元素個(gè)數(shù)b = b - a; %A與B的元素相同但位置不相同的元素個(gè)數(shù)set(handles.text_rez, 'string',['第',num2str(handles.pc),'步: ',num2str(B),' ',... num2str(a),'A',num2str(b),'B']); %顯示步驟及結(jié)果end % --- Executes on button press in pushbutton_reset.function pushbutton_reset_Callback(hObject, eventdata, handles)% hObject handle to pushbutton_reset (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)handles.source = rand4; %重新生成一個(gè)隨機(jī)4位數(shù)guidata(hObject, handles);% --- Executes on button press in pushbutton_ans.function pushbutton_ans_Callback(hObject, eventdata, handles)% hObject handle to pushbutton_ans (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(handles.text_rez, 'string',['正確答案: ',num2str(handles.source)]); %顯示正確答案%定義生成隨機(jī)4位數(shù)的函數(shù)function Y=rand4()M=randperm(10);M(find(M==10))=0;Y=M(1:4);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -