⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mfcthirddlg.cpp

📁 一个计算器程序
💻 CPP
字号:
// MFCThirdDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFCThird.h"
#include "MFCThirdDlg.h"
#include "Calculator.h"
#include "stdlib.h"
#include "conio.h"
#include "stdio.h"

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

CCalculator ob;
//char ob.string[100];

// 这一段是记号的定义
#define ADD 0
#define MUL 1
#define LBRACE 2
#define RBRACE 3
#define NUM 4
#define END 5
#define OTHER 6
#define SUB 7
#define DIV 8


//char ob.string[200];	// 输入串。
int lookahead;
int pCur;
int yylval;

/*
// 函数声明
int yylex();
void Match(int t);
long T();
long E_(int *AFlag);
long E();
long T_();
long F();
long T_(int *AFlag);

*/
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMFCThirdDlg dialog

CMFCThirdDlg::CMFCThirdDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMFCThirdDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMFCThirdDlg)
		// 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(IDI_MyICON);

}

void CMFCThirdDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMFCThirdDlg)
	DDX_Control(pDX, IDC_SUB_BUTTON, m_SUBButton);
	DDX_Control(pDX, IDC_DIV_BUTTON, m_DIVButton);
	DDX_Control(pDX, IDC_Exit_BUTTON, m_ExitButton);
	DDX_Control(pDX, IDC_backspace_BUTTON, m_BPButton);
	DDX_Control(pDX, IDC_EQUAL_BUTTON, m_EqualButton);
	DDX_Control(pDX, IDC_Clear_BUTTON, m_ClearButton);
	DDX_Control(pDX, IDC_EDIT, m_Edit);
	DDX_Control(pDX, IDC_0_BUTTON, m_Button0);
	DDX_Control(pDX, IDC_RIGHTBRACKET_BUTTON, m_ButtonRB);
	DDX_Control(pDX, IDC_MUL_BUTTON, m_ButtonMUL);
	DDX_Control(pDX, IDC_LEFTBRACKET_BUTTON, m_ButtonLB);
	DDX_Control(pDX, IDC_ADD_BUTTON, m_ButtonAdd);
	DDX_Control(pDX, IDC_9_BUTTON, m_Button9);
	DDX_Control(pDX, IDC_8_BUTTON, m_Button8);
	DDX_Control(pDX, IDC_7_BUTTON, m_Button7);
	DDX_Control(pDX, IDC_6_BUTTON, m_Button6);
	DDX_Control(pDX, IDC_5_BUTTON, m_Button5);
	DDX_Control(pDX, IDC_4_BUTTON, m_Button4);
	DDX_Control(pDX, IDC_3_BUTTON, m_Button3);
	DDX_Control(pDX, IDC_2_BUTTON, m_Button2);
	DDX_Control(pDX, IDC_1_BUTTON, m_Button1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMFCThirdDlg, CDialog)
	//{{AFX_MSG_MAP(CMFCThirdDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_1_BUTTON, On1Button)
	ON_BN_CLICKED(IDC_0_BUTTON, On0Button)
	ON_BN_CLICKED(IDC_2_BUTTON, On2Button)
	ON_BN_CLICKED(IDC_3_BUTTON, On3Button)
	ON_BN_CLICKED(IDC_4_BUTTON, On4Button)
	ON_BN_CLICKED(IDC_5_BUTTON, On5Button)
	ON_BN_CLICKED(IDC_6_BUTTON, On6Button)
	ON_BN_CLICKED(IDC_7_BUTTON, On7Button)
	ON_BN_CLICKED(IDC_8_BUTTON, On8Button)
	ON_BN_CLICKED(IDC_9_BUTTON, On9Button)
	ON_BN_CLICKED(IDC_ADD_BUTTON, OnAddButton)
	ON_BN_CLICKED(IDC_LEFTBRACKET_BUTTON, OnLeftbracketButton)
	ON_BN_CLICKED(IDC_MUL_BUTTON, OnMulButton)
	ON_BN_CLICKED(IDC_RIGHTBRACKET_BUTTON, OnRightbracketButton)
	ON_BN_CLICKED(IDC_Clear_BUTTON, OnClearBUTTON)
	ON_BN_CLICKED(IDC_EQUAL_BUTTON, OnEqualButton)
	ON_BN_CLICKED(IDC_Exit_BUTTON, OnExitBUTTON)
	ON_BN_CLICKED(IDC_backspace_BUTTON, OnbackspaceBUTTON)
	ON_BN_CLICKED(IDC_SUB_BUTTON, OnSubButton)
	ON_BN_CLICKED(IDC_DIV_BUTTON, OnDivButton)
	ON_BN_CLICKED(IDC_Help_BUTTON, OnHelpBUTTON)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMFCThirdDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMFCThirdDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CMFCThirdDlg::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 CMFCThirdDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMFCThirdDlg::On0Button() 
{
	// TODO: Add your control notification handler code here
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel("");
	strcat(ob.string,"0");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}


void CMFCThirdDlg::On1Button() 
{
	// TODO: Add your control notification handler code here
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel("");

	strcat(ob.string,"1");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}


void CMFCThirdDlg::On2Button() 
{
	// TODO: Add your control notification handler code here
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel("");
	strcat(ob.string,"2");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}

void CMFCThirdDlg::On3Button() 
{
	// TODO: Add your control notification handler code here
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel("");
	strcat(ob.string,"3");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}

void CMFCThirdDlg::On4Button() 
{
	// TODO: Add your control notification handler code here
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel("");
	strcat(ob.string,"4");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}

void CMFCThirdDlg::On5Button() 
{
	// TODO: Add your control notification handler code here
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel("");
	strcat(ob.string,"5");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}

void CMFCThirdDlg::On6Button() 
{
	// TODO: Add your control notification handler code here
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel("");
	strcat(ob.string,"6");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}

void CMFCThirdDlg::On7Button() 
{
	// TODO: Add your control notification handler code here
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel("");
	strcat(ob.string,"7");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}

void CMFCThirdDlg::On8Button() 
{
	// TODO: Add your control notification handler code here
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel("");
	strcat(ob.string,"8");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}

void CMFCThirdDlg::On9Button() 
{
	// TODO: Add your control notification handler code here
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel("");
	strcat(ob.string,"9");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}

void CMFCThirdDlg::OnSubButton() 
{
	// TODO: Add your control notification handler code here
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel("");
	strcat(ob.string,"-");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}

void CMFCThirdDlg::OnDivButton() 
{
	// TODO: Add your control notification handler code here
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel("");
	strcat(ob.string,"/");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}

void CMFCThirdDlg::OnAddButton() 
{
	// TODO: Add your control notification handler code here
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel("");
	strcat(ob.string,"+");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}

void CMFCThirdDlg::OnLeftbracketButton() 
{
	// TODO: Add your control notification handler code here
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel("");
	strcat(ob.string,"(");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}

void CMFCThirdDlg::OnMulButton() 
{
	// TODO: Add your control notification handler code here
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel("");
	strcat(ob.string,"*");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}

void CMFCThirdDlg::OnRightbracketButton() 
{
	// TODO: Add your control notification handler code here
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel("");
	strcat(ob.string,")");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}

void CMFCThirdDlg::OnClearBUTTON() 
{
	// TODO: Add your control notification handler code here
	strcpy(ob.string,"");
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}

void CMFCThirdDlg::OnEqualButton() 
{
	// TODO: Add your control notification handler code here
	ob.pCur=0;
	ob.lookahead = ob.yylex();
	long number=ob.E();    
    sprintf(ob.string,"%d",number);
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
    strcpy(ob.string,"");	
}

void CMFCThirdDlg::OnExitBUTTON() 
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void CMFCThirdDlg::OnbackspaceBUTTON() 
{
	// TODO: Add your control notification handler code here
    char temp[200];
    for(int i=0;i<(signed)strlen(ob.string)-1;i++)
    temp[i]=ob.string[i];
    temp[i]='\0';
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(" ");
	strcpy(ob.string,temp);
	m_Edit.SetSel(0,-1);
	m_Edit.ReplaceSel(ob.string);
}


void CMFCThirdDlg::OnHelpBUTTON() 
{
	// TODO: Add your control notification handler code here
	MessageBox("本计算器可以用来做整型数的加减乘除运算!!","帮助",MB_OK);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -