function varargout = listMathDemo(varargin)
% LISTMATHDEMO M-file for listMathDemo.fig
%
% See the List Box Math Drill Demo documenation file 
% (listBoxMathDemo.doc) for more information.
%
% Created by Dr. C. S. Tritt
% Last revised 1/24/07 (version 1.0)
%
% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help listMathDemo

% Last Modified by GUIDE v2.5 24-Jan-2007 11:46:30

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @listMathDemo_OpeningFcn, ...
                   'gui_OutputFcn',  @listMathDemo_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if 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 listMathDemo is made visible.
function listMathDemo_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 listMathDemo (see VARARGIN)

% Choose default command line output for listMathDemo
handles.output = hObject;

% Initialize (seed) random number generator using system clock.
rand('state', sum(100*clock)); 

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes listMathDemo wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = listMathDemo_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 structure
varargout{1} = handles.output;

% --- Executes during object creation, after setting all properties.
function opList_CreateFcn(hObject, eventdata, handles)
% hObject    handle to opList (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

function ansEdit_Callback(hObject, eventdata, handles)
% hObject    handle to ansEdit (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 ansEdit as text
%        str2double(get(hObject,'String')) returns contents of ansEdit as a double

% Get answer and echo it.
userAns = str2double(get(hObject,'String'));
echoStr = sprintf('%f', userAns);
set(hObject, 'String', echoStr);

% Determine if correct.

if (userAns == handles.Ans)
   set(handles.resultText, 'String', 'Correct!');
else
   set (handles.resultText, 'String', 'Incorrect.');
end

% --- Executes during object creation, after setting all properties.
function ansEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ansEdit (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

% --- Executes on button press in newButton.
function newButton_Callback(hObject, eventdata, handles)
% hObject    handle to newButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Clear existing output.
set(handles.ansEdit, 'String', '');
set(handles.resultText, 'String', '');

% Generate numeric terms.
term1 = floor(10*rand()); % Rounds down to produce values 0 to 9.
term2 = 1 + floor(9*rand()); % Rounds down to produce values 1 to 9.

% Get vector of selected indexes from list box, determine its size
% and initialize and empty character array for the operation symbols.
vOpList = get(handles.opList, 'Value');
opListSize = size(vOpList, 2);
opVector = [''];

% Loop through the vector of operation indexes and accumulate the 
% operation symbols in the vOpList vector.
for n = 1:opListSize
   if vOpList(n) == 1
      opVector = [opVector '+'];
   elseif vOpList(n) == 2
      opVector = [opVector '-'];
   elseif vOpList(n) == 3
      opVector = [opVector '*'];
   elseif vOpList(n) == 4
      opVector = [opVector '/'];
   end
end

% Generate a random index and select the corresponding operator 
% symbol from the vector.
opIndex = 1 + floor(opListSize*rand());
opStr = opVector(opIndex);

% Build problem string, display it & evaluate answer.
probStr = sprintf('%.0f %s %.0f' , term1, opStr, term2);
set(handles.probText, 'String', probStr);
handles.Ans = eval(probStr);

% Update handles structure to save Ans
guidata(hObject, handles);