C#难题求高手解答 我要实现计算器的功能的

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespaceConsoleApplicat... using System;
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;
}

}
}
展开
 我来答
老冯文库
推荐于2016-10-08 · 知道合伙人软件行家
老冯文库
知道合伙人软件行家
采纳数:1139 获赞数:8733

向TA提问 私信TA
展开全部

修改后的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()方法中,你运行时需要使用我修改后的完整代码,而不是部分。

你的错误在于对对象的传值没有搞清楚,造成某些对象成员没有得到输入的值。还有一点更重要的是,你虽然定义了设计模式,但压根儿就没有引用。
白昼怎懂夜的黑8111
2012-03-09 · 超过24用户采纳过TA的回答
知道答主
回答量:114
采纳率:0%
帮助的人:65.2万
展开全部
Main方法,下面写了个工厂,所以Operation对象应该从工厂里面获取
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的所以忽略了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式