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

📄 calculatordlg.cpp

📁 有二叉树计算表达式的值
💻 CPP
字号:
// calculatorDlg.cpp : implementation file
//

#include "StdAfx.h"
#include "calculator.h"
#include "calculatorDlg.h"
#include "Usinghelp.h"
#include "math.h"
#include "ctype.h"
#include "string.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()

/////////////////////////////////////////////////////////////////////////////
// CCalculatorDlg dialog

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

void CCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCalculatorDlg)
	DDX_Text(pDX, IDC_EDIT1, m_input);
	DDX_Text(pDX, IDC_EDIT2, m_output);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)
	//{{AFX_MSG_MAP(CCalculatorDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_PROCESS, OnButtonProcess)
	ON_BN_CLICKED(IDC_BUTTON_HELP, OnButtonHelp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCalculatorDlg message handlers

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

void CCalculatorDlg::OnButtonProcess() 
{
	UpdateData(TRUE);
	char str[100];
	for(int i=0;i<m_input.GetLength();i++)
		str[i]=m_input.GetAt(i);
	str[i]='\0';
	double value;
	value=compute(create(str));
	m_output.Format("%f",value);
	UpdateData(FALSE);
	
}


double CCalculatorDlg::compute(btree *bt)
{
	if(bt->left==NULL&&bt->right==NULL)
		switch(bt->oper.op)
		{
		case '+':return bt->left_num+bt->right_num;
		case '-':return bt->left_num-bt->right_num;
		case '*':return bt->left_num*bt->right_num;
		case '/':return bt->left_num/bt->right_num;
		case '^':return pow(bt->left_num,bt->right_num);
		}


	if(bt->left!=NULL&&bt->right==NULL)
		switch(bt->oper.op)
		{
		case '+':return compute(bt->left)+bt->right_num;
		case '-':return compute(bt->left)-bt->right_num;
		case '*':return compute(bt->left)*bt->right_num;
		case '/':return compute(bt->left)/bt->right_num;
		case '^':return pow(compute(bt->left),bt->right_num);
		}

	if(bt->left==NULL&&bt->right!=NULL)
		switch(bt->oper.op)
		{
		case '+':return bt->left_num+compute(bt->right);
		case '-':return bt->left_num-compute(bt->right);
		case '*':return bt->left_num*compute(bt->right);
		case '/':return bt->left_num/compute(bt->right);
		case '^':return pow(bt->left_num,compute(bt->right));

		}

	if(bt->left!=NULL&&bt->right!=NULL)
	     switch(bt->oper.op)
		{
		case '+':return compute(bt->left)+compute(bt->right);
		case '-':return compute(bt->left)-compute(bt->right);
		case '*':return compute(bt->left)*compute(bt->right);
		case '/':return compute(bt->left)/compute(bt->right);
		case '^':return pow(compute(bt->left),compute(bt->right));
		}
	return -1;

}

btree * CCalculatorDlg::create(char *str)
{
	btree *root,*current;//二叉树的根节点和当前节点
	current=root=new btree;
	operation tmpoper;
	tmpoper.op=0;tmpoper.priority=0;
	double tmp=0.0,prev=0.0;
	int cnt=0,root_priority;//mid_prirotiy临时存储根节点运算符的优先级
	unsigned int i=0;
	int left_right=0;//用于判断是给left_num,还是给right_num赋值
	int cntleftbracket=0,cntrightbracket=0;//左括号和右括号的个数
	current->left=NULL;
	current->right=NULL;
	current->parent=NULL;
	while(1)
	{
	  char ch;
		int dot=0;
//处理括号
		if(str[i]=='(')
		{
			cntleftbracket++;
			i++;
			for(unsigned int j=i;str[i];i++)
			{
				if(str[i]==')')cntrightbracket++;
				else if(str[i]=='(')cntleftbracket++;
				if(cntleftbracket==cntrightbracket)
				{
					char strbracket[100];
					for(int k=0;j<i;k++,j++)
						strbracket[k]=str[j];
					strbracket[k]='\0';
					tmp=compute(create(strbracket));
					prev=tmp;
					i++;
					break;
				}
			}
		}
		cntleftbracket=0;
		cntrightbracket=0;
//处理括号结束
	  int singed=1;
	  for(;i<strlen(str)&&(isdigit(str[i])||str[i]=='.'||str[i]=='-'&&!isdigit(str[i-1])&&i>0);i++)
	  {   ch=str[i];
			if(ch=='-')
			{
				singed=-1;
				continue;
			}
		   if(ch=='.')
			{
			 dot=1;
	 		  continue;
			}
		  if(dot==0)
			  tmp=(ch-0x30)+tmp*10.0;
		  else if(dot>0)
		  {
			  tmp+=(ch-0x30)*pow(10,-dot);
			  dot++;
		  }
	  }
	  tmp*=singed;
	  if(left_right==0)
		  current->left_num=tmp;
	  else if(left_right==1)
		current->right_num=tmp;
	  left_right=1-left_right;
	  prev=tmp;
	  tmp=0;
	  ch=str[i];
	  i++;
	  if(i>strlen(str))break;
	  if(cnt==0)
	  {
		switch(ch)
		{
			case '+':current->oper.op='+';current->oper.priority=1;break;
			case '-':current->oper.op='-';current->oper.priority=1;break;
			case '*':current->oper.op='*';current->oper.priority=2;break;
			case '/':current->oper.op='/';current->oper.priority=2;break;
			case '^':current->oper.op='^';current->oper.priority=3;break;
		}
		cnt++;
	  }
	  else
		   switch(ch)
			{
			case '+':tmpoper.op='+';tmpoper.priority=1;break;
			case '-':tmpoper.op='-';tmpoper.priority=1;break;
			case '*':tmpoper.op='*';tmpoper.priority=2;break;
			case '/':tmpoper.op='/';tmpoper.priority=2;break;
			case '^':tmpoper.op='^';tmpoper.priority=3;break;
			}
		root_priority=root->oper.priority;
	  if(tmpoper.priority>current->oper.priority)
	  {
		btree * tmpbt=new btree;
		tmpbt->left_num=prev;
		left_right=1;
		tmpbt->oper.op=tmpoper.op;
		tmpbt->oper.priority=tmpoper.priority;
		tmpbt->left=NULL;
		tmpbt->right=NULL;
		tmpbt->parent=current;
		current->right=tmpbt;
		current=tmpbt;
		cnt++;
	  }
	  else if(tmpoper.priority==current->oper.priority)
	  {
		  if(tmpoper.priority>root_priority)
		  {
			  btree *tmpbt=new btree;
			  left_right=1;
			  tmpbt->oper.op=tmpoper.op;
		      tmpbt->oper.priority=tmpoper.priority;
		      tmpbt->left=current;
		      tmpbt->right=NULL;
			  tmpbt->parent=current->parent;
			  current->parent->right=tmpbt;
			  current->parent=tmpbt;
		      current=tmpbt;
			  cnt++;
		  }
		  else if(tmpoper.priority==root_priority)
		  {
			  btree *tmpbt=new btree;
			  left_right=1;
			  tmpbt->oper.op=tmpoper.op;
			  tmpbt->oper.priority=tmpoper.priority;
			  tmpbt->left=root;
			  tmpbt->right=NULL;
			  tmpbt->parent=NULL;
			  root->parent=tmpbt;
			  root=tmpbt;
			  current=tmpbt;
			  cnt++;
		  }

	  } 
	   else if(tmpoper.priority<current->oper.priority&&tmpoper.priority!=0)
	  {
			while(1)
			{
				if(current==root)
				{
					btree *tmpbt=new btree;
					left_right=1;
					tmpbt->oper.op=tmpoper.op;
					tmpbt->oper.priority=tmpoper.priority;
					tmpbt->left=root;//tmpbt->left=current
					tmpbt->right=NULL;
					tmpbt->parent=NULL;
					root->parent=tmpbt;
					root=tmpbt;
					current=tmpbt;
					cnt++;
					break;
				}
				else if(tmpoper.priority>current->parent->oper.priority&&current->parent==root)
				{
					btree *tmpbt=new btree;
					left_right=1;
					tmpbt->oper.op=tmpoper.op;
					tmpbt->oper.priority=tmpoper.priority;
					tmpbt->left=current;
					tmpbt->right=NULL;
					tmpbt->parent=root;//tmpbt->parent=current->parent;
					root->right=tmpbt;
					current->parent=tmpbt;
					current=tmpbt;
					cnt++;
					break;
				}

				current=current->parent;
				if(tmpoper.priority==current->oper.priority&&tmpoper.priority!=root_priority)
				{
					btree *tmpbt=new btree;
					left_right=1;
					tmpbt->oper.op=tmpoper.op;
					tmpbt->oper.priority=tmpoper.priority;
					tmpbt->left=current;
					tmpbt->right=NULL;
					tmpbt->parent=current->parent;
					current->parent->right=tmpbt;
					current->parent=tmpbt;
					current=tmpbt;
					cnt++;
					break;
				}
				if(tmpoper.priority==current->oper.priority&&tmpoper.priority==root_priority)
				{
					btree *tmpbt=new btree;
					left_right=1;
					tmpbt->oper.op=tmpoper.op;
					tmpbt->oper.priority=tmpoper.priority;
					tmpbt->left=root;
					tmpbt->right=NULL;
					tmpbt->parent=NULL;
					root->parent=tmpbt;
					root=tmpbt;
					current=tmpbt;
					cnt++;
					break;
				}

			}	 
	  }
	 
	}
	return root;
}

void CCalculatorDlg::OnButtonHelp() 
{
	CUsinghelp dlg;
	dlg.DoModal();
}

void CCalculatorDlg::OnOK() 
{
	OnButtonProcess();

}

⌨️ 快捷键说明

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