1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
using System; using System.Collections.Generic; using System.Text; namespace Operation { /// <summary> /// 运算类 /// </summary> public class Operation { private double _numberA = 0; private double _numberB = 0; /// <summary> /// 数字A /// </summary> public double NumberA { get { return _numberA; } set { _numberA = value; } } /// <summary> /// 数字B /// </summary> public double NumberB { get { return _numberB; } set { _numberB = value; } } /// <summary> /// 得到运算结果 /// </summary> /// <returns></returns> public virtual double getResult() { double result = 0; return result; } } /// <summary> /// 加法类 /// </summary> class OperationAdd : Operation { public override double getResult() { double result = 0; result = NumberA + NumberB; return result; } } /// <summary> /// 减法类 /// </summary> class OperationSub : Operation { public override double getResult() { double result = 0; result = NumberA - NumberB; return result; } } /// <summary> /// 乘法类 /// </summary> class OperationMul : Operation { public override double getResult() { double result = 0; result = NumberA * NumberB; return result; } } /// <summary> /// 除法类 /// </summary> class OperationDiv : Operation { public override double getResult() { double result = 0; if (NumberB==0) throw new Exception("除数不能为0。"); result = NumberA / NumberB; return result; } } /// <summary> /// 平方类 /// </summary> class OperationSqr : Operation { public override double getResult() { double result = 0; result = NumberB * NumberB; return result; } } /// <summary> /// 平方根类 /// </summary> class OperationSqrt : Operation { public override double getResult() { double result = 0; if (NumberB < 0) throw new Exception("负数不能开平方根。"); result = Math.Sqrt(NumberB); return result; } } /// <summary> /// 相反数类 /// </summary> class OperationReverse : Operation { public override double getResult() { double result = 0; result = -NumberB; return result; } } /// <summary> /// 运算类工厂 /// </summary> class OperationFactory { public static Operation createOperate(string operate) { Operation oper = null; switch (operate) { case "+": { oper = new OperationAdd(); break; } case "-": { oper = new OperationSub(); break; } case "*": { oper = new OperationMul(); break; } case "/": { oper = new OperationDiv(); break; } case "sqr": { oper = new OperationSqr(); break; } case "sqrt": { oper = new OperationSqrt(); break; } case "+/-": { oper = new OperationReverse(); break; } } return oper; } } } |
本站所有文章、资源、如无特殊说明或标注互联网转载外, 均为本站以及特邀作者、注册会员、游客投稿原创发布. 所提供下载链接均为站外链接, 网站本身并不存储相关资源文件, 所有资源仅用于个人学习及研究使用, 请在24小时内删除, 切勿用于商业用途, 如产生法律纠纷本站概不负责, 任何个人或组织, 在未征得本站同意时, 禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台. 如若本站内容侵犯了原著者的合法权益, 请及时通知本站, 待核实后将在三个工作日内进行删除.