用代码实现计算器类,实现两个数的加、减、乘、除运算
发布网友
发布时间:2022-10-25 16:53
我来回答
共1个回答
热心网友
时间:2023-10-03 14:51
【计算器】
功能 实现输入两个数字与运算符自动运算
运算类父类
public class Operation
{
private double _numberA = 0;//要运算的数字
private double _numberB = 0;
public double NumberA//运算数字的方法
{
get {return _numberA;}
set {_numberA = value;}
}
public double NumberB
{
get {return _numberB;}
set {_numberB = value;}
}
public virtual double GetResult()//等着让子类重写(延迟到子类重写)
{
double result = 0;
return result;
}
}
加减乘除子类
class OperationAdd : Operation//加法运算类
{
public override double GetResult()
{
double result = 0;
reslut = NumberA + NumberB;//运算
return result;//返回结果
}
}
class Operationsub : Operation
{
public override double GetResult()
{
double result = 0;
reslut = NumberA - NumberB;
return result;
}
}
class OperationMul : Operation
{
public override double GetResult()
{
double result = 0;
reslut = NumberA * NumberB;
return result;
}
}
class OperationDiv : Operation
{
public override double GetResult()
{
double result = 0;
reslut = NumberA / NumberB;
return result;
}
}
内部运算工厂类实现
interface Ifactory//工厂类接口
{
Operation CreateOperation();
}
class AddFactory : Ifactory//需要哪种运算就实例化哪个运算类
{
public Operation CreateOperation()
{
return new OperationAdd();
}
}
class SubFactory : Ifactory
{
public Operation CreateOperation()
{
return new OperationSub();
}
}
class MulFactory : Ifactory
{
public Operation CreateOperation()
{
return new OperationMul();
}
}
class DivFactory : Ifactory
{
public Operation CreateOperation()
{
return new OperationDiv();
}
}
客户端代码
Ifactory operFactory = new AddFatory();//要什么运算就生成什么运算
Operation oper = operFactory.createOperation();
oper.NumberA = 1;
oper.NumberB = 2;
double result =oper.GetResult();
结构化程序设计客户端写法
class program
{
static void Main(string[] ards)
{
string strNumberA = Console.ReadLine();
string strOperate = Console.Readline();
string strNumberB = Console.ReadLine();
string strResult = "";
switch (strOperate)
{
case "+"
strResult = Convert.Tostring(Convert.Todouble(strNumberA)) + Convert.ToDouble(strNumberB));
break;
case "-"
strResult = Convert.Tostring(Convert.Todouble(strNumberA)) - Convert.ToDouble(strNumberB));
break;
case "*"
strResult = Convert.Tostring(Convert.Todouble(strNumberA)) * Convert.ToDouble(strNumberB));
break;
case "/"
strResult = Convert.Tostring(Convert.Todouble(strNumberA)) / Convert.ToDouble(strNumberB));
break;
}
console.witeLine("结果是:" + strResult);
console.ReadLine();
}
}
##########################################################
PS:C#版代码。要是还有不懂之处可以Q我
热心网友
时间:2023-09-11 13:54
【计算器】
功能 实现输入两个数字与运算符自动运算
运算类父类
public class Operation
{
private double _numberA = 0;//要运算的数字
private double _numberB = 0;
public double NumberA//运算数字的方法
{
get {return _numberA;}
set {_numberA = value;}
}
public double NumberB
{
get {return _numberB;}
set {_numberB = value;}
}
public virtual double GetResult()//等着让子类重写(延迟到子类重写)
{
double result = 0;
return result;
}
}
加减乘除子类
class OperationAdd : Operation//加法运算类
{
public override double GetResult()
{
double result = 0;
reslut = NumberA + NumberB;//运算
return result;//返回结果
}
}
class Operationsub : Operation
{
public override double GetResult()
{
double result = 0;
reslut = NumberA - NumberB;
return result;
}
}
class OperationMul : Operation
{
public override double GetResult()
{
double result = 0;
reslut = NumberA * NumberB;
return result;
}
}
class OperationDiv : Operation
{
public override double GetResult()
{
double result = 0;
reslut = NumberA / NumberB;
return result;
}
}
内部运算工厂类实现
interface Ifactory//工厂类接口
{
Operation CreateOperation();
}
class AddFactory : Ifactory//需要哪种运算就实例化哪个运算类
{
public Operation CreateOperation()
{
return new OperationAdd();
}
}
class SubFactory : Ifactory
{
public Operation CreateOperation()
{
return new OperationSub();
}
}
class MulFactory : Ifactory
{
public Operation CreateOperation()
{
return new OperationMul();
}
}
class DivFactory : Ifactory
{
public Operation CreateOperation()
{
return new OperationDiv();
}
}
客户端代码
Ifactory operFactory = new AddFatory();//要什么运算就生成什么运算
Operation oper = operFactory.createOperation();
oper.NumberA = 1;
oper.NumberB = 2;
double result =oper.GetResult();
结构化程序设计客户端写法
class program
{
static void Main(string[] ards)
{
string strNumberA = Console.ReadLine();
string strOperate = Console.Readline();
string strNumberB = Console.ReadLine();
string strResult = "";
switch (strOperate)
{
case "+"
strResult = Convert.Tostring(Convert.Todouble(strNumberA)) + Convert.ToDouble(strNumberB));
break;
case "-"
strResult = Convert.Tostring(Convert.Todouble(strNumberA)) - Convert.ToDouble(strNumberB));
break;
case "*"
strResult = Convert.Tostring(Convert.Todouble(strNumberA)) * Convert.ToDouble(strNumberB));
break;
case "/"
strResult = Convert.Tostring(Convert.Todouble(strNumberA)) / Convert.ToDouble(strNumberB));
break;
}
console.witeLine("结果是:" + strResult);
console.ReadLine();
}
}
##########################################################
PS:C#版代码。要是还有不懂之处可以Q我