【Basic computer】------ Calculator
来源:互联网 发布:博罗县网络问政平台 编辑:程序博客网 时间:2024/05/16 08:50
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
【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 computer】------ Calculator
- Basic Calculator
- Basic Calculator
- Basic Calculator
- Basic Calculator
- Basic Calculator
- Basic Calculator
- Basic Calculator
- Basic Calculator
- Basic Calculator
- Basic Calculator
- Basic Calculator
- Basic Calculator
- Basic Calculator
- Basic Calculator
- Basic Calculator
- Basic Calculator
- Basic Calculator
- zTree使用心得
- Mysql 状态字段转中文描述 case then
- 详解intel处理器命名规则
- 【OpenCV入门教程之四】 ROI区域图像叠加&初级图像混合 全剖析
- eclipse优化
- 【Basic computer】------ Calculator
- 安卓TTS文字转语音
- A*寻路算法
- XMLDocument转成字符串[javascript]
- 内存——Android中常见的内存泄漏
- 传输层安全协议TLS/SSL
- 关于利用oracle自带功能防止SQL注入的方法
- 基于C语言tftp服务器与客户端实现
- 如何在spring security避免登录页面素材文件被禁止加载