普通计算模块:(运行效果图参见计算器项目综述)
该模块开发文档截图
这一模块是该项目的核心。考虑核心代码的实现:由于运算符只有这六种(+、-、*、/、平方、开方)所以就可以将表达式中的运算符存在一个char数组中,然后先对该数组中平方和开方这两个一元运算符进行处理,这样表达式中就只剩下另外四种运算符了。而这四种运算符只有两个优先级,利用堆栈很容易实现。遇到+、-就将数字(或其相反数)push(),遇到*、/就进行pop(),这样最后将栈中的数字加和即可。
该模块完整代码如下:
// 简化调试Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "简化调试.h"
#include "简化调试Dlg.h"
#include "cmath"
#include "stack"
#include "MyDlg_1.h"
#include "MyDlg_2.h"
#include "MyDlg_3.h"
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CString m_num("");
CString m_sig("");
double xiaoshu,jieguo,answer;
int zhengshu,index;
int i,j,num_lenth=0;
double num[50];
stack <double> result;
/
// 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()
/
// CMyDlg dialog
CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyDlg::IDD, pParent)
{
//{
{AFX_DATA_INIT(CMyDlg)
m_str = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{
{AFX_DATA_MAP(CMyDlg)
DDX_Text(pDX, IDC_EDIT1, m_str);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
//{
{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_BN_CLICKED(IDC_BUT