📄 calcdlgn.txt
字号:
// CalcDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Calc.h"
#include "CalcDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include<math.h>
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CCalcDlg dialog
CCalcDlg::CCalcDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCalcDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCalcDlg)
m_str = _T("");
m_answer = _T("");
m_mode = -1;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCalcDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCalcDlg)
DDX_Text(pDX, IDC_EDIT_STR, m_str);
DDX_Text(pDX, IDC_EDIT_ANSWER, m_answer);
DDX_Radio(pDX, IDC_RADIO_INT, m_mode);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCalcDlg, CDialog)
//{{AFX_MSG_MAP(CCalcDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CALC, OnButtonCalc)
ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
ON_EN_CHANGE(IDC_EDIT_STR, OnChangeEditStr)
ON_BN_CLICKED(IDC_RADIO_INT, OnRadioInt)
ON_BN_CLICKED(IDC_RADIO_FLOAT, OnRadioFloat)
ON_EN_CHANGE(IDC_EDIT_ANSWER, OnChangeEditAnswer)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCalcDlg message handlers
BOOL CCalcDlg::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
// m_str="4+(5+6)+(6+5)*(4*(8+9)-4*8)+8*(9-7)-7";
// m_str="4*5";
m_mode=0;
UpdateData(0);
return TRUE; // return TRUE unless you set the focus to a control
}
void CCalcDlg::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 CCalcDlg::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 CCalcDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCalcDlg::OnButtonCalc()
{
CWaitCursor m_waitcursor;
BOOL flag=0;int left=0,right=0;
for(int i=0;i<m_str.GetLength();i++)
{
if(m_str.GetAt(i)=='+'||m_str.GetAt(i)=='-'||m_str.GetAt(i)=='*'||m_str.GetAt(i)=='/')
if(m_str.GetAt(i+1)=='*'||m_str.GetAt(i+1)=='/')
flag=1;
if(m_str.GetAt(i)=='*'||m_str.GetAt(i)=='/')
if(m_str.GetAt(i+1)=='+'||m_str.GetAt(i+1)=='-')
flag=1;
if(m_str.GetAt(i)=='(')left++;
if(m_str.GetAt(i)==')')right++;
}
if(left!=right)flag=1;
if(left>9||right>9)
{
flag=1;
MessageBox("输入的计算式括号不可超过9个!","抱歉",MB_OK|MB_ICONERROR);
}
if(flag==0)
{
if(m_mode==0)
{
if(m_str.Find('.',0)==-1)
{
if(m_str.GetLength())
m_answer=GetInt(m_str);
}
else
{
m_mode=1;
UpdateData(0);
OnButtonCalc();
}
}
else
{
if(m_str.GetLength())
m_answer=GetFloat(m_str);
}
// MessageBox("暂不能实现浮点运算,对不起!","抱歉",MB_OK|MB_ICONASTERISK);
}
else
MessageBox("输入的计算式有语法错误!","错误",MB_OK|MB_ICONERROR);
UpdateData(0);
}
void CCalcDlg::OnButtonExit()
{
// TODO: Add your control notification handler code here
OnOK();
}
void CCalcDlg::OnChangeEditStr()
{
UpdateData(1);
}
void CCalcDlg::OnRadioInt()
{
UpdateData(1);
}
void CCalcDlg::OnRadioFloat()
{
UpdateData(1);
}
CString CCalcDlg::GetFloat(CString str)
{
int count[10][2];for(int i=0;i<10;i++){count[i][0]=-1;count[i][1]=-1;}
/////以上初始化数组count,使之全为-1
int cou=0;//用于填充数组count的第一维
int sgn=0;//用于记录无用的“(”和“)”,遇“(”加1,遇“)”减1。
for(i=str.GetLength()-1;i>=0;i--)
{
if(str.GetAt(i)==41)
{
if(sgn==0)
count[cou][0]=i;
sgn++;
}
if(str.GetAt(i)==40)
{
if(sgn==1)
{
count[cou][1]=i;
cou++;
}
sgn--;
}
}
if(cou)
{
int kuo=0;CString kuostr,kuoanswer;
for(kuo=0;kuo<cou;kuo++)
{
kuostr=str.Mid(count[kuo][1]+1,count[kuo][0]-count[kuo][1]-1);
// MessageBox(kuostr);
kuoanswer=GetFloat(kuostr);
str.Delete(count[kuo][1],count[kuo][0]-count[kuo][1]+1);
str.Insert(count[kuo][1],kuoanswer);
return GetFloat(str);
}
}
//以上得到m_str是几个(...)组成的
if(str.GetAt(0)=='+')str.Delete(0);
if(str.Mid(1).SpanIncluding("0123456789.").GetLength()==str.Mid(1).GetLength())
return str;
CString s;s=str;
for(i=0;i<str.GetLength();i++)//负负得正;“--”号变“+”。
{
if(str.GetAt(i)=='-'&&i!=0&&str.GetAt(i+1)=='-')
{
str.Delete(i,2);
str.Insert(i,'+');
}
}
s=str;
for(i=0;i<str.GetLength();i++)//减法变加法;“-”号变“+-”。
{
if(str.GetAt(i)=='-'&&i!=0&&str.GetAt(i-1)!='+'&&str.GetAt(i-1)!='*'&&str.GetAt(i-1)!='/')
str.Insert(i,'+');
}
s=str;
for(i=0;i<str.GetLength();i++)//去掉多余加号
{
if(str.GetAt(i)=='+'&&str.GetAt(i+1)=='+')
{s.Delete(i);}
}
str=s;
// MessageBox(s);
if(cou==0)//式中没有括号
{float cheng=0;int chengdown=0,chengup=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -