Alternative calculator in simple factory mode (just released source code)

Source: Internet
Author: User

As a friend in the jealousy group wrote a calculator, I made a special use of the simple factory model "coffee" I just learned. It felt so cool !! The emergence of the object-oriented design model is to save my messy code. Looking at my previous "Book of heaven", I feel impulsive !!!

The calculator mode is very simple and suitable for beginners to "practice ". The operation (operator) class should be written first, and the two private fields (operands A and B) should be "typed", and the virtual calculation method is enough. Encapsulate the operation base class of the calculator to inherit it infinitely. You only need to reconstruct the computing method of the operation class. My calculator inherits all the four arithmetic functions of the third-grade primary school! In addition, the functions of the founder and the n are also given !! But because I don't know the MR, Mc ...... What is it, so I just ignored it !!! It's easy to write!

Code:
  1. // Operation base class: (Visual Studio 2008 and sharpdevelop3.1 tips: Right-click the field and select Auto generate attribute)
  2. BLIC class operation
  3. {
  4. Private double _ numbera = 0;
  5. Public double numbera
  6. {
  7. Get {return _ numbera ;}
  8. Set {_ numbera = value ;}
  9. }
  10. Private double _ numberb = 0;
  11. Public double numberb
  12. {
  13. Get {return _ numberb ;}
  14. Set {_ numberb = value ;}
  15. }
  16. Public Virtual double getresult ()
  17. {
  18. Double result = 0;
  19. Return result;
  20. }
  21. }

 

The fourth arithmetic derived class (addition) in the third grade inherits from operation. You only need to reconstruct the getresult method:

Code:
  1. Class Add: Operation
  2. {
  3. Public override double getresult ()
  4. {
  5. Double result = 0;
  6. Result = numbera + numberb;
  7. Return result;
  8. }
  9. }


Simple factory mode class, like a factory pipeline operation, allocates the received operator (operator) parameters to the corresponding operation subclass:

Code:
  1. Public class factory
  2. {
  3. Public static operation createoperate (string operate)
  4. {
  5. Operation operator = NULL;
  6. Switch (operate)
  7. {
  8. Case "+ ":
  9. Sums = new add ();
  10. Break;
  11. Case "-":
  12. Vertex = new sub ();
  13. Break;
  14. Case "*":
  15. Cursor = new MUL ();
  16. Break;
  17. Case "/":
  18. Cursor = new Div ();
  19. Break;
  20. Case "SQRT ":
  21. Response = new mysqrt ();
  22. Break;
  23. Case "pow ":
  24. Topology = new MYPOW ();
  25. Break;
  26. }
  27. Return response;
  28. }


After reading the above code, I feel that it is very useful to learn the object-oriented design model !!!

Next, show me my "alternative" Calculator:

After the program is started, it gradually appears:

Isn't that cool !! Since the double type of C # can bear very scary data, the fourth arithmetic of the third grade is no problem completely !!!

If you accidentally click, and these keys, they may appear, along with the music, of course, more likely, "they're coming, we are their slaves!" :

Some may ask, but I did not see the red cross. Believe it or not ~~~

The source code has been uploaded to the Network Disk:

Www.rayfile.com/files/e266d9e8-8708-11de-9ac4-0014221f469f/

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.