【Basic computer】------ Calculator

原创 2016年08月29日 13:14:56


【Basic computer】------ Calculator



Reuse VS copy

  
【Copy】: copy is pretty to all of us, just Ctrl+c and Ctrl+v;


【Reuse】: the same thing you can use it for many time .


【comparison】:for example, you want to use a car, I already  have a car, but you do not, you want use the same car as mine, copy is that you go to build a car as my 100% but reuse is different ,you just borrow the car from me and use it .

   So which is better, while we are program which is for sure that reuse is much better, because reuse is less code but copy which means you gonna to use more workspace and program more.


    



Tight coupling  VS  loose coupling


      【Tight coupling】:which is mean each function have strongly connection to themselves and all the codes at the same winform.


      【loose coupling】:there have many winforms. Each windform has their own method and just finish the things themselves. For example: three level.


     【comparison】:for example, you have a restaurant, at the first period maybe there just you alone to running it , you to service people, you go to buy the demand goods and you cook the food for customers by yourself. All these three thing you do it by you alone this what we call it Tight coupling;

  

        But what is Loose coupling? One day you restaurant is became more popular and you hire two people, person A going to service the Customers; person B is going to input some demand goods like oil, vegetables , salt and so on. So right now you are upgrade, that is mean different people have their own business which means everyone in your restaurant have clear duty and you restaurant will make more and more efficient for sure. As the same while we are designer the software and coding the code where we learn it from the real life around us.

  

【Calculator】

       First time we code all the codes on the same class which we call it tight coupling, but we want to make it more efficient ,so we changed to loose coupling. We separate  class “Program” to two classes as “Operation” and ”Program”.

                    
    




The detail codes:

 【Operation】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Caculator
{
    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 "/":
                    result = numberA / numberB;
                    break;
            }
            return result;
        }
    }
}



【program】

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Caculator
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.Write("Pleaser input NumberA: ");
                string strNumberA = Console.ReadLine();
                Console.Write("plsase input a operational sign(+、—、*、/):  ");
                string strOperate = Console.ReadLine();
                Console.Write("Pleaser input NumberB: ");
                string strNuberB = Console.ReadLine();
                string strResult = "";
                strResult = Convert.ToString(Operation.GetResult(Convert.ToDouble(strNumberA), Convert.ToDouble(strNuberB), strOperate));
                Console.WriteLine("The result is:" + strResult);
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("You are wrong:" + ex.Message);
            }
        }
    }
}




The result show: 



 
 


 







版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

Basic Calculator & Basic Calculator II

Basic Calculator Description: Implement a basic calculator to evaluate a simple expression s...

Leetcode之Basic Calculator & Basic Calculator II

Leetcode之basic calculator、basic calculator II

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

Basic Calculator

 题目描述:Implement a basic calculator to evaluate a simple expression string. The expression string...

Basic Calculator

Implement a basic calculator to evaluate a simple expression string. The expression string may cont...

Basic Calculator

题目描述: The expression string may contain open ( and closing parentheses ), the plus + or minus s...

Basic Calculator

该题的思路很明确就是将中缀表达式转换为后缀表达式,然后通过后缀表达式来求值。 class Solution { public: int calculate(string s) { ...

Basic Calculator

这道题目真是花了九牛二虎之力才做对。。。= = 先转成逆波兰式再计算。。。不多说了,直接上代码:public class Solution { public static int calcul...

Basic Calculator

题目描述: Implement a basic calculator to evaluate a simple expression string. The expression st...

【Leetcode】Basic Calculator II

题目链接:https://leetcode.com/problems/basic-calculator-ii/题目: Implement a basic calculator to evaluat...

[LeetCode]Basic Calculator II

解题思路: 1,就是要写个简单的计算器呗 2,先把所有的 space去掉, 实现nonSpace方法; 3,把所有的 operator 和 operands分开,实现简单的 splitToOpe...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)