使用vs2005 (语言C#)设计一个简单的计算器 代码如何编写呢 详细
展开全部
//定义接口成员 public interface Ipeople { /// <summary> /// NumberA /// </summary> double NumberA { get; set; } /// <summary> /// NumberB /// </summary> double NumberB { get; set; } double GetResult(); } /// <summary> /// 抽象运算基类 /// </summary> public abstract class Operator:Ipeople { private double m_NumberA; private double m_NumberB; /// <summary> /// NumberA /// </summary> public double NumberA { get { return m_NumberA; } set { m_NumberA = value; } } /// <summary> /// NumberB /// </summary> public double NumberB { get { return m_NumberB; } set { m_NumberB = value; } } /// <summary> /// 定义虚方法用来重写返回结果 /// </summary> /// <returns>Result</returns> public virtual Double GetResult() { double Result = 0; return Result; } } /// <summary> /// 加法运算 /// </summary> public class Add : Operator { /// <summary> /// 重写抽象基类虚方法 /// </summary> /// <returns></returns> public override double GetResult() { double Result = 0; Result = NumberA + NumberB; return Result; } } /// <summary> /// 减法运算 /// </summary> public class Sub : Operator { /// <summary> /// 重写抽象基类虚方法 /// </summary> /// <returns></returns> public override double GetResult() { double Result = 0; Result = NumberA - NumberB; return Result; } } /// <summary> /// 乘法运算 /// </summary> public class Mult : Operator { /// <summary> /// 重写抽象基类虚方法 /// </summary> /// <returns></returns> public override double GetResult() { double Result = 0; Result = NumberA * NumberB; return Result; } } /// <summary> /// 除法运算 /// </summary> public class Div : Operator { /// <summary> /// 重写抽象基类虚方法 /// </summary> /// <returns></returns> public override double GetResult() { double Result = 0; if (NumberB != 0) { Result = NumberA / NumberB; } else { //抛出异常 throw new Exception("除数不能为0!");
麻烦采纳,谢谢!
麻烦采纳,谢谢!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询