Write a simple calculator with the Win32 SDK

Source: Internet
Author: User

Using MFC to do a calculator is very easy, we all think that using the SDK is very difficult, but I think as long as your thinking is correct, do it is as handy. Because the SDK does the project also has the law to be able to seek, is establishes the frame and then carries on the message processing to the frame. So I learn VC + + one months to make up my first small program, here and you VC enthusiasts Exchange and exchange.

Before you make a calculator, you should first understand the following two functions: string conversion to double and double precision to string. The string is converted to a double strtod (const char *nptr, char **endptr), in which nptr represents the string to be converted, endptr is expressed to a word that is not a number characters stops the scan, and the double is converted to a string function of char *_ GCVT (double value, int digits, char *buffer), where value represents the numeric value to be converted to a string, Digts represents a meaningful number of digits, and buffer represents the buffers used to store strings. Understanding these two functions, the calculator's organization began to have a certain understanding.

Here's a description of the calculator steps:

1. Write a master function that can be said to be stereotyped.

2. Message handling This section is the hardest and most important part of this section, which includes creating buttons, text boxes, and the ability to push each button down. Create buttons and text boxes, and so on some of the appearance of the steps, if you want detailed code to see the source program.

3. The number key is represented by a function:

//------按下数字键(0~9和小数点)的操作处理函数------
void NumResult(char *NumData)
{
  if (nOptF==0)
   SetWindowText(hEditResult,""); //hEditResult表示文本框
  nMax=GetWindowTextLength(hEditResult)+1;
  GetWindowText(hEditResult,lpszAddItem,nMax); //用lpszAddItembr存储字符串
  strcat(lpszAddItem,NumData); ///字符串加该数字键的字符
  //lpszOpt表示是否按下操作符键,
  //如是没有按过就赋值为“N”;
  //如是按了就赋值为该操作符
  if (strcmp(lpszOpt,"N")==0)
  {
   strcpy(lpszResult1,lpszAddItem);
   SetWindowText(hEditResult,lpszResult1);
  }
  else
  {
    strcpy(lpszResult2,lpszAddItem);
    SetWindowText(hEditResult,lpszResult2);
  }
  nOptF=1; //按下了数字键
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.