C#难题求高手解答 我要实现计算器的功能的
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Operation voluation = new Operation();
Console.WriteLine("please input numberA");
double a=Convert.ToDouble(Console.ReadLine ());
Console.WriteLine("please input numberB");
double b=Convert.ToDouble(Console.ReadLine ());
voluation.NumberA = a;
voluation.NumberB = b;
OperationFactory Op = new OperationFactory();
Console.WriteLine("please input sign");
string str = Console.ReadLine();
Console.ReadLine();
}
}
class Operation
{
private double numberA;
private double numberB;
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 = NumberA + NumberB;
return result;
}
}
class OperationSub : Operation
{
public override double Getresult()
{
double result = NumberA - NumberB;
return result;
}
}
class OperationMul : Operation
{
public override double Getresult()
{
double result = NumberA * NumberB;
return result;
}
}
class OperationDiv : Operation
{
public override double Getresult()
{
if (NumberB == 0)
throw new Exception("除数不能为0");
double result = NumberA / NumberB;
return result;
}
}
class OperationFactory
{
public static Operation craeteOperation(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;
}
}
return oper;
}
}
} 展开
修改后的C#程序:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("please input numberA");
double a = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("please input numberB");
double b = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("please input sign");
string str = Console.ReadLine();
Operation voluation = OperationFactory.createOperation(str);
voluation.NumberA = a;
voluation.NumberB = b;
Console.WriteLine("{0}{1}{2}={3}", voluation.NumberA, str, voluation.NumberB, voluation.Getresult());
}
}
class Operation
{
private double numberA;
private double numberB;
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 = NumberA + NumberB;
return result;
}
}
class OperationSub : Operation
{
public override double Getresult()
{
double result = NumberA - NumberB;
return result;
}
}
class OperationMul : Operation
{
public override double Getresult()
{
double result = NumberA * NumberB;
return result;
}
}
class OperationDiv : Operation
{
public override double Getresult()
{
if (NumberB == 0)
throw new Exception("除数不能为0");
double result = NumberA / NumberB;
return result;
}
}
class OperationFactory
{
public static Operation createOperation(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;
}
}
return oper;
}
}
}
运行测试:
我的为何不能运行啊。。错误 1 “ConsoleApplication1.OperationFactory”并不包含“createOperation
我对你的代码进行了几处修改,主要是在Main()方法中,你运行时需要使用我修改后的完整代码,而不是部分。
你的错误在于对对象的传值没有搞清楚,造成某些对象成员没有得到输入的值。还有一点更重要的是,你虽然定义了设计模式,但压根儿就没有引用。
Main方法修改:
Console.WriteLine("please input numberA");
double a=Convert.ToDouble(Console.ReadLine ());
Console.WriteLine("please input numberB");
double b=Convert.ToDouble(Console.ReadLine ());
Console.WriteLine("please input sign");
string str = Console.ReadLine();
OperationFactory Op = new OperationFactory(str);
Console.Write("result:"+Op.Getresult());
Console.ReadLine();
基类Operation几个构造函数:
public Operation(double a,double b){
this.numberA=a;
this.numberB=b;
}
还是有错。。1错误 “OperationFactory”方法没有采用“1”个参数的重载
错误 2 “ConsoleApplication1.OperationFactory”并不包含“Getresult”的定义
你的qq号,我找你详解
QQ930070554
把OperationFactory Op = new OperationFactory(str);改为:
Operation op=OperationFactory.createOperation(str);
这下应该是对的了,刚才没注意OperationFactory是一个类,因为你那上面也是直接new的所以忽略了