Comparison between process-oriented implementation and object-oriented implementation of a simple calculator

Source: Internet
Author: User

1) First, a simple calculator is computed using C # process-oriented Code. The Code is as follows:

Try {console. writeline ("Enter the number A:"); string numbera = console. readline (); console. writeline ("Enter the calculated character" +,-, *,/""); string operater = console. readline (); console. writeline ("Enter the number B:"); string numberb = console. readline (); double strrusult = 0; If (operater = "+") {strrusult = convert. todouble (numbera) + convert. todouble (numbera);} else if (operater = "-") {strrusult = convert. todouble (numbera)-Conv ERT. todouble (numbera);} else if (operater = "*") {strrusult = convert. todouble (numbera) * convert. todouble (numbera);} else if (operater = "/") {If (convert. todouble (numberb )! = 0) {strrusult = convert. todouble (numbera)/convert. todouble (numbera);} else {Throw new exception ("the divisor cannot be zero! ") ;}} Console. writeline ("computer is" + strrusult. tostring (); console. readkey ();} catch (exception ex) {Throw new exception (ex. message, ex );}

The disadvantage in this process is that in each calculation process, if statements must be verified for each operator, and the Code cannot be reused, which is very cumbersome, it is not conducive to later function addition and maintenance.

2) use the idea of an image object to block the verification and method so that the presentation layer and the logic operation code layer are separated and then called at the main operation layer. The Code is as follows:

First, define an operation class. The Code is as follows:

Namespace object-oriented calculator {class operation {public static double getresult (double numbera, double numberb, string operate) {double result = 0d; Switch (operate) {Case "+ ": {result = numbera + numberb;} break; Case "-": {result = numbera-numberb;} break; Case "*": {result = numbera * numberb;} break; case "/": {If (numberb! = 0) {result = numbera/numberb;} else {console. writeline ("the divisor cannot be zero! ") ;}} Break;} return result ;}}}

After defining the corresponding class, call it on the main interface. The Code is as follows:

Try {console. writeline ("Enter the number A:"); string numbera = console. readline (); console. writeline ("Enter the calculated character" +,-, *,/""); string operater = console. readline (); console. writeline ("Enter the number B:"); string numberb = console. readline (); string strrusult = ""; strrusult = convert. tostring (operation. getresult (convert. todouble (numbera), convert. todouble (numberb), operater); console. writeline ("computer is" + strrusult); console. readkey ();} catch (exception ex) {Throw new exception (ex. message, ex );}

In this way, the operator verification is encapsulated, and the switch statement is used to verify the operator. You do not need to verify each operator each time, saving code and reusing code.

In addition, the computation of a simple calculator that fully embodies the Object Concept is to define a computing class with the corresponding computing attributes and the virtual method of class computing. Define the corresponding computing classes respectively, and then call the corresponding classes during computing, and rewrite the methods included in the class to implement, encapsulate, inherit, and polymorphism,

Code structure:

The Code is as follows:

Operation1.cs parent class. Other Calculation Methods inherit from this class,

Namespace object-oriented calculator {class operation1 {private double _ numbera = 0; private double _ numberb = 0; Public double numbera {get {return _ numbera ;} set {_ numbera = value ;}} public double numberb {get {return _ numberb;} set {_ numberb = value ;}} Public Virtual double getresult () {double result = 0; return result ;}}}

Operationadd. CS class implements addition operation:

Namespace object-oriented calculator {class operationadd: operation1 {public override double getresult () {double result = 0; Result = numbera + numberb; return result ;}}}

Operationsub. CS class implements subtraction:

Namespace object-oriented calculator {class operationsub: operation1 {public override double getresult () {double result = 0; Result = numbera-numberb; return result; // return base. getresult ();}}}

Operationmul. CS class for multiplication:

Namespace object-oriented calculator {class operationmul: operation1 {public override double getresult () {double result = 0; Result = numbera * numberb; return result; // return base. getresult ();}}}

Opetiondiv. CS class implements division operation:

Namespace object-oriented calculator {class opetiondiv: operation1 {public override double getresult () {double result = 0; If (numberb! = 0) {result = numbera/numberb;} else {// throw new exception ("the divisor cannot be zero! "); Console. writeline (" Division cannot be zero! ") ;}Return result; // return base. getresult ();}}}

Operationfactory. CS class to determine which class the main function should call for calculation:

Namespace object-oriented calculator {class operationfactory {public static operation1 creatoperation (string operate) {operation1 operator = NULL; Switch (operate) {Case "+ ": {operator = new operationadd (); break;} case "-": {operator = new operationsub (); break;} case "*": {operator = new operationmul (); break;} case "/": {response = new opetiondiv (); break ;}} return response ;}}}

The main function is called:

Operation1 operator = new operation1 (); operator = operationfactory. creatoperation ("+"); operator. numbera = 1; Numeric. numberb = 2; double result = random. getresult (); console. writeline ("Calculation Result:" + result); console. readkey ();

Finally, the idea of encapsulation, inheritance, and polymorphism is fully used. Each piece of code can be reused, and each part is separated into blocks without affecting each other.

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.