function varargout = Taschenrechner(varargin)

    % Begin initialization code - DO NOT EDIT
    gui_Singleton = 1;
    gui_State = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @Taschenrechner_OpeningFcn, ...
                       'gui_OutputFcn',  @Taschenrechner_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 Taschenrechner is made visible.
function Taschenrechner_OpeningFcn(hObject, eventdata, handles, varargin)
    handles.output = 0;    
    guidata(hObject, handles);

    % Daten initialisieren 
    initResult( hObject, handles );
    

% --- Outputs from this function are returned to the command line.
function varargout = Taschenrechner_OutputFcn(hObject, eventdata, handles) 
    varargout{1} = 0;


% --- Ziffern-Pushbuttons - Ziffern in Ziffernpuffer übernehmen 
function pushbutton1_Callback(hObject, eventdata, handles)
    addResult( '1', handles, hObject );

function pushbutton2_Callback(hObject, eventdata, handles)
    addResult( '2', handles, hObject );

function pushbutton3_Callback(hObject, eventdata, handles)
    addResult( '3', handles, hObject );

function pushbutton4_Callback(hObject, eventdata, handles)
    addResult( '4', handles, hObject );

function pushbutton5_Callback(hObject, eventdata, handles)
    addResult( '5', handles, hObject );

function pushbutton6_Callback(hObject, eventdata, handles)
    addResult( '6', handles, hObject );

function pushbutton7_Callback(hObject, eventdata, handles)
    addResult( '7', handles, hObject );

function pushbutton8_Callback(hObject, eventdata, handles)
    addResult( '8', handles, hObject );

function pushbutton9_Callback(hObject, eventdata, handles)
    addResult( '9', handles, hObject );

function pushbutton0_Callback(hObject, eventdata, handles)
    addResult( '0', handles, hObject );

    
% --- Aktions-Pushbuttons: letzte Aktion ausführen  
function pushbuttonPlus_Callback(hObject, eventdata, handles)
    doLastAction( 1, hObject, handles );        

function pushbuttonMinus_Callback(hObject, eventdata, handles)
    doLastAction( 2, hObject, handles );        

function pushbuttonMult_Callback(hObject, eventdata, handles)
    doLastAction( 3, hObject, handles );        

function pushbuttonDiv_Callback(hObject, eventdata, handles)
    doLastAction( 4, hObject, handles );        

function pushbuttonResult_Callback(hObject, eventdata, handles)
    doLastAction( 0, hObject, handles );        

% C-Pushbutton: Daten initialisieren 
function pushbutton20_Callback(hObject, eventdata, handles)
    initResult( hObject, handles )

    
function doLastAction( action, hObject, handles )
% action: gerade ausgelöste Aktion 
%           arbeitet zuerst letzte Aktion ab, 
%           z.B. 1 + 2 -, wertet jetzt 1+2 aus 

    % Inhalt des Ziffernfeldes in Zahl umwandeln 
    if( isempty( handles.result ) )
        var2 = 0;   % leerer Puffer entspricht Zahl 0 
    else
        var2 = str2double( handles.result );
    end 
                
    % nach letzter Aktion verzweigen und abarbeiten 
    switch( handles.action ) 
        % nach = : Daten so übernehmen, z.B. 1+2= +   
        %     Denn die letzte Aktion hatte 1+2 bereits ausgewertet 
        case 0
          handles.var1 = handles.var1;  
          
        % nach + : aktuelle Daten var2 zu vorherigen addieren 
        %     und in der Variablen handles.var1 merken 
        case 1
          handles.var1 = handles.var1 + var2;  
          
        case 2
          handles.var1 = handles.var1 - var2; 
          
        case 3
          handles.var1 = handles.var1 * var2;  
          
        case 4
          handles.var1 = handles.var1 / var2; 
          
        % nach -1 = Init: bei 1. Aktion akt. Daten merken 
        otherwise
          handles.var1 = var2; 
    end
    
    % letzte Aktion merken 
    handles.action = action;
    
    % berechnete Daten in Textfeld eintragen  
    set( handles.text1, 'String', handles.var1 ); 
    
    % Ziffernpuffer leeren für nächste Eingabe
    handles.result = '';
    
    guidata(hObject, handles);  
    
    
function addResult( str, handles, hObject )
% nächste Ziffer in Ziffernpuffer eintragen 

   handles.result = sprintf( '%s%s', handles.result, str ); 
   
   % alle Ziffern in Textfeld eintragen 
   set( handles.text1, 'String', handles.result ); 
   
   guidata(hObject, handles);
   
 
function initResult( hObject, handles )
% Initialisierung der Daten, z.B. auch nach C-Button 

    handles.result = '';   % Ziffernpuffer leeren 
    handles.var1   =  0;   % zuletzt gemerkte Zahl
    handles.action = -1;   % -1: Init, 0:=, 1:+, 2:-, 3:*, 4:/
    
    % Textfeld leeren 
    set( handles.text1, 'String', handles.result ); 

    % Update handles structure
    guidata(hObject, handles);
    
    


