- 博客(0)
- 资源 (11)
- 收藏
- 关注
C++编程实例详解---C++入门(Jesse Lberty著)
C++编程实例详解---C++入门(Jesse Lberty著)
建立一个逻辑游戏,即Dcryptix.
通过编程实例来给初学者讲解。
2009-07-27
Visual C++课程设计案例精编(第二版)--第十一章 画图软件的开发
//ChildFrm.cpp
// ChildFrm.cpp : implementation of the CChildFrame class
//
#include "stdafx.h"
#include "Painter.h"
#include "ChildFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CChildFrame
IMPLEMENT_DYNCREATE(CChildFrame, CMDIChildWnd)
BEGIN_MESSAGE_MAP(CChildFrame, CMDIChildWnd)
//{{AFX_MSG_MAP(CChildFrame)
ON_WM_CLOSE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChildFrame construction/destruction
CChildFrame::CChildFrame()
{
// TODO: add member initialization code here
}
CChildFrame::~CChildFrame()
{
}
BOOL CChildFrame::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style = WS_CHILD | WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU
| FWS_ADDTOTITLE | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_MAXIMIZE;
if( !CMDIChildWnd::PreCreateWindow(cs) )
return FALSE;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CChildFrame diagnostics
#ifdef _DEBUG
void CChildFrame::AssertValid() const
{
CMDIChildWnd::AssertValid();
}
void CChildFrame::Dump(CDumpContext& dc) const
{
CMDIChildWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CChildFrame message handlers
void CChildFrame::OnClose()
{
// TODO: Add your message handler code here and/or call default
CMDIChildWnd::OnClose();
}
2009-07-27
Visual C++课程设计案例精编(第二版)--第十章 24点游戏软件的开发
//Express.cpp
// Function.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "math.h"
#include "express.h"
#include <ctype.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
IMPLEMENT_SERIAL(CExpression, CObject, 0)
//////////////////////////////////////////////////////////////////////
// CExpression Class
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Variabile Globale
//parametrii Global(50);
/////////////////////////////////////////////////////////////////////////////////
// Eliberarea memorie ocupate de arbore
void CExpression::elibmem ( arbore a)
{
if (a==NULL) return;
if (a->left!=NULL) elibmem(a->left);
if (a->right!=NULL) elibmem(a->right);
if (a->operatie == '`')
delete a->valoarestr;
delete a;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CExpression::CExpression()
{
m_Arbore = NULL;
m_definitie = "";
pozitie = 0;
m_pvariabile = NULL;
}
CExpression::CExpression(CMapVariabile * vars)
{
m_Arbore = NULL;
m_definitie = "";
pozitie = 0;
m_pvariabile = vars;
}
CExpression::~CExpression()
{
elibmem (m_Arbore);
}
void CExpression::Serialize(CArchive & ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
ar << m_definitie;
}
else
{
// TODO: add loading code here
ar >> m_definitie;
m_Arbore = NULL;
pozitie = 0;
UpdateArbore();
// After loading this object if it contains variables you should atach a variable
// Table to it
}
}
int CExpression::UpdateArbore()
{
if (m_definitie.IsEmpty())
return 0;
elibmem(m_Arbore); // Eliberarea memoriei ocupate de arbore
m_Arbore = NULL;
pozitie = 0;
m_Arbore = expresie();
if (m_definitie[pozitie]!='\0')
{
elibmem(m_Arbore);
m_Arbore = NULL;
}
if (m_Arbore == NULL)
return pozitie;
else return -1;
}
CExpression::arbore CExpression::expresie()
{
arbore nod;
arbore arb1 = termen();
arbore arb2;
if (arb1 == NULL) return NULL; // In caz de eroare terminate
while ((m_definitie[pozitie]=='-') || (m_definitie[pozitie]=='+'))
{
nod=new NOD;
nod->left=arb1;
nod->operatie=m_definitie[pozitie];
pozitie++;
arb2 = termen();
nod->right=arb2;
if (arb2 == NULL)
{
elibmem(nod);
return NULL; // In caz de eroare terminate
}
arb1 = nod;
}
return arb1;
}
CExpression::arbore CExpression::termen()
{
arbore nod;
arbore arb1 = putere();
arbore arb2;
if (arb1 == NULL) return NULL; // In caz de eroare terminate
while ((m_definitie[pozitie]=='*') || (m_definitie[pozitie]=='/'))
{
nod=new NOD;
nod->left=arb1;
nod->operatie=m_definitie[pozitie];
pozitie++;
arb2 = putere();
nod->right=arb2;
if (arb2 == NULL)
{
elibmem(nod);
return NULL; // In caz de eroare terminate
}
arb1 = nod;
}
return arb1;
}
CExpression::arbore CExpression::putere()
{
arbore nod = NULL;
arbore arb1 = logicalOp();
arbore arb2;
if (arb1 == NULL) return NULL; // In caz de eroare terminate
while (m_definitie[pozitie]=='^')
{
nod=new NOD;
nod->left=arb1;
nod->operatie=m_definitie[pozitie];
pozitie++;
arb2 = logicalOp();
nod->right=arb2;
if (arb2 == NULL)
{
elibmem(nod);
return NULL; // In caz de eroare terminate
}
arb1 = nod;
}
return arb1;
}
CExpression::arbore CExpression::factor()
{
arbore nod = NULL,nod2 = NULL,left = NULL;
while(m_definitie[pozitie] == ' ' && m_definitie[pozitie] != '\0')
pozitie++;
if (m_definitie[pozitie]=='-')
{
nod = new NOD;
left = new NOD;
left->right=NULL;
left->left=NULL;
left->operatie='@';
left->valoare=-1;
nod->left=left;
nod->operatie='*';
pozitie++;
nod->right = expresie();
if (nod->right == NULL) return NULL;
return nod;
}
if (m_definitie[pozitie]=='(')
{
pozitie++;
nod = expresie();
if (nod == NULL) return NULL;
if (m_definitie[pozitie]!=')')
{
elibmem(nod);
return NULL;
}
pozitie++;
return nod;
}
else if (m_definitie[pozitie]=='|')
{
pozitie++;
nod2 = expresie();
if (nod2 == NULL) return NULL;
if (m_definitie[pozitie]!='|')
{
elibmem(nod);
return NULL;
}
nod = new NOD;
nod->left=nod2;
nod->right=NULL;
nod->operatie='|';
pozitie++;
return nod;
}
else return identificator();
return nod;
}
CExpression::arbore CExpression::identificator()
{
int poz;
arbore nod = NULL,nod2 = NULL;
arbore result = NULL;
poz=pozitie;
SkipSpaces();
if (m_definitie[pozitie]=='\0') result = NULL;
if (isdigit(m_definitie[pozitie])) // Este numar ?
{
while ((isdigit(m_definitie[pozitie]) || (m_definitie[pozitie]=='.')))
pozitie++;
nod = new NOD;
nod->left = NULL;
nod->right = NULL;
nod->operatie = '@';
nod->valoare = atof(m_definitie.Mid(poz,pozitie-poz));
result = nod;
}
if (isalpha(m_definitie[pozitie])) // Am i an identifier ?
{
while (isalnum(m_definitie[pozitie]))
pozitie++;
CString id = m_definitie.Mid(poz,pozitie-poz);
CString nid = id;
id.MakeUpper();
if (id =="SIN") // Functia sinus CString
{
nod2 = factor();
nod = new NOD;
nod->left = nod2;
nod->right=NULL;
nod->operatie=150;
SkipSpaces();
return nod;
}
if (id =="COS") // Functia sinus CString
{
nod2 = factor();
nod = new NOD;
nod->left = nod2;
nod->right=NULL;
nod->operatie=151;
SkipSpaces();
return nod;
}
if (id =="EXP") // Functia sinus CString
{
nod2 = factor();
nod = new NOD;
nod->left = nod2;
nod->right=NULL;
nod->operatie=152;
SkipSpaces();
return nod;
}
if (id =="SQRT") // Functia sinus CString
{
nod2 = factor();
nod = new NOD;
nod->left = nod2;
nod->right=NULL;
nod->operatie=153;
SkipSpaces();
return nod;
}
if (id =="LOG") // Functia sinus CString
{
nod2 = factor();
nod = new NOD;
nod->left = nod2;
nod->right=NULL;
nod->operatie=154;
SkipSpaces();
return nod;
}
if (id =="TG") // Functia sinus CString
{
nod2 = factor();
nod = new NOD;
nod->left = nod2;
nod->right=NULL;
nod->operatie=155;
SkipSpaces();
return nod;
}
if (id =="CTG") // Functia sinus CString
{
nod2 = factor();
nod = new NOD;
nod->left = nod2;
nod->right=NULL;
nod->operatie=156;
SkipSpaces();
return nod;
}
if (id =="ASIN") // Functia sinus CString
{
nod2 = factor();
nod = new NOD;
nod->left = nod2;
nod->right=NULL;
nod->operatie=157;
SkipSpaces();
return nod;
}
if (id =="ACOS") // Functia sinus CString
{
nod2 = factor();
nod = new NOD;
nod->left = nod2;
nod->right=NULL;
nod->operatie=158;
SkipSpaces();
return nod;
}
if (id =="ATG") // Functia sinus CString
{
nod2 = factor();
nod = new NOD;
nod->left = nod2;
nod->right=NULL;
nod->operatie=159;
SkipSpaces();
return nod;
}
CValue *valoare;
if (m_pvariabile->Lookup(nid,valoare))
{
nod = new NOD;
nod -> left = NULL;
nod -> right = NULL;
nod -> operatie = '`';
nod -> valoarestr = new CString(nid);
result = nod;
}
else
result = NULL;
}
SkipSpaces();
return result;
}
int CExpression::ChangeExpression(CString & expresie)
{
m_definitie = expresie + '\0' + '\0';
return UpdateArbore();
}
// Error code var
int code;
int CExpression::Value(double& valoare)
{
code=0;
if (m_Arbore == NULL) return -1;
valoare=vexp(m_Arbore);
return (code);
}
double CExpression::vexp ( arbore a )
{
double v;
if (a->operatie==NULL) {code=10;return 0;}
switch(a->operatie)
{
case '+' : return( vexp(a->left)+vexp(a->right) );
case '-' : return( vexp(a->left)-vexp(a->right) );
case '*' : return( vexp(a->left)*vexp(a->right) );
case '/' : v=vexp(a->right) ;
if (v==0)
{code=DIVISION_BY_0;return -vexp(a->left)/0.001;}
else
return(vexp(a->left)/v);
case 150 : return(sin(vexp(a->left)));
case 151 : return(cos(vexp(a->left)));
case 152 : return(exp(vexp(a->left)));
case 153 : v=vexp(a->left) ;
if (v<0) {code=INVALID_DOMAIN;return 0;}
else return(sqrt(v));
case 154 : v=vexp(a->left) ;
if (v<=0) {code=INVALID_DOMAIN;return 0;}
else return(log(v));
case 155 : return (tan (vexp(a->left)));
case 156 : return (1 / tan (vexp(a->left)));
case 157 : return (asin (vexp(a->left)));
case 158 : return (acos (vexp(a->left)));
case 159 : return (atan (vexp(a->left)));
case '|' : return(fabs(vexp(a->left)));
case '^' : return(pow(vexp(a->left),vexp(a->right)));
case '@' : return (a->valoare);
//logical operations evaluation
case '<' : return( vexp(a->left)<vexp(a->right) );
case '>' : return( vexp(a->left)>vexp(a->right) );
case '!' : return(!vexp(a->right)) ;
default : {
if (m_pvariabile==NULL)
{
code=UNDEFINED_VARIABLE;
return 0;
}
CValue *valoare;
if (!m_pvariabile->Lookup(*a->valoarestr,valoare))
{
code=UNDEFINED_VARIABLE;
return 0;
}
else
return valoare->GetValue();
}
}
}
CExpression::arbore CExpression::GetArbore()
{
return m_Arbore;
}
CExpression::CExpression(CExpression & expresie)
{
*this = expresie;
}
CExpression::arbore CExpression::CloneTree()
{
return clone(m_Arbore);
}
void CExpression::AtachVariables(CMapVariabile * vars)
{
m_pvariabile = vars;
}
CExpression::arbore CExpression::clone(arbore arb)
{
if (arb == NULL)
return NULL;
arbore clonArb = new NOD;
*clonArb = *arb;
clonArb->left = clone(arb->left);
clonArb->right = clone(arb->right);
return clonArb;
}
CExpression& CExpression::operator=(CExpression &expr)
{
m_definitie = expr.m_definitie;
m_pvariabile = expr.m_pvariabile;
pozitie = 0;
m_Arbore = expr.CloneTree();
return *this;
}
void CExpression::SkipSpaces()
{
while (m_definitie[pozitie]==' ' && m_definitie[pozitie]!='\0')
pozitie ++;
}
///////////////////////////////////////////////////////////////////////
// the new inserted operations
// logical operations on the first level of priority
// warning: for operations with more than one character you nedd to modify sligthly the code
// See also the new inserted operations in the vexp function
CExpression::arbore CExpression::logicalOp()
{
arbore nod;
arbore arb1 = sgOp();
arbore arb2;
if (arb1 == NULL) return NULL; // In caz de eroare terminate
while ((m_definitie[pozitie]=='<') || (m_definitie[pozitie]=='>') /* || another same priority operations*/)
{
nod=new NOD;
nod->left=arb1;
nod->operatie=m_definitie[pozitie];
pozitie++;
arb2 = sgOp();
nod->right=arb2;
if (arb2 == NULL)
{
elibmem(nod);
return NULL; // In caz de eroare terminate
}
arb1 = nod;
}
return arb1;
}
CExpression::arbore CExpression::sgOp()
{
arbore nod = NULL;
arbore arb2;
if ((m_definitie[pozitie]=='!') /* || another same priority operations*/)
{
nod=new NOD;
nod->left=NULL;
nod->operatie=m_definitie[pozitie];
pozitie++;
arb2 = sgOp();
nod->right=arb2;
if (arb2 == NULL)
{
elibmem(nod);
return NULL; // In caz de eroare terminate
}
}
else
nod = factor();
return nod;
}
2009-07-27
Visual C++课程设计案例精编(第二版)--第六章 电子邮件发送和接受程序的开发
//MailServer.h
// MailServer.h : main header file for the MAILSERVER application
//
#if !defined(AFX_MAILSERVER_H__CB181B60_A7D0_4663_B1F4_A97F7F2B9BAB__INCLUDED_)
#define AFX_MAILSERVER_H__CB181B60_A7D0_4663_B1F4_A97F7F2B9BAB__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif
#include "resource.h" // main symbols
/////////////////////////////////////////////////////////////////////////////
// CMailServerApp:
// See MailServer.cpp for the implementation of this class
//
class CMailServerApp : public CWinApp
{
public:
CMailServerApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMailServerApp)
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
//}}AFX_VIRTUAL
// Implementation
//{{AFX_MSG(CMailServerApp)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MAILSERVER_H__CB181B60_A7D0_4663_B1F4_A97F7F2B9BAB__INCLUDED_)
2009-07-27
Visual C++课程设计案例精编(第二版)--第七章 聊天室软件的开发
//ChannelDlg.cpp
// ChannelDlg.cpp : implementation file
//
#include "stdafx.h"
#include "E04.h"
#include "ChannelDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CChannelDlg dialog
CChannelDlg::CChannelDlg(CWnd* pParent /*=NULL*/)
: CDialog(CChannelDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CChannelDlg)
m_channel = _T("1");
//}}AFX_DATA_INIT
}
void CChannelDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CChannelDlg)
DDX_Text(pDX, IDC_EDIT_CHANNEL, m_channel);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CChannelDlg, CDialog)
//{{AFX_MSG_MAP(CChannelDlg)
// NOTE: the ClassWizard will add message map macros here
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CChannelDlg message handlers
2009-07-27
Visual C++课程设计案例精编(第二版) --第三章 系统进程管理器的开发
//showallprocess.h
// showallprocess.h : main header file for the SHOWALLPROCESS application
//
#if !defined(AFX_SHOWALLPROCESS_H__928B7E87_BDF8_4C4F_84D3_AF4D921C3CF8__INCLUDED_)
#define AFX_SHOWALLPROCESS_H__928B7E87_BDF8_4C4F_84D3_AF4D921C3CF8__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#ifndef __AFXWIN_H__
#error include 'stdafx.h' before including this file for PCH
#endif
#include "resource.h" // main symbols
/////////////////////////////////////////////////////////////////////////////
// CShowallprocessApp:
// See showallprocess.cpp for the implementation of this class
class CShowallprocessApp : public CWinApp
{
public:
CShowallprocessApp();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CShowallprocessApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL
// Implementation
//{{AFX_MSG(CShowallprocessApp)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_SHOWALLPROCESS_H__928B7E87_BDF8_4C4F_84D3_AF4D921C3CF8__INCLUDED_)
2009-07-27
Visual C++课程设计案例精编(第二版)--第二章 屏幕保护程序的开发
// B14.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "B14.h"
#include "B14Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CB14App
BEGIN_MESSAGE_MAP(CB14App, CWinApp)
//{{AFX_MSG_MAP(CB14App)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CB14App construction
CB14App::CB14App()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CB14App object
CB14App theApp;
/////////////////////////////////////////////////////////////////////////////
// CB14App initialization
BOOL CB14App::InitInstance()
{
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
CB14Dlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
2009-07-27
Visual C++课程设计案例精编(第二版) --第一章 系统信息程序的开发
系统信息程序的开发片段:
// MainFrm.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "sysinfo.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CRgn rgn;
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
int SCR_Width = 350,SCR_Height = 140;
int Org_xPos = 20,Org_yPos = 0;
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code !
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
cs.dwExStyle = WS_EX_TOOLWINDOW | WS_EX_TOPMOST;//|WS_EX_TOPMOST;
cs.style=WS_POPUP;
cs.style &= ~WS_CAPTION;
cs.x = Org_xPos;
cs.y = Org_yPos;
cs.cx = SCR_Width;
cs.cy = SCR_Height;
cs.hMenu = NULL;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
2009-07-27
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人