使用vs2005 (语言C#)设计一个简单的计算器 代码如何编写呢 详细

 我来答
原时轻w
2014-07-11 · TA获得超过433个赞
知道答主
回答量:184
采纳率:0%
帮助的人:134万
展开全部
//定义接口成员 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!");

麻烦采纳,谢谢!
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式