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

📄 caldlg.cpp

📁 简单的加减乘除运算。功能齐全。运行速度可以。支持括号的运算。代码量少。
💻 CPP
字号:


#include "stdafx.h"
#include "cal.h"
#include "calDlg.h"
#include <string>
#include "stack.h"
using namespace std;
#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()

/////////////////////////////////////////////////////////////////////////////
// CCalDlg dialog

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

BEGIN_MESSAGE_MAP(CCalDlg, CDialog)
	//{{AFX_MSG_MAP(CCalDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDCLEAR, OnClear)
	//ON_BN_CLICKED(IDC_ABOUT, OnAbout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCalDlg message handlers

BOOL CCalDlg::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 CCalDlg::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 CCalDlg::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 CCalDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
bool CCalDlg::check(string* sp){//检测表达式是否合法
	int i;
	if(!sp->size()){//检测是否为空
		SetDlgItemText(IDC_STATIC_1,"Empty expression.");
		return false;
	}
	for(i=0;i<sp->size();i++){//检测字符是否合法
		if(!(((*sp)[i]>='0'&&(*sp)[i]<='9')||(*sp)[i]=='.'
			||(*sp)[i]=='+'||(*sp)[i]=='-'||(*sp)[i]=='*'||(*sp)[i]=='/'
			||(*sp)[i]=='('||(*sp)[i]==')')){
				cerr<<""<<endl;
				SetDlgItemText(IDC_STATIC_1,"非法字符串");
				return false;
		}
	}
	int qnum=0;//qnum记录左括号-右括号
	for(i=0;i<(*sp).size();i++){
		if((*sp)[i]=='(')qnum++;
		else if((*sp)[i]==')')qnum--;
		if(qnum<0){		
			SetDlgItemText(IDC_STATIC_1,"括号错误");
			return false;
		}
	}
	if(qnum){//qnum应该为零
			SetDlgItemText(IDC_STATIC_1,"括号错误");
			return false;
	}
	char x=(*sp)[0],y,z;
	
	if(!(x=='('||x=='+'||x=='-'||(x>='0'&&x<='9')||x=='.')){//检测第一个字符
		SetDlgItemText(IDC_STATIC_1,"首字符错误");
		return false;
	}
	x=(*sp)[sp->size()-1];
	if(!(x==')'||(x>='0'&&x<='9')||x=='.')){//检测最后一个字符
		SetDlgItemText(IDC_STATIC_1,"尾字符错误");
		return false;
	}
	for(i=1;i<(*sp).size()-1;i++){//检测字符串中非法情况
		x=(*sp)[i];y=(*sp)[i-1];z=(*sp)[i+1];
		if(x=='('){
			if(y>='0'&&y<='9'||y=='.'){//左括号
				SetDlgItemText(IDC_STATIC_1,"非法字符串");
				return false;
			}
			if(z=='+'||z=='-'){//+-合法
				
				string s1=(*sp).substr(0,i+1);
				string s2=(*sp).substr(i+1,(*sp).size()-i+1);
				(*sp)=s1+'0'+s2;
				
			}
			if(z=='*'||z=='/'||z==')'){//*/非法
				SetDlgItemText(IDC_STATIC_1,"非法字符串");
				return false;
			}
		}
		else if(x==')'){//右括号
			if(y=='+'||y=='-'||y=='*'||y=='/'){
				SetDlgItemText(IDC_STATIC_1,"非法字符串");
				return false;
			}
			if(z>='0'&&z<='9'||z=='.'){
				SetDlgItemText(IDC_STATIC_1,"非法字符串");
				return false;
			}
		}
		else if(x=='+'||x=='-'||x=='*'||x=='/'){
			if(z=='+'||z=='-'||z=='*'||z=='/'){
				SetDlgItemText(IDC_STATIC_1,"非法字符串");
				return false;
			}
		}
	}
	x=(*sp)[i];y=(*sp)[i-1];
	if(x==')'&&(y=='+'||y=='-'||y=='*'||y=='/')){
				SetDlgItemText(IDC_STATIC_1,"非法字符串");
				return false;
	}
	return true;
}

void CCalDlg::cal(string s){
	s=s+'+';
	Stack<double>ns;//数据	
	Stack<char>ss;//符号
	Stack<int>ps;//优先级
	ns.Add(0);//加个零
	int p1=1,p2=2;//p1+-优先级,p2*/优先级
	double current=0;//字符串转数字
	string cur;//字码转数字字符串
	int i;
	char x;
	for(i=0;i<s.size();i++){
		x=s[i];
		if(x>='0'&&x<='9'){//找数字
			
			cur=x;
			while(i<s.size()&&(s[i+1]>='0'&&s[i+1]<='9'||s[i+1]=='.')){
				
				cur=cur+s[i+1];
				i++;
			}
			current=atof(cur.data());
			ns.Add(current);
		}
		else if(x=='('){
			p1+=2;
			p2+=2;
		}
		else if(x==')'){
			p1-=2;
			p2-=2;
		}
		else if(x=='*'||x=='/'){//*/情况
			while(!ps.IsEmpty()){//优先级非空
				int t=ps.Top();//取栈顶
				if(t<p2)
					break;//如果当前优先级高,break
				double m,n;
				char q;
				ns.Delete(n);//取得m,n
				ns.Delete(m);
				ss.Delete(q);//取操作符q
				ps.Delete(t);//取出优先级t
				if(q=='+')
					ns.Add(m+n);//结果压栈
				else if(q=='-')
					ns.Add(m-n);
				else if(q=='*')
					ns.Add(m*n);
				else if(q=='/'){
					if(n==0){//判断除数是否为零
						SetDlgItemText(IDC_STATIC_1,"Divided by 0.");//error
						return ;
					}
					ns.Add(m/n);
				}
			}
			ps.Add(p2);//优先级压栈p2
			ss.Add(x);//符号压栈
		}
		else if(x=='+'||x=='-'){//+-情况
			while(!ps.IsEmpty()){//优先级非空
				int t=ps.Top();//取栈顶
				if(t<p1)
					break;//如果当前优先级高,break
				double m,n;
				char q;
				ns.Delete(n);//取得m,n
				ns.Delete(m);
				ss.Delete(q);//取操作符q
				ps.Delete(t);//取出优先级t
				if(q=='+')
					ns.Add(m+n);//结果压栈
				else if(q=='-')
					ns.Add(m-n);
				else if(q=='*')
					ns.Add(m*n);
				else if(q=='/'){
					if(n==0){//判断除数是否为零
						SetDlgItemText(IDC_STATIC_1,"Divided by 0.");//error
						return ;
					}
					ns.Add(m/n);
				}
			}
			ps.Add(p1);//优先级压栈p1
			ss.Add(x);//符号压栈
		}
		
	}

	ns.Delete(current);//取最后结果
	CString str;
	str.Format("%f",current);
	SetDlgItemText(IDC_STATIC_1,str);
}
void CCalDlg::OnOK() //运行按钮
{

	// TODO: Add extra validation here
	CString str; 

	GetDlgItemText(IDC_EDIT1,str);
	string s,*sp;
	s=str.GetBuffer(0);
	sp=&s;
	if(check(sp)){
		cal(s);
		
	}
}

void CCalDlg::OnClear() //清零按钮
{
	SetDlgItemText(IDC_EDIT1,"");
	SetDlgItemText(IDC_STATIC_1,"");

	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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