?? susuan~1.cpp
字號:
// SusuanDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Susuan.h"
#include "SusuanDlg.h"
#include "math.h"
#include "stdlib.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSusuanDlg dialog
CSusuanDlg::CSusuanDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSusuanDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSusuanDlg)
m_EDIT1 = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSusuanDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSusuanDlg)
DDX_Text(pDX, IDC_EDIT1, m_EDIT1);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSusuanDlg, CDialog)
//{{AFX_MSG_MAP(CSusuanDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSusuanDlg message handlers
BOOL CSusuanDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_Button[0]=(CButton*)GetDlgItem(IDC_BUTTON1);
m_Button[1]=(CButton*)GetDlgItem(IDC_BUTTON2);
m_Button[2]=(CButton*)GetDlgItem(IDC_BUTTON3);
m_Button[3]=(CButton*)GetDlgItem(IDC_BUTTON4);
//BackGroundBitmap.LoadBitmap(IDB_BITMAP14);
Bitmap[0].LoadBitmap(IDB_BITMAP1);
Bitmap[1].LoadBitmap(IDB_BITMAP2);
Bitmap[2].LoadBitmap(IDB_BITMAP3);
Bitmap[3].LoadBitmap(IDB_BITMAP4);
Bitmap[4].LoadBitmap(IDB_BITMAP5);
Bitmap[5].LoadBitmap(IDB_BITMAP6);
Bitmap[6].LoadBitmap(IDB_BITMAP7);
Bitmap[7].LoadBitmap(IDB_BITMAP8);
Bitmap[8].LoadBitmap(IDB_BITMAP9);
Bitmap[9].LoadBitmap(IDB_BITMAP10);
Bitmap[10].LoadBitmap(IDB_BITMAP11);
Bitmap[11].LoadBitmap(IDB_BITMAP12);
Bitmap[12].LoadBitmap(IDB_BITMAP13);
m_hBackGround = ::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(
IDB_BITMAP14));
for (int i=1;i<5;i++)
{
m_Button[i-1]->SetBitmap(m_hBackGround);
}
// 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
}
// 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 CSusuanDlg::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 CSusuanDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSusuanDlg::OnOK()
{
// TODO: Add extra validation here
UpdateData();
KillTimer(1);
int Result=TotalCompute(m_EDIT1);
if(Result==24)
{
MessageBox("您真行,我服了您!","對了",MB_OK);
}
else
{ CString temp;
temp.Format("%d",Result);
CString Answer="您輸入的表達式的計算結果為"+temp+"!";
MessageBox(Answer,"錯了",MB_OK);
}
//CDialog::OnOK();
}
void CSusuanDlg::RandomDisplay()
{
int temp[14];
int k;
for(int m=1;m<14;m++)
temp[m]=m;
for(int j=13;j>0;j--)
{
srand(10*time(NULL));
k=1+rand()%j;
card[j]=temp[k];
for(int t=k;t<j;t++)
temp[t]=temp[t+1];
}
}
void CSusuanDlg::OnButton5()
{
RandomDisplay();
for(int i=1;i<5;i++)
{int j;
j=card[i+5];
m_Button[i-1]->SetBitmap(HBITMAP(Bitmap[j-1]));}
GetDlgItem(IDC_BUTTON5)->SetWindowText("重新開始");
// TODO: Add your control notification handler code here
SetTimer(1,1000, NULL);
SpendTime=0;
}
//定位最后一個算術符號的位置
int CSusuanDlg::AnyLastPos(CString Str)
{
int SubPos=Str.ReverseFind('-')+1;
int PluPos=Str.ReverseFind('+')+1;
int MulPos=Str.ReverseFind('*')+1;
int DivPos=Str.ReverseFind('/')+1;
int Pos=SubPos;
if(Pos<PluPos)
Pos=PluPos;
if(Pos<MulPos)
Pos=MulPos;
if(Pos<DivPos)
Pos=DivPos;
return Pos;
}
//---------------------------------------------------------------------------
//定位最先一個算術符號的位置
int CSusuanDlg::AnyFirstPos(CString Str)
{
int SubPos=Str.Find('-')+1;
int PluPos=Str.Find('+')+1;
int MulPos=Str.Find('*')+1;
int DivPos=Str.Find('/')+1;
int ForPos=Str.Find('^')+1;
int Pos=200;
if(SubPos==0) //如果沒有-號
SubPos=200; //將SubPos設置成一個不可能的值
if(PluPos==0) //如果沒有-號
PluPos=200; //將PluPos設置成一個不可能的值
if(MulPos==0) //如果沒有-號
MulPos=200; //將MulPos設置成一個不可能的值
if(DivPos==0) //如果沒有-號
DivPos=200; //將DivPos設置成一個不可能的值
if(ForPos==0) //如果沒有-號
ForPos=200; //將ForPos設置成一個不可能的值
if(Pos>SubPos)
Pos=SubPos;
if(Pos>PluPos)
Pos=PluPos;
if(Pos>MulPos)
Pos=MulPos;
if(Pos>DivPos)
Pos=DivPos;
if(Pos>ForPos)
Pos=ForPos;
return Pos;
}
//---------------------------------------------------------------------------
//判斷最先出現的符號是+號、-號、*號還是/號
char CSusuanDlg::AnyFirstF(CString Str)
{
int SubPos=Str.Find('-')+1;
int PluPos=Str.Find('+')+1;
int MulPos=Str.Find('*')+1;
int DivPos=Str.Find('/')+1;
if(SubPos==0)
SubPos=200;
if(PluPos==0)
PluPos=200;
if(MulPos==0)
MulPos=200;
if(DivPos==0)
DivPos=200;
char Result='-';
int tempPos=SubPos;
if(PluPos<tempPos)
{
Result='+';
tempPos=PluPos;
}
if(MulPos<tempPos)
{
Result='*';
tempPos=MulPos;
}
if(DivPos<tempPos)
{
Result='/';
tempPos=DivPos;
}
return Result;
}
//---------------------------------------------------------------------------
//此計算用于計算不帶()號的乘方、加、減、乘、除運算
int CSusuanDlg::SubCompute(CString Str)
{
CString Middle="";
CString Mul2="";
CString Right="";
//定位第一個^號位置 ,計算乘方
int First=Str.Find("^")+1;
if(First) //循環計算乘方
{
CString tempStr=Str.Mid(0,First-1);
int temp=AnyLastPos(tempStr);
CString Left=Str.Mid(0,temp);
CString Mul1 =Str.Mid(temp,First-temp-1);
tempStr=Str.Mid(First,Str.GetLength()-First);
temp=AnyFirstPos(tempStr);
if(temp==200)
{
Mul2=tempStr;
Right="";
}
else
{
Mul2 =tempStr.Mid(0,temp-1);
Right=tempStr.Mid(temp-1,tempStr.GetLength()-temp+1);
}
Middle.Format("%d",(int)(pow(atof(Mul1),atof(Mul2))));
Str=Left+Middle+Right;
First=Str.Find("^")+1;
}
//定位第一個*號或/號的位置
int MulPos=Str.Find('*')+1;
int DivPos=Str.Find('/')+1;
First=MulPos;
if(MulPos>DivPos)
First=DivPos;
if(DivPos==0 && MulPos!=0)
{
First=MulPos;
DivPos=2000; // 將除號所在位置設置成一個大于MulPos但又不可能的值
}
if(DivPos!=0 && MulPos==0)
{
First=DivPos; // 將乘號所在位置設置成一個大于DivPos但不可能的值
MulPos=2000;
}
while(First) //循環計算乘、除
{
CString tempStr=Str.Mid(0,First-1);
int temp=AnyLastPos(tempStr);
CString Left=Str.Mid(0,temp);
CString Mul1 =Str.Mid(temp,First-temp-1);
tempStr=Str.Mid(First,Str.GetLength()-First);
temp=AnyFirstPos(tempStr);
if(temp==200)
{
Mul2=tempStr;
Right="";
}
else
{
Mul2 =tempStr.Mid(0,temp-1);
Right=tempStr.Mid(temp-1,tempStr.GetLength()-temp+1);
}
if(MulPos>DivPos)
Middle.Format("%d",(int)(strtod(Mul1.GetBuffer(Mul1.GetLength()),&stopstring)/strtod(Mul2.GetBuffer(Mul2.GetLength()),&stopstring)));
else
Middle.Format("%d",(int)(strtod(Mul1.GetBuffer(Mul1.GetLength()),&stopstring)*strtod(Mul2.GetBuffer(Mul2.GetLength()),&stopstring)));
Str=Left+Middle+Right;
MulPos=Str.Find('*')+1;
DivPos=Str.Find('/')+1;
First=MulPos;
if(MulPos>DivPos)
First=DivPos;
if(DivPos==0 && MulPos!=0)
{
First=MulPos;
DivPos=2000; // 將除號所在位置設置成一個大于MulPos但又不可能的值
}
if(DivPos!=0 && MulPos==0)
{
First=DivPos; // 將乘號所在位置設置成一個大于DivPos但不可能的值
MulPos=2000;
}
}
//定位+、-號首先出現的位置
First=AnyFirstPos(Str);
if(First==200)//如果沒有+、-號,則可以直接返回結果
return (int)strtod(Str.GetBuffer(Str.GetLength()),&stopstring);
char Fuhao=AnyFirstF(Str); //確定首先出現的符號是+號還是-號
while(First)
{//如果找到+號或-號
CString tempStr=Str.Mid(0,First-1);
int temp=AnyLastPos(tempStr);
CString Left=Str.Mid(0,temp);
CString Mul1 =Str.Mid(temp,First-temp-1);
tempStr=Str.Mid(First,Str.GetLength()-First);
temp=AnyFirstPos(tempStr);
if(temp==200)
{
Mul2=tempStr;
Right="";
}
else
{
Mul2 =tempStr.Mid(0,temp-1);
Right=tempStr.Mid(temp-1,tempStr.GetLength()-temp+1);
}
if(Fuhao=='+')
Middle.Format("%d",(int)(strtod(Mul1.GetBuffer(Mul1.GetLength()),&stopstring)+strtod(Mul2.GetBuffer(Mul2.GetLength()),&stopstring)));
else
Middle.Format("%d",(int)(strtod(Mul1.GetBuffer(Mul2.GetLength()),&stopstring)-strtod(Mul2.GetBuffer(Mul2.GetLength()),&stopstring)));
Str=Left+Middle+Right;
First=AnyFirstPos(Str);
if(First==200)
break;
Fuhao=AnyFirstF(Str);
}
return (int)strtod(Middle.GetBuffer(Middle.GetLength()),&stopstring);
}
//---------------------------------------------------------------------------
//用于計算表達式的結果
int CSusuanDlg::TotalCompute(CString Str)
{
int First=Str.ReverseFind('(')+1; //定位最后一個(號位置
while(First)
{
CString SubStr =Str.Mid(First,(Str.GetLength()-First));
int Last= SubStr.Find(')')+1;
Last+=First; //定位最后一個(號以后的最開始的)號位置
CString LeftStr=Str.Mid(0,First-1); //(號左邊的字符串
CString Middle =Str.Mid(First,Last-First-1); //()號中間的字符串
CString Right =Str.Mid(Last,Str.GetLength()-Last); //)號右邊的字符串
int Result=SubCompute(Middle); //進入下面的計算
Middle.Format("%d",Result);
Str=LeftStr+Middle+Right;
First=Str.ReverseFind('(')+1;
}
int Result=SubCompute(Str);
return Result;
}
void CSusuanDlg::OnTimer(UINT nIDEvent)
{
SpendTime++;
CString SSpendTime;
SSpendTime.Format("%d",SpendTime);
GetDlgItem(IDC_STATIC1)->SetWindowText("使用時間:"+SSpendTime+"秒");
// TODO: Add your message handler code here and/or call default
CDialog::OnTimer(nIDEvent);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -