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

📄 简单lisp算术表达式计算器dlg.cpp

📁 MFC 本程序是一个简单的LISP算术表达式,用于实现加法
💻 CPP
字号:
// 简单LISP算术表达式计算器Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "简单LISP算术表达式计算器.h"
#include "简单LISP算术表达式计算器Dlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CLISPDlg dialog

CLISPDlg::CLISPDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLISPDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLISPDlg)
	m_Edit1 = _T("");
	m_Int1 = 0.0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLISPDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLISPDlg)
	DDX_Control(pDX, IDC_COMBOBOXEX1, m_CCombo);
	DDX_Text(pDX, IDC_EDIT1, m_Edit1);
	DDX_Text(pDX, IDC_EDIT2, m_Int1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLISPDlg, CDialog)
	//{{AFX_MSG_MAP(CLISPDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_EDITCHANGE(IDC_COMBOBOXEX1, OnEditchangeComboboxex1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLISPDlg message handlers

BOOL CLISPDlg::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 CLISPDlg::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 CLISPDlg::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 CLISPDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
int x=-1;
struct Node{
	char ch;
	Node *right;
	Node *left;
};


void CLISPDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	CDialog::OnCancel();
}

void CLISPDlg::OnEditchangeComboboxex1() 
{
	INT index = m_CCombo.GetCurSel();
	x = index;
	// TODO: Add your control notification handler code here
	
}

double  print(Node  *root) 
{ 
	if( root ->right == NULL || root->left == NULL)
		return (root->ch-'0'+0.0);
    switch(root->ch) 
    {
	    case'+': return (print(root->left) + print(root->right));
        case'-': return (print(root->left) - print(root->right));
	    case'*': return (print(root->left) * print(root->right)); 
	    case'/': return (print(root->left) / print(root->right));
    } 
} 
void Inorder_Creat(Node **t,char a[])
{
	if( strlen(a) == 1)
	{
		(*t) = new Node;
		(*t)->left = NULL;
		(*t)->right = NULL;
		(*t)->ch = a[0];
		return;
	}
	int x = 0,i,j,j1 = -1 ,j2 = -1;
	char *b,*c;
	b = a;
	for(i= 0; i < (j = strlen(a)); i++)
	{
		if( x == 0&&(a[i] == '-' || a[i] == '+'))
		{
			j1 = i;			
		}
		if(x == 0&& (a[i] == '*' || a[i] =='/'))
		{
			j2 = i;
		}
		if( a[i] == '(')
		{
			x++;
			continue;
		}
		if( a[i] == ')' )
		{
			x--;
			continue;
		}
	}
	i = j2;
	if( j1 != -1)
       i = j1;
	(*t)= new Node;
	(*t)->ch = a[i];
	(*t)->left = NULL;
	(*t)->right = NULL;
	a[i] = '\0';
	if( a[0] == '(' && a[i-1] == ')')
	{
		b = a +1;
		a[i-1] = '\0';
	}
	c = a+i+1;
	if( c[0] == '(' && a[j-1] == ')')
	{
		c++;
		a[j-1] = '\0';
	}
	Inorder_Creat(&((*t)->left),b);
	Inorder_Creat(&((*t)->right),c);
	return;
}
void Preorder_Creat(Node **t,char aa[])
{
	char *b,*c;
	char a[40];
	int x = 0,i,j;
	j = strlen(aa);
	if( aa[0] == '(' && aa[strlen(aa)-1] == ')')
	{		
		b = aa+1;
		aa[j-1]= '\0';
		j--;
		strcpy(a,b);
	}
	else
		strcpy(a,aa);
	(*t)= new Node;
	(*t)->left = NULL;
	(*t)->right = NULL;
	(*t)->ch = a[0];
	if( strlen(a) == 1)
	    return; 
	b = a+1;
	if( a[1] != '(')
	{
		char tem[2];
		tem[0] = a[1];
		tem[1] = '\0';
		Preorder_Creat(&((*t)->left),tem);
		c = a+2;
	    if( a[2] ==  '(' && a[j-1] == ')')
		{
			c = a+3;
			a[j-1] = '\0';
		}
		Preorder_Creat(&((*t)->right),c);
		return;
	}

	if( a[1] == '(' )
	{
		for( i = 1; i < j; i++)
		{			
			if( a[i] == '(')
			{
			    x++;
			    continue;
			}
		    if( a[i] == ')' )
			{
		       x--;
			}
			if( x == 0 )
			{
				c = a+i+1;
				if( c[0] == '(' && a[j-1] == ')')
				{
					c++;
					a[j-1] ='\0';
				}
				if( b[0] == '(' && a[i] == ')')
				{
					b++;
					a[i] = '\0';
				}
			    Preorder_Creat(&((*t)->left),b);
			    Preorder_Creat(&((*t)->right),c);
				return;
			}
		}
	}
}

void Aftorder_Creat(Node **t,char aa[])
{
	char *b,*c;
	char a[40];
	int x = 0,i,j;
	j = strlen(aa);
	if( aa[0] == '(' && aa[strlen(aa)-1] == ')')
	{		
		b = aa+1;
		aa[j-1]= '\0';
		j-=2;
		strcpy(a,b);
	}
	else
		strcpy(a,aa);
	(*t)= new Node;
	(*t)->left = NULL;
	(*t)->right = NULL;
	(*t)->ch = a[j-1];
	if( strlen(a) == 1)
	    return; 
	b = a;
	if( a[0] != '(')
	{
		char tem[2];
		tem[0] = a[0];
		tem[1] = '\0';
		Preorder_Creat(&((*t)->left),tem);
		c = a+1;
	    c[j-2] = '\0';
		Preorder_Creat(&((*t)->right),c);
		return;
	}
	if( a[0] == '(' )
	{
		for( i = 0; i < j; i++)
		{			
			if( a[i] == '(')
			{
			    x++;
			    continue;
			}
		    if( a[i] == ')' )
			{
		       x--;
			}
			if( x == 0 )
			{
				c = a+i+1;
				a[j-1] = '\0';
				if( b[0] == '(' && a[i] == ')')
				{
					b++;
					a[i] = '\0';
				}
			    Aftorder_Creat(&((*t)->left),b);
			    Aftorder_Creat(&((*t)->right),c);
				return;
			}
		}
	}
}
void CLISPDlg::OnOK() 
{
	if( x == -1)
	{
		MessageBox("请先选者表达式形式");
		return;
	}
	m_Int1 = 0;
	UpdateData(true);
	char *a;
	char b[40];
	a = m_Edit1.GetBuffer(0);  
	strcpy(b,a);
    Node *head;
	switch(x)
	{
	    case 0: Preorder_Creat(&head,a);break;
		case 1: Inorder_Creat(&head,a);break;
        case 2: Aftorder_Creat(&head,a);
	}
    m_Int1 = print(head);
	m_Edit1.Format("%s",b);
	UpdateData(false);
//	CDialog::OnOK();
}

⌨️ 快捷键说明

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