📄 calcdlg.cpp
字号:
// 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("");
//}}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);
//}}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_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
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;
CString a=m_str.SpanIncluding(".0123456789+-*/^%()");
if(a.CompareNoCase(m_str))
{
MessageBox("输入的字符中有非法字符");
flag=1;
}
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)
{
MessageBox("输入的括号不成对!","抱歉",MB_OK|MB_ICONERROR);
flag=1;
}
if(left>20)
{
flag=1;
MessageBox("输入的计算式括号不可超过20个!","抱歉",MB_OK|MB_ICONERROR);
}
if(flag==0)
{
m_answer=GetFloat(m_str);
for(int i=m_answer.GetLength()-1;m_answer.GetAt(i)!=46;i--)
if(m_answer.GetAt(i)==48)
m_answer.Delete(i);
if(m_answer.GetAt(m_answer.GetLength()-1)==46)
m_answer.Delete(m_answer.GetLength()-1);
}
UpdateData(0);
}
void CCalcDlg::OnButtonExit()
{
// TODO: Add your control notification handler code here
OnOK();
}
void CCalcDlg::OnChangeEditStr()
{
UpdateData(1);
m_str.TrimLeft();
m_str.TrimRight();
UpdateData(0);
}
void CCalcDlg::OnChangeEditAnswer()
{
UpdateData(0);
}
CString CCalcDlg::GetFloat(CString str)
{
int count[21][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;
if(cou==0)//式中没有括号
{
double cheng=0;int chengdown=0,chengup=0;
CString chengstr,strup,strdown;
double chu=0;int chudown=0,chuup=0;
CString chustr,struup,strudown;
double fang=0;int fangdown=0,fangup=0;
CString fangstr,strfangup,strfangdown;
double mo=0;int modown=0,moup=0;int mm=0;
CString mostr,strmup="9",strmdown="8";
for(i=0;i<str.GetLength();i++)//实现乘方
{
if(str.GetAt(i)=='^')
{
for(fangup=i-1;fangup>=0;fangup--)
{
if(str.GetAt(fangup)<46||str.GetAt(fangup)>57||str.GetAt(fangup)==47)
{break; }
// strup.Format("%d\n%d",chengup,i);
}
for(fangdown=i+2;fangdown<str.GetLength();fangdown++)
{
if(str.GetAt(fangdown)<46||str.GetAt(fangdown)>57||str.GetAt(fangdown)==47)
break;
}
break;
}
}
if(fangdown)
{
strfangup=str.Mid(fangup+1,i-fangup-1);
strfangdown=str.Mid(i+1,fangdown-i-1);
// MessageBox(strdown);
fang=pow(atof(strfangup),atof(strfangdown));
fangstr.Format("%10.10f",fang);
str.Delete(fangup+1,fangdown-fangup-1);
str.Insert(fangup+1,fangstr);
return GetFloat(str);
}
for(i=0;i<str.GetLength();i++)//实现乘法和除法
{
if(str.GetAt(i)=='*')
{
for(chengup=i-1;chengup>=0;chengup--)
{
if(str.GetAt(chengup)<46||str.GetAt(chengup)>57||str.GetAt(chengup)==47)
{break; }
// strup.Format("%d\n%d",chengup,i);
}
for(chengdown=i+2;chengdown<str.GetLength();chengdown++)
{
if(str.GetAt(chengdown)<46||str.GetAt(chengdown)>57||str.GetAt(chengdown)==47)
break;
}
break;
}
if(str.GetAt(i)=='/')
{
for(chuup=i-1;chuup>=0;chuup--)
{
if(str.GetAt(chuup)<46||str.GetAt(chuup)>57||str.GetAt(chuup)==47)
{break; }
}
for(chudown=i+2;chudown<str.GetLength();chudown++)
{
if(str.GetAt(chudown)<46||str.GetAt(chudown)>57||str.GetAt(chudown)==47)
break;
}
break;
}
if(str.GetAt(i)=='%')//实现求余
{
for(moup=i-1;moup>=0;moup--)
{
if(str.GetAt(moup)==47||str.GetAt(moup)<46||str.GetAt(moup)>57)
{if(str.GetAt(moup)==46)mm=1;break; }
}
for(modown=i+2;modown<str.GetLength();modown++)
{
if(str.GetAt(modown)==47||str.GetAt(modown)<46||str.GetAt(modown)>57)
{ if(str.GetAt(modown)==46)mm=1;break;}
}
break;
}
}
if(chengdown)
{
strup=str.Mid(chengup+1,i-chengup-1);
strdown=str.Mid(i+1,chengdown-i-1);
// MessageBox(strdown);
cheng=atof(strup)*atof(strdown);
chengstr.Format("%10.10f",cheng);
str.Delete(chengup+1,chengdown-chengup-1);
str.Insert(chengup+1,chengstr);
return GetFloat(str);
}
if(chudown)
{
struup=str.Mid(chuup+1,i-chuup-1);
strudown=str.Mid(i+1,chudown-i-1);
chu=atof(struup)/atof(strudown);
chustr.Format("%10.10f",chu);
str.Delete(chuup+1,chudown-chuup-1);
str.Insert(chuup+1,chustr);
return GetFloat(str);
}
if(modown)//求余
{
strmup=str.Mid(moup+1,i-moup-1);
strmdown=str.Mid(i+1,modown-i-1);
// return "Error:小数不可求余";
mo=atoi(strmup)%atoi(strmdown);
mostr.Format("%10.10f",mo);
str.Delete(moup+1,modown-moup-1);
str.Insert(moup+1,mostr);
return GetFloat(str);
}
for(i=0;i<str.GetLength();i++)//实现加法
{
if(str.GetAt(i)=='+')
{
CString temp1,temp2;double xx=0;
temp1=str.Left(i);
temp2=GetFloat(str.Mid(i+1));
xx=atof(temp1)+atof(temp2);
s.Format("%10.10f",xx);
return s;
}
}
}
return s;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -