读林锐博士的《高质量c++/c编程指南》有感

       1. 简单的WINDOWS 应用程序命名规则

                 类名和函数名用大写字母开头的单词组合而成。
                 例如:
                 class Node; // 类名
                 class LeafNode; // 类名
                 void Draw(void); // 函数名
                 void SetValue(int value); // 函数名
                

                变量和参数用小写字母开头的单词组合而成。
                 例如:
                 BOOL flag;
                 int drawMode;
                

               常量全用大写的字母,用下划线分割单词。
              例如:
              const int MAX = 100;
              const int MAX_LENGTH = 100;
              

             静态变量加前缀s_(表示static)。
             例如:
             void Init(…)
             {
                  static int s_initValue; // 静态变量
                   …

             }
            

           如果不得已需要全局变量,则使全局变量加前缀g_(表示global)。
          例如:
           int g_howManyPeople; // 全局变量
           int g_howMuchMoney; // 全局变量
       

            类的数据成员加前缀m_(表示member),这样可以避免数据成员与成员函数的参数同名。
              例如:
              void Object::SetValue(int width, int height)
                {
                    m_width = width;
                    m_height = height;
                }

      林锐的高质量C/C++编程指南要求我们遵循以下规则:
          (1)为了防止头文件被重复引用,应当用ifndef/define/endif结构产生预处理块。
          (2)用 #include <filename.h> 格式来引用标准库的头文件(编译器将从标准库目录开始搜索)。
          (3)用 #include “filename.h” 格式来引用非标准库的头文件(编译器将从用户的工作目录开始搜索)。
          (4)在每个类声明之后、每个函数定义结束之后都要加空行。
          (5)在一个函数体内,逻揖上密切相关的语句之间不加空行,其它地方应加空行分隔。
          (6)一行代码只做一件事情,如只定义一个变量,或只写一条语句。这样的代码容易阅读,并且方便于写注释。
          (7)if、for、while、do等语句自占一行,执行语句不得紧跟其后。不论执行语句有多少都要加{}。这样可以防止书写失误。
          (8)程序的分界符‘{’和‘}’应独占一行并且位于同一列,同时与引用它们的语句左对齐。
          (9)程序中不要出现标识符完全相同的局部变量和全局变量,尽管两者的作用域不同而不会发生语法错误,但会使人误解。
          (10)a.不可将布尔变量直接与TRUE、FALSE或者1、0进行比较。

 

 

voidCCalculatorDlg::OnPaint()

{

    if (IsIconic())

    {

        CPaintDC dc(this); // device context forpainting

 

        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();

    }

}

 

 

 

 

voidCCalculatorDlg::OnAdd()

{

    // TODO: Add your control notificationhandler code here

    Calculate();

    m_operator = OpAdd;

}

 

voidCCalculatorDlg::OnMinus()

{

    // TODO: Add your control notificationhandler code here

    Calculate();

    m_operator = OpSubtract;

}

 

voidCCalculatorDlg::OnDivid()

{

    // TODO: Add your control notificationhandler code here

    Calculate();

    m_operator = OpDivide;

}

 

voidCCalculatorDlg::OnMultiply()

{

    // TODO: Add your control notificationhandler code here

    Calculate();

    m_operator = OpMultiply;

}

 

void CCalculatorDlg::OnEqual()

{

    // TODO: Add your control notificationhandler code here

    Calculate();

    m_operator = OpNone;

}

 

voidCCalculatorDlg::OnSign()

{

    // TODO: Add your control notificationhandler code here

    m_operand*=-1;

    UpdateDisplay();

}

 

void CCalculatorDlg::OnSqrt()

{

    // TODO: Add your control notificationhandler code here

    m_func=FuncSqrt;

    Run_Func();

}

 

voidCCalculatorDlg::OnReciprocal()

{

    // TODO: Add your control notificationhandler code here

    m_func=FuncRec;

    Run_Func();

}

 

voidCCalculatorDlg::OnPoint()

{

    // TODO: Add your control notificationhandler code here

    m_bCoff=1;

 

    UpdateDisplay();

}

 

voidCCalculatorDlg::OnClear()

{

    // TODO: Add your control notificationhandler code here

    m_operator = OpNone;

    m_operand = 0;

    m_accum = 0;

    m_bOperandAvail = FALSE;

    m_errorState = ErrNone;

    m_coff=0.1;

    UpdateDisplay();

}

 

 

voidCCalculatorDlg::OnLog()

{

    // TODO: Add your control notificationhandler code here

    m_func=FuncLog;

    Run_Func();

}

 

voidCCalculatorDlg::OnLn()

{

    // TODO: Add your control notificationhandler code here

    m_func=FuncLn;

    Run_Func();

}

 

voidCCalculatorDlg::OnFactorial()

{

    // TODO: Add your control notificationhandler code here

    m_func=FuncN;

    Run_Func();

}

 

voidCCalculatorDlg::OnExp()

{

    // TODO: Add your control notificationhandler code here

    m_func=FuncExp;

    Run_Func();

}

 

voidCCalculatorDlg::OnSentific()

{

    // TODO: Add your control notificationhandler code here

    m_bIsExpand=1;

    m_sentific=0;

    ExpandToSentific();

}

 

voidCCalculatorDlg::OnSin()

{

    // TODO: Add your control notificationhandler code here

    m_func=FuncSin;

    Run_Func();

}

 

voidCCalculatorDlg::OnSquar()

{

    // TODO: Add your control notificationhandler code here

    m_func=FuncSqre;

    Run_Func();

}

 

voidCCalculatorDlg::OnTan()

{

    // TODO: Add your control notificationhandler code here

    m_func=FuncTan;

    Run_Func();

}

 

voidCCalculatorDlg::OnStandard()

{

    // TODO: Add your control notificationhandler code here

    m_bIsExpand=0;

    m_sentific=1;

    ExpandToSentific();

}

 

void CCalculatorDlg::OnDegree()

{

    // TODO: Add your control notificationhandler code here

    m_isDegree=0;

    DtoR=(2*PI)/360;

    UpdateData(FALSE);

}

 

voidCCalculatorDlg::OnRad()

{

    // TODO: Add your control notificationhandler code here

    m_isDegree=1;

    DtoR=1;

    UpdateData(TRUE);

}

 

voidCCalculatorDlg::OnCos()

{

    // TODO: Add your control notificationhandler code here

    m_func=FuncCos;

    Run_Func();

}

 

 

 

8.CCalculatorDlg.hpublic下添加以下的成员变量

      floatm_operand;

       float m_accum;

       BOOL m_bCoff;

       float m_coff;

       float DtoR;

      

       BOOL m_bIsExpand;

       CWnd* m_pMark;

       CRect rcSentific;

       CRect rcStandard;

 

       Operator m_operator;

       Func m_func;

       CalcError m_errorState;

       BOOLm_bOperandAvail;

 

       voidCalculate();

       voidRun_Func();

       voidExpandToSentific();

       voidSetVisibleCtrl();

       voidUpdateDisplay()

10.为上面函数添加如下代码。

void CCalculatorDlg::OnOperandInput(UINTnID)

{

       ASSERT(nID>= IDC_0 && nID <= IDC_9);

 

 

              return;

 

       if(m_func!=FuncNone)

       {

              m_func=FuncNone;

              Calculate();

       }

 

       if(!m_bOperandAvail)

              m_operand= 0;

 

   if(!m_bCoff)

              m_operand=m_operand*10+(nID-IDC_0);

       else

       {

              m_operand=m_operand+(nID-IDC_0)*m_coff;

              m_coff*=0.1;

       }

 

       m_bOperandAvail=TRUE;

       UpdateDisplay();

}

 

void CCalculatorDlg::Calculate()

{

       if(m_errorState != ErrNone)

              return;

 

       if(m_bOperandAvail)

       {

              if(m_operator == OpNone)

                     m_accum= m_operand;

              elseif (m_operator == OpMultiply)

                     m_accum*= m_operand;

              elseif (m_operator == OpDivide)

              {

                     if(m_operand == 0)

                            m_errorState= ErrDivideByZero;

                     else

                            m_accum/= m_operand;

              }

              elseif (m_operator == OpAdd)

                     m_accum+= m_operand;

              elseif (m_operator == OpSubtract)

                     m_accum-= m_operand;

       }

 

       m_bOperandAvail= FALSE;

       m_bCoff=0;

       m_coff=0.1;

       UpdateDisplay();

}

 

void CCalculatorDlg::UpdateDisplay()

{

       if(GetSafeHwnd() == NULL)

              return;

 

       if(m_errorState != ErrNone)

              m_result="除数不能为零";

       else

       {

              floatlVal = (m_bOperandAvail) ? m_operand : m_accum;

              m_result.Format(_T("%f"),lVal);

              inti=m_result.GetLength();

              while(m_result.GetAt(i-1)=='0')

              {

                     m_result.Delete(i-1,1);

                     i-=1;

              }

       }

       UpdateData(FALSE);

}

 

BOOL CCalculatorDlg::Keyboard(LPCTSTRszButton)

{

       switch(szButton[0])

       {

       case'c':

       case'C':

              OnClear();

              break;

 

       case'/':

              OnDivid();

              break;

       case'+':

              OnAdd();

              break;

       case'-':

              OnMinus();

              break;

       case'*':

              OnMultiply();

              break;

       case'=':

              OnEqual();

              break;

 

       default:

              if(szButton[0] >= '0' && szButton[0] <= '9')

              {

                     if(m_errorState != ErrNone)

                       return FALSE;

 

               if (!m_bOperandAvail)

                       m_operand = 0;

 

               m_operand=m_operand*10+szButton[0] -'0';

               m_bOperandAvail=TRUE;

               UpdateDisplay();

              }

              else

                     returnFALSE;

              break;

       }

       returnTRUE;

}

 

 

BOOLCCalculatorDlg::PreTranslateMessage(MSG* pMsg)  //位于中间

{

       if(m_hAccel != NULL && TranslateAccelerator(m_hWnd, m_hAccel, pMsg))

              returnTRUE;

 

       returnCDialog::PreTranslateMessage(pMsg);

}

 

void CCalculatorDlg::Run_Func()

{

       if(m_errorState != ErrNone)

              return;

 

       if(m_bOperandAvail)

       {

              if(m_func==FuncExp)

                     m_operand=exp(m_operand);

              if(m_func==FuncLn)

                     m_operand=log(m_operand);

              if(m_func==FuncLog)

                     m_operand=log10(m_operand);

              if(m_func==FuncSqrt)

                     m_operand=sqrt(m_operand);

              if(m_func==FuncSqre)

                     m_operand=pow(10,m_operand);

              if(m_func==FuncSin)

                     m_operand=sin(m_operand*DtoR);

              if(m_func==FuncCos)

                     m_operand=cos(m_operand*DtoR);

              if(m_func==FuncTan)

                     m_operand=tan(m_operand*DtoR);

              if(m_func==FuncRec)

                     m_operand=1/m_operand;

              if(m_func=FuncN)

              {

                     inti;

               for(i=m_operand-1;i>=1;i--)//中间

                  m_operand*=i;

              }

       }

       UpdateDisplay();

 

//     m_func=FuncNone;

}

 

void CCalculatorDlg::ExpandToSentific()

{

       if(m_bIsExpand)

       {

              SetWindowPos(NULL,0,0,rcSentific.Width(),rcSentific.Height(),

                     SWP_NOMOVE|SWP_NOZORDER);

              SetVisibleCtrl();

       }

       else

       {

              SetWindowPos(NULL,0,0,rcStandard.Width(),rcSentific.Height(),

                     SWP_NOMOVE|SWP_NOZORDER);

              SetVisibleCtrl();

       }

       UpdateData(FALSE);

}

 

void CCalculatorDlg::SetVisibleCtrl()

{

       CWnd*pCtrl=GetWindow(GW_CHILD);

       CRectrcTest;

       CRectrcControl;

       CRectrcShow;

 

       GetWindowRect(rcShow);

 

       while(pCtrl!=NULL)

       {

              pCtrl->GetWindowRect(rcControl);

 

              if(rcTest.IntersectRect(rcShow,rcControl))

                     pCtrl->EnableWindow(TRUE);

              else

                     pCtrl->EnableWindow(FALSE);

 

              pCtrl=pCtrl->GetWindow(GW_HWNDNEXT);

       }

}
(1)知错就改;
(2)经常温故而知新;
(3)坚持学习,天天向上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值