C#上机练习:实现2个数的加减乘除取余计算;下面是图。还有 用if语句些 不要其他的语句。

 我来答
wangbenson
2011-04-25 · TA获得超过161个赞
知道答主
回答量:283
采纳率:0%
帮助的人:192万
展开全部
看看大话设计模式第一章
Codes:
1. Operation.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace OperationLibrary
{
/// <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>
/// <param name="currentNumber"></param>
/// <param name="inputString"></param>
/// <returns></returns>
public static string checkNumberInput(string currentNumber, string inputString)
{
string result = "";
if (inputString == ".")
{
if (currentNumber.IndexOf(".") < 0)
{
if (currentNumber.Length == 0)
result = "0" + inputString;
else
result = currentNumber + inputString;
}
}
else if (currentNumber == "0")
{
result = inputString;
}
else
{
result = currentNumber + inputString;
}

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>
public 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;
}
}

}

2. 控制台程序
using System;
using System.Collections.Generic;
using System.Text;
using OperationLibrary;

namespace 计算器控制台
{

class Program
{
static void Main(string[] args)
{
while (true)
{
try
{
Console.Write("请输入第一个操作数:");
string strNumberA = Console.ReadLine();
Console.Write("请选择运算符号(+、-、*、/):");
string strOperate = Console.ReadLine();
Console.Write("请输入第二个操作数:");
string strNumberB = Console.ReadLine();
string strResult = "";

Operation oper;
oper = OperationFactory.createOperate(strOperate);
oper.NumberA = Convert.ToDouble(strNumberA);
oper.NumberB = Convert.ToDouble(strNumberB);
strResult = oper.GetResult().ToString();

Console.WriteLine("结果是:" + strResult);

Console.WriteLine();
Console.WriteLine("请按任意键继续...");
string inputStr = Console.ReadLine();
if (inputStr.ToUpper() == "exit".ToUpper())
break;
}
catch (Exception ex)
{
Console.WriteLine("您的输入有错:" + ex.Message);
}
}
}
}

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

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式