C # simple calculator design

Source: Internet
Author: User

(1) category design:
1. Operator Enumeration
Enum caltypewithtwounit
Add sub multi Div 5512915179

Enum caltypewithoneunit
Twice (square) SQRT (open) daoshu (reciprocal) OPP (reverse)

2. Subclass
Inherited from the parent class calactionbase
In this parent class, all sub-operators must perform operations.
Validate verifies the validity of the operand
The operation type is converted and then calculated.
The calculate abstract method is implemented in each subclass.

3. Interface icalculator
Method: pushnum pushcal backaword (undefined) reset (reset)
Clearoneoppend (clear current operand)
Attribute: The result read-only format has only one get Method for external calls.

4. Interface implementation class Calculator
Inherited interface icalculator
For specific calculation, there must be a calactionbase reference _ Action
_ Action: The calculate method can be called, while the calactionbase method is an abstract method.
Therefore, when _ Action instantiates objects of different subclasses, you can call the implementation body of the calculate abstract method in the subclass-Polymorphism @@@@@@

(2) Business logic:
1. Call in form1:
Define an interface _ ical = new calculator (); then, _ ical can call all methods and attributes of the interface

2. Analyze the functions of the above classes again, and it is not difficult to find out
Each operation subclass is only an operation for specific calculation behaviors.
Calactionbase, as the base class, is only an overall arrangement of each suboperation and can only contain public operations in some specific operations.
There is naturally no implementation of the method body in the interface.
The calculation logic of the subject will naturally be in calculator.

3. Analyze the attributes and fields in Calculator
(1) Reference _ action of a calactionbase class. This must exist, and the reason will not be repeated.
(2) string _ strexp of an overall arithmetic expression
Set a get Method for external calls to display all input
Also used for the clear operation (c)
(3) an operand string _ strnum
Used for clearoneoppend operations (CE) and backaword operations
Set a get Method for external calls to display the current input.

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.