完成计算器:
仅利用面向对象的封装特性,完成计算器。
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Calculate { class Program { static void Main(string[] args) { Console.Write("请输入数字A:"); string strNumberA = Console.ReadLine(); Console.Write("请选择运算符号(+,-,*,/):"); string strOperate = Console.ReadLine(); Console.WriteLine("请输入数字B:"); string strNumberB = Console.ReadLine(); string strResult = ""; strResult = Convert.ToString(Operation.GerResult(Convert.ToDouble(strNumberA),Convert.ToDouble(strNumberB),strOperate)); Console.WriteLine("结果是:"+strResult); Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Calculate { public class Operation { public static double GerResult(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; default: break; } return result; } } }
使用封装、继承和多态来完成计算器。