⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 susuan~1.cpp

📁 趣味编程速算24.rar
💻 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 + -