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

📄 s_caldlg.cpp

📁 使用实际的编译算法的计算器 包括词法、文法分析过程
💻 CPP
字号:
// s_calDlg.cpp : implementation file
//

#include "stdafx.h"
#include "s_cal.h"
#include "s_calDlg.h"
#include "parser.h"

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

/////////////////////////////////////////////////////////////////////////////
// CS_calDlg dialog

CS_calDlg::CS_calDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CS_calDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CS_calDlg)
		// 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 CS_calDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CS_calDlg)
	DDX_Control(pDX, IDC_DSP, m_dsp);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CS_calDlg, CDialog)
	//{{AFX_MSG_MAP(CS_calDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_1, On1)
	ON_BN_CLICKED(IDC_2, On2)
	ON_BN_CLICKED(IDC_3, On3)
	ON_BN_CLICKED(IDC_4, On4)
	ON_BN_CLICKED(IDC_5, On5)
	ON_BN_CLICKED(IDC_6, On6)
	ON_BN_CLICKED(IDC_7, On7)
	ON_BN_CLICKED(IDC_8, On8)
	ON_BN_CLICKED(IDC_9, On9)
	ON_BN_CLICKED(IDC_0, On0)
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_MINUS, OnMinus)
	ON_BN_CLICKED(IDC_MUL, OnMul)
	ON_BN_CLICKED(IDC_DIV, OnDiv)
	ON_BN_CLICKED(IDC_CE, OnCe)
	ON_BN_CLICKED(IDC_EQ, OnEq)
	ON_BN_CLICKED(IDC_LP, OnLp)
	ON_BN_CLICKED(IDC_RP, OnRp)
	ON_BN_CLICKED(IDC_DOT, OnDot)
	ON_BN_CLICKED(IDC_BS, OnBs)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CS_calDlg message handlers

BOOL CS_calDlg::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
	
	flag=false;     // false when the EQ button hasn't been pushed ,else true
	
	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 CS_calDlg::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 CS_calDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CS_calDlg::On1() 
{
	if(flag)input.Empty();
	flag=false;
	input+="1";
	m_dsp.SetWindowText(input);
}

void CS_calDlg::On2() 
{
    if(flag)input.Empty();
	flag=false;
	input+="2";
	m_dsp.SetWindowText(input);
	
}

void CS_calDlg::On3() 
{
	if(flag)input.Empty();
	flag=false;
	input+="3";
	m_dsp.SetWindowText(input);	
}

void CS_calDlg::On4() 
{
	if(flag)input.Empty();
	flag=false;
	input+="4";
	m_dsp.SetWindowText(input);		
}

void CS_calDlg::On5() 
{
	if(flag)input.Empty();
	flag=false;
	input+="5";
	m_dsp.SetWindowText(input);		
}

void CS_calDlg::On6() 
{
	if(flag)input.Empty();
	flag=false;
	input+="6";
	m_dsp.SetWindowText(input);		
}

void CS_calDlg::On7() 
{
	if(flag)input.Empty();
	flag=false;
	input+="7";
	m_dsp.SetWindowText(input);		
}

void CS_calDlg::On8() 
{
	if(flag)input.Empty();
	flag=false;
	input+="8";
	m_dsp.SetWindowText(input);		
}

void CS_calDlg::On9() 
{
	if(flag)input.Empty();
	flag=false;
	input+="9";
	m_dsp.SetWindowText(input);		
}

void CS_calDlg::On0() 
{
	if(flag)input.Empty();
	flag=false;
	input+="0";
	m_dsp.SetWindowText(input);		
}

void CS_calDlg::OnAdd() 
{
	if(flag)input.Empty();
	flag=false;
	input+="+";
	m_dsp.SetWindowText(input);		
}

void CS_calDlg::OnMinus() 
{
	if(flag)input.Empty();
	flag=false;
	input+="-";
	m_dsp.SetWindowText(input);		
}

void CS_calDlg::OnMul() 
{
	if(flag)input.Empty();
	flag=false;
	input+="*";
	m_dsp.SetWindowText(input);		
}

void CS_calDlg::OnDiv() 
{
	if(flag)input.Empty();
	flag=false;
	input+="/";
	m_dsp.SetWindowText(input);		
}

void CS_calDlg::OnLp() 
{
	if(flag)input.Empty();
	flag=false;
	input+="(";
	m_dsp.SetWindowText(input);	
	
}

void CS_calDlg::OnRp() 
{
	if(flag)input.Empty();
	flag=false;
    input+=")";
	m_dsp.SetWindowText(input);		
}

void CS_calDlg::OnDot() 
{
	if(flag)input.Empty();
	flag=false;
	input+=".";
	m_dsp.SetWindowText(input);	
	
}

void CS_calDlg::OnBs() 
{
	if(flag)input.Empty();
	flag=false;
	int i=input.GetLength()-1;
	if(i>=0)input.Delete(i);
	m_dsp.SetWindowText(input);
}


void CS_calDlg::OnCe() 
{
	flag=false;
	input.Empty();
	m_dsp.SetWindowText("");
}

void CS_calDlg::OnEq() 
{
	using std::string;
	using namespace my_space;
	flag=true;
	CString re;
	string in=input+'$';
	Scanner calsc(in);
	if(calsc.Scan()){
		Parser calpa(calsc);
		PSTATE a=calpa.Parse();
		if(a==SUCCESS){
			re.Format("=%f",calpa.GetRe());
			input+=re;
		}
		else
			switch(a){
			    case MAERROR: input="MA ERROR!";break;
			    case DBZ:    input="DIV BY ZERO!";break;
			}
			
	}
	else input="ILLEGAL INPUT!";

	m_dsp.SetWindowText(input);
}

⌨️ 快捷键说明

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