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

📄 calculate2dlg.cpp

📁 带界面计算器 vc++ 界面编程 利用堆栈
💻 CPP
字号:
// calculate2Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "calculate2.h"
#include "calculate2Dlg.h"

//#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//#include<iostream.h>

#define size 100
#define stackincrement 10
#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()

/////////////////////////////////////////////////////////////////////////////
// CCalculate2Dlg dialog

CCalculate2Dlg::CCalculate2Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCalculate2Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCalculate2Dlg)
		// 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 CCalculate2Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCalculate2Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCalculate2Dlg, CDialog)
	//{{AFX_MSG_MAP(CCalculate2Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton9)
	ON_BN_CLICKED(IDC_BUTTON11, OnButton1add)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON12, OnButton1plus)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON9, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON13, OnButton1multiply)
	ON_BN_CLICKED(IDC_BUTTON10, OnButton0)
	ON_BN_CLICKED(IDC_BUTTON15, OnButtondot)
	ON_BN_CLICKED(IDC_BUTTON14, OnButtondiv)
	ON_BN_CLICKED(IDC_BUTTON16, OnButtonleft)
	ON_BN_CLICKED(IDC_BUTTON17, OnButtonright)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton8)    
	ON_BN_CLICKED(IDCANCEL2, OnOkCE2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCalculate2Dlg message handlers

BOOL CCalculate2Dlg::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 CCalculate2Dlg::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 CCalculate2Dlg::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 CCalculate2Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
//--------------------------------------
void CCalculate2Dlg::OnOK() 
{ 
	CString m_showend;	
	
	GetDlgItemText(IDC_EDIT1,m_show);
	m_showend=m_show;
	m_show+="#";

	SqStackChar OPTR;
    SqStackData OPND;

	char c,theta,x;
	float a,b;
	char Opr[8][8]; //存放优先规则
	char cha[7]={'+','-','*','/','(',')','#'};

	//char sou[15]="2.3*(2+3)#";
	//char *p=m_show;
	int per=0;
	int index=0;

	float result;

	int i=0,j=0;
	int flag=0;  //出现小数点标记
	int flagin=0;  //运算符前有数字
	float s;
	char data[10];

	ReadOpr(Opr);

	InitStackChar(&OPTR);
	InitStackData(&OPND);
	PushChar(&OPTR,'#');

	c=m_show.GetAt(index++);
	

	while(c!='#'||GetTopChar(&OPTR)!='#')
	{
		if(!In(c,cha))            //不是运算符,是数据
		{
			if(c>='0'&&c<='9')   //是数字
			{
			   flagin=1;
			   if(!flag)        //没有小数点
			   {
				   data[++i]=c;
				   j=i;
			   }
			   else  data[++j]=c;
			   
			}// if datachar;

			else if(c=='.')   flag=1;
		    	else                    //是其他非法字符
				{			
			    	//MessageBox("You input illegal character(s),please check!\n","ERROR");
					SetDlgItemText(IDC_EDIT1,"You input illegal character(s)!");
					return ;
			    //	exit(0);
				}


		c=m_show.GetAt(index++);
		}//if not operater
		
		else    //是运算符
		{
			if(flagin)
			{
				s=0;
				for(int m=1;m<=j;m++)
					s+=(data[m]-'0')*pow(10,i-m);
				PushData(&OPND,s);
				i=j=flag=flagin=0;
			}  //if have input


			switch(Precede(GetTopChar(&OPTR),c,Opr,cha))
			{
		     	case '<': PushChar(&OPTR,c);  c=m_show.GetAt(index++); break;    ////////
			    case '=': PopChar(&OPTR,&x);  c=m_show.GetAt(index++); break;    ///////
			    case '>': PopChar(&OPTR,&theta); 
				          PopData(&OPND,&b);  
					      PopData(&OPND,&a); 
					      PushData(&OPND,Operate(a,theta,b));
				          break;					  
			}	//switch		
		} //else 
	} //while

	result=GetTopData(&OPND);

//	printf("%f\n",result);
//	cout<<result<<endl;
	CString   ss;
	ss.Format("%f",result);
	m_showend+="="+ss;
	SetDlgItemText(IDC_EDIT1,m_showend);



	
//	CDialog::OnOK();
}

void CCalculate2Dlg::OnButton7() ///
{
	// TODO: Add your control notification handler code here
  m_show+="7";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

void CCalculate2Dlg::OnButton8() 
{
	m_show+="8";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

void CCalculate2Dlg::OnButton9() 
{
	m_show+="9";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

void CCalculate2Dlg::OnButton1add() 
{
	m_show+="+";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

void CCalculate2Dlg::OnButton4() 
{
	m_show+="4";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

void CCalculate2Dlg::OnButton5() 
{
	m_show+="5";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

void CCalculate2Dlg::OnButton6() 
{
	m_show+="6";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

void CCalculate2Dlg::OnButton1plus() 
{
	m_show+="-";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

void CCalculate2Dlg::OnButton1() 
{
	m_show+="1";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

void CCalculate2Dlg::OnButton2() 
{
	m_show+="2";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

void CCalculate2Dlg::OnButton3() 
{
	m_show+="3";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

void CCalculate2Dlg::OnButton1multiply() 
{
	m_show+="*";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

void CCalculate2Dlg::OnButton0() 
{
	m_show+="0";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

void CCalculate2Dlg::OnButtondot() 
{
	m_show+=".";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

void CCalculate2Dlg::OnButtondiv() 
{
	m_show+="/";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

void CCalculate2Dlg::OnButtonleft() 
{
	m_show+="(";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

void CCalculate2Dlg::OnButtonright() 
{
	m_show+=")";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}


//--------------------------
int CCalculate2Dlg::In(char c,char cha[7])
{
	int i;
	for(i=0;i<7;i++)
		if(cha[i]==c)   return 1;
    return 0;
}


float CCalculate2Dlg::Operate(float a,char theta,float b)
{   
	switch(theta)
	{
	   case '+' :return a+b;break;
	   case '-' :return a-b;break;
	   case '*' :return a*b;break;
	   case '/' :return a/b;break;
	}
	return NULL;

}

char CCalculate2Dlg::Precede(char a,char b,char Opr[8][8],char cha[7])
{
	int i,first,last;

	for(i=0;i<7;i++)
	{
		if(cha[i]==a)  first=i;
		if(cha[i]==b)  last=i;
	}

	return Opr[first][last];
}

void CCalculate2Dlg::InitStackChar(SqStackChar *s)
{
	(*s).base=(char *)malloc(size*sizeof(char));
	(*s).top=(*s).base;
	(*s).stacksize=size;
}

void CCalculate2Dlg::InitStackData(SqStackData *s)
{
	(*s).base=(float *)malloc(size*sizeof(float));
	(*s).top=(*s).base;
	(*s).stacksize=size;
}

void CCalculate2Dlg::PushChar(SqStackChar *s,char n)
{
	if((*s).top-(*s).base>=(*s).stacksize)
	{
		(*s).base=(char *)realloc((*s).base,((*s).stacksize+stackincrement)*sizeof(char));
		(*s).top=(*s).base+(*s).stacksize;
		(*s).stacksize +=stackincrement;
	}

	*((*s).top++)=n;
}


void CCalculate2Dlg::PushData(SqStackData *s,float n)
{
	if((*s).top-(*s).base>=(*s).stacksize)
	{
		(*s).base=(float *)realloc((*s).base,((*s).stacksize+stackincrement)*sizeof(float));
		(*s).top=(*s).base+(*s).stacksize;
		(*s).stacksize +=stackincrement;
	}

	*((*s).top++)=n;
}


void CCalculate2Dlg::PopChar(SqStackChar *s,char *e)
{
	if((*s).top==(*s).base)  return;
	*e=*(--(*s).top);
}


void CCalculate2Dlg::PopData(SqStackData *s,float *e)
{
	if((*s).top==(*s).base)  return;
	*e=*(--(*s).top);
}


char CCalculate2Dlg::GetTopChar(SqStackChar *s)
{
//	if((*s).top==(*s).base)  return;
	return *((*s).top-1);
}

float CCalculate2Dlg::GetTopData(SqStackData *s)
{
//	if((*s).top==(*s).base)  return;
	return *((*s).top-1);
}

void CCalculate2Dlg::ReadOpr(char Opr[8][8])
{
	FILE *fp;
	int i,j;

	if((fp=fopen("c:\\1.txt","r"))==NULL)            //打开文件
		{
		     printf("Open file error!\n");
		     exit(0);
		}

    for(i=0;i<8;i++)
		for(j=0;j<8;j++)
			fscanf(fp,"%c",&Opr[i][j]); 

	
}

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

void CCalculate2Dlg::OnOkCE2() 
{
	// TODO: Add your control notification handler code here
	m_show="";
  SetDlgItemText(IDC_EDIT1,m_show); 
	
}

⌨️ 快捷键说明

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