// Tema6Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "Tema6.h"
#include "Tema6Dlg.h"
#include <time.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTema6Dlg dialog

CTema6Dlg::CTema6Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTema6Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTema6Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTema6Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTema6Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTema6Dlg, CDialog)
	//{{AFX_MSG_MAP(CTema6Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_PLUS, OnPlus)
	ON_BN_CLICKED(IDC_NR1, OnNr1)
	ON_BN_CLICKED(IDC_NR2, OnNr2)
	ON_BN_CLICKED(IDC_MINUS, OnMinus)
	ON_BN_CLICKED(IDC_MUL, OnMul)
	ON_BN_CLICKED(IDC_DIV, OnDiv)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTema6Dlg message handlers

BOOL CTema6Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_Nr1Apasat=0;
	m_Nr2Apasat=0;
	srand((unsigned) time (NULL));
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CTema6Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CTema6Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CTema6Dlg::OnNr1() 
{
	// TODO: Add your control notification handler code here
	m_Nr1=rand()%100;;
	m_Nr1Apasat++;
}

void CTema6Dlg::OnNr2() 
{
	// TODO: Add your control notification handler code here
	m_Nr2=rand()%100;;
	m_Nr2Apasat++;
}

void CTema6Dlg::OnPlus() 
{
	// TODO: Add your control notification handler code here
	operatie=ADUNARE;
	Calcul();
}

void CTema6Dlg::OnMinus() 
{
	// TODO: Add your control notification handler code here
	operatie=SCADERE;
	Calcul();
}

void CTema6Dlg::OnMul() 
{
	// TODO: Add your control notification handler code here
	operatie=INMULTIRE;
	Calcul();
}

void CTema6Dlg::OnDiv() 
{
	// TODO: Add your control notification handler code here
	operatie=IMPARTIRE;
	Calcul();

}

void CTema6Dlg::Calcul()
{
	if (m_Nr1Apasat && m_Nr2Apasat)
	{
		CString rezultat;
		switch(operatie)
		{
		case ADUNARE:
			rezultat.Format(" %d + %d = %d ",m_Nr1,m_Nr2,m_Nr1+m_Nr2);
			MessageBox(rezultat,"ADUNARE",MB_ICONEXCLAMATION);
			break;
		case SCADERE:
			rezultat.Format(" %d - %d = %d ",m_Nr1,m_Nr2,m_Nr1-m_Nr2);
			MessageBox(rezultat,"SCADERE",MB_ICONEXCLAMATION);
			break;
		case INMULTIRE:
			rezultat.Format(" %d * %d = %d ",m_Nr1,m_Nr2,m_Nr1*m_Nr2);
			MessageBox(rezultat,"INMULTIRE",MB_ICONEXCLAMATION);
			break;
		case IMPARTIRE:
			double rez;
			if (m_Nr2!=0) 
			{
				rez=(double)m_Nr1/(double)m_Nr2;
				rezultat.Format(" %d / %d = %f ",m_Nr1,m_Nr2,rez);
			}
			else rezultat+="Impartire cu 0!";
			MessageBox(rezultat,"IMPARTIRE",MB_ICONEXCLAMATION);
		}
		m_Nr1Apasat=m_Nr2Apasat=0;
	}
	else
		if (m_Nr1Apasat)
			MessageBox("Nu a fost apasat Nr.2",
		       "ERROR",MB_ICONSTOP);
		else
			if (m_Nr2Apasat)
				MessageBox("Nu a fost apasat Nr.1",
					"ERROR",MB_ICONSTOP);
			else
				MessageBox("Nu au fost apasate Nr.1 si Nr.2",
					"ERROR",MB_ICONSTOP);	
}


