📄 calculatordlg.cpp
字号:
case IDC_OPERATION17:UpdateData(FALSE);break;//右边的括号
case IDC_OPERATION18:number1=number2=m_number=0;UpdateData(FALSE);NumberState=1;break;
case IDC_OPERATION19:DotState=1;t=1;UpdateData(FALSE);break;
case IDC_OPERATION20:Calculator ();break;
case IDC_OPERATION21:
{
if(NumberState==1)
{
number1=m_number=3.1415926;
UpdateData(FALSE);
}
else
{
number2=m_number=3.1415926;
UpdateData(FALSE);
}
break;
}
case IDC_OPERATION22:
{
if(NumberState==1)
{
number1=m_number=0.618;
UpdateData(FALSE);
}
else
{
number2=m_number=0.618;
UpdateData(FALSE);
}
break;
}
}
}
void CCalculatorDlg::Calculator ()
{
if(!m_bExp)
{
switch(OperationState)
{
case 1:m_number=(double)number1/number2;NumberState=1;UpdateData(FALSE);break;
case 2:m_number=number1*number2;NumberState=1;UpdateData(FALSE);break;
case 3:m_number=number1+number2;NumberState=1;UpdateData(FALSE);break;
case 4:m_number=number1-number2;NumberState=1;UpdateData(FALSE);break;
case 5:
{
m_number=1;
for(int i=0;i<number2;i++)
{
m_number=m_number*number1;
}
NumberState=1;
UpdateData(FALSE);
break;
}
case 6:
{
m_number=fmod(number1,number2);
NumberState=1;
UpdateData(FALSE);
break;
}
}
OperationState=0;
}
else
{
int ans; //保存算术表达式的计算结果
bool quit=false; //是否退出计算
CExpress exp;
UpdateData(true);
int len=m_sExpress.GetLength();
for(int i=0;i<m_sExpress.GetLength();i++)
exp.expr[i]=m_sExpress.GetAt(i);
exp.expr[len]='\0';
if(setjmp(errjb)==0) //如果没有错误
{
if(exp.expr[0]=='q'||exp.expr[0]=='Q')
//检查第一个字符,是否退出?
quit=true;
else
{
//调用推导式“E -> T+E | T-E | T”的函数,
//从起始符号“E”开始推导。
ans=exp.E_AddSub();
//此时,程序认为对表达式的语法分析已经完毕,下面判断出错的原因:
//如果表达式中的某个右括号后直接跟着数字或其他字符,
//则报错,因为数字i不属于FOLLOW())集。
if(exp.expr[exp.pos-1]==')'&&exp.expr[exp.pos]!='\0')
exp.Error(CHAR_AFTER_RIGHT);
//如果表达式中的某个数字或右括号后直接跟着左括号,
//则报错,因为左括号不属于FOLLOW(E)集。
if(exp.expr[exp.pos]=='(')
exp.Error(LEFT_AFTER_NUM);
//如果结尾有其他非法字符
if(exp.expr[exp.pos]!='\0')
exp.Error(INVALID_CHAR_TAIL);
}
}
m_nResult=ans;
char str[10];
sprintf(str,"%d",ans);
m_sExpress=m_sExpress+"="+str;
UpdateData(FALSE);
}
}
void CCalculatorDlg::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 CCalculatorDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCalculatorDlg::OnLoadskin()
{
char strExt[]= "Skin Files(*.smf)|*.smf||";
CFileDialog dlg( TRUE, "*.smf", "*.smf" , OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT
, strExt , this );
if( dlg.DoModal() == IDOK )
{
LoadSkinFile( (char*)(LPCSTR)(dlg.GetPathName()) );
}
}
void CCalculatorDlg::OnAppAbout()
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
void CCalculatorDlg::OnContextMenu(CWnd* pWnd, CPoint point)
{
// CG: This block was added by the Pop-up Menu component
{
if (point.x == -1 && point.y == -1){
//keystroke invocation
CRect rect;
GetClientRect(rect);
ClientToScreen(rect);
point = rect.TopLeft();
point.Offset(5, 5);
}
CMenu menu;
VERIFY(menu.LoadMenu(IDR_POPUPMENU));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
CWnd* pWndPopupOwner = this;
while (pWndPopupOwner->GetStyle() & WS_CHILD)
pWndPopupOwner = pWndPopupOwner->GetParent();
TrackSkinPopupMenu( pPopup->m_hMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
pWndPopupOwner->m_hWnd );
}
// OnLoadskin() ;
}
BOOL CCalculatorDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN || pMsg->message == WM_CHAR)
SendMessage(pMsg->message,pMsg->wParam,pMsg->lParam);
return CDialog::PreTranslateMessage(pMsg);
}
void CCalculatorDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if(!m_bExp)
{
UINT nID;
int n=0;
if(nChar<58&&nChar>47){
switch(nChar) {
case 49:nID=IDC_NUMBER1;break;
case 50:nID=IDC_NUMBER2;break;
case 51:nID=IDC_NUMBER3;break;
case 52:nID=IDC_NUMBER4;break;
case 53:nID=IDC_NUMBER5;break;
case 54:nID=IDC_NUMBER6;break;
case 55:nID=IDC_NUMBER7;break;
case 56:
{
if(::GetKeyState(VK_SHIFT)&0x8000)
{OnOperationKey(IDC_OPERATION4);goto a;}
else
nID=IDC_NUMBER8;
}
break;
case 57:
{
if(::GetKeyState(VK_SHIFT)&0x8000)
{OnOperationKey(IDC_OPERATION16);goto a;}
else
nID=IDC_NUMBER9;
}
break;
case 48:
{
if(::GetKeyState(VK_SHIFT)&0x8000)
{OnOperationKey(IDC_OPERATION17);goto a;}
else
nID=IDC_NUMBER10;
}
break;
default:break;
}
OnNumberKey(nID);
}
else
{
UINT nID;
switch(nChar)
{
case 189: nID=IDC_OPERATION6; break; //-
case 8: nID=IDC_OPERATION2; break; //BackSpace
case 191: nID=IDC_OPERATION3; break; ///
case 187: //+/=
{
if(::GetKeyState(VK_SHIFT)&0x8000)
nID=IDC_OPERATION5;
else
nID=IDC_OPERATION20;
}
break;
case 190: nID=IDC_OPERATION19; break; //.
}
OnOperationKey(nID);
}
}
a: CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
// 是否选择表达式运算方式
//
void CCalculatorDlg::OnOptExp()
{
m_bExp=!m_bExp;
CEdit* pLB=(CEdit*)GetDlgItem(IDC_EDIT_PUTOUT);
CEdit* pLBNew=(CEdit*)GetDlgItem(IDC_EDIT1);
if(m_bExp)
{
pLB->ShowWindow(SW_HIDE);
pLBNew->ShowWindow(SW_SHOW);
}
else
{
pLB->ShowWindow(SW_SHOW);
pLBNew->ShowWindow(SW_HIDE);
}
m_sExpress="0";
m_nResult=0;
m_number=0;
UpdateData(FALSE);
}
void CCalculatorDlg::OnUpdateOptExp(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_bExp==true);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -