4 Replies - 776 Views - Last Post: 28 March 2013 - 06:42 AM Rate Topic: -----

#1 greatcircle49  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-October 12

how to get data from context in stategy

Posted 26 March 2013 - 06:16 PM

Hi I need advise
I don't how to write ToString with muilti format ( OPInfix, OPPrefix, OPSuffix)
Posted Image
Is This A Good Question/Topic? 0
  • +

Replies To: how to get data from context in stategy

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: how to get data from context in stategy

Posted 26 March 2013 - 08:25 PM

You shouldn't write output in those classes, you would simply call ToString on the current expression that you are handling.
Was This Post Helpful? 0
  • +
  • -

#3 greatcircle49  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-October 12

Re: how to get data from context in stategy

Posted 27 March 2013 - 06:09 PM

with ConstExp, NomialExpr, BinaryExp ( AdditiveExpr) has 3 way to write ToString()
So I think I can use Stategy.
I don't know how to OPInfix, OPPrefix, OPSuffix understand what is type of Expression(ConstExp, MonialExp....), don't use swith case
Posted Image
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: how to get data from context in stategy

Posted 27 March 2013 - 06:55 PM

Assuming your OutputSupport has a virtual method named Print(), within your Print() method, you would simply call ToString on the expression.

Unfortunately, you've not shown us enough code to be able to be more specific than that. Show us what you have tried.
Was This Post Helpful? 0
  • +
  • -

#5 greatcircle49  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-October 12

Re: how to get data from context in stategy

Posted 28 March 2013 - 06:42 AM

thank you very much for your help, I want to uses stategy to write OutputSupport, OPInfix,OPPrefix,OPSuffix
this is original source
Posted Image

Expression
    public abstract class Expression
    {
        public abstract double Evaluate();

        public abstract Expression Derive();

        public abstract string OPInfixToString();
        public abstract string OPPrefixToString();
        public abstract string OPSuffixToString();
    }


ConstExpr
 public class ConstExpr : Expression
    {
        private double c;
        private Expression cachedDerive = null;

        // CONSTRUCTORS
        public ConstExpr()
        {
            c = 0;
        }

        public ConstExpr(double _value)
        {
            c = _value;
        }

        public override double Evaluate()
        {
            return c;
        }

        public override Expression Derive()
        {
            if (cachedDerive == null)
                cachedDerive = new ConstExpr(0);

            return cachedDerive;
        }

        
        public override string OPInfixToString()
        {
            return c.ToString();   
        }
        public override string OPPrefixToString()
        {
            //do something
            return null;
        }
        public override string OPSuffixToString()
        {
            //do some thing
            return null;
        }
    }


NomialExpr
    public class NomialExpr : Expression
    {
        private double x;
        private double a;
        private double n;
        private Expression cachedDerive = null;

        // CONSTRUCTORS
        public NomialExpr()
        {
            a = 1;
            n = 1;
        }
        public static Expression Create(double _x, double _h, double _s)
        {
            if (_h == 0||_s==0)
                return new ConstExpr(_h);
            else
                return new NomialExpr(_x, _h, _s);
        }
        private NomialExpr(double _x, double _h, double _s)
        {
            x = _x;
            a = _h;
            n = _s;
        }


        public override double Evaluate()
        {
            return (a * Math.Pow(x, n));
        }

        public override Expression Derive()
        {
            if(cachedDerive==null)
                cachedDerive = new NomialExpr(x, this.a * this.n, this.n - 1);

            return cachedDerive;
        }

        
        public override string OPInfixToString()
        {
            return (a + "X ^ " + n);
        }
        public override string OPPrefixToString()
        {
            //do some thing
            return null;
        }
        public override string OPSuffixToString()
        {
            //do some thing
            return null;
        }
    }


BinaryExpr

  public abstract class BinaryExpr : Expression
    {
        protected Expression u;
        protected Expression v;
        protected Expression cachedDerive = null;

        public override string OPInfixToString()
        {
            return (u.ToString() + v.ToString());
        }
        public override string OPPrefixToString()
        {
            //do some thing
            return null;
        }
        public override string OPSuffixToString()
        {
            //do some thing
            return null;
        }
    }


AdditiveExpr
   public class AdditiveExpr : BinaryExpr
    {

        private bool isAdditiveExpr = true;  

        // CONSTRUCTORS
        public AdditiveExpr(Expression _u, Expression _v, bool _isDaThucTong)
        {
            u = _u;
            v = _v;
            isAdditiveExpr = _isDaThucTong;
        }

        public override double Evaluate()
        {
            if (isAdditiveExpr)
                return u.Evaluate() + v.Evaluate();
            return u.Evaluate() - v.Evaluate();
        }

        public override Expression Derive()
        {
            if(cachedDerive == null)
                cachedDerive = new AdditiveExpr(u.Derive(), v.Derive(), isAdditiveExpr);

            return cachedDerive;
        }

        public override string OPInfixToString()
        {
            if (isAdditiveExpr) 
                return ("(" + u.ToString() + " + " + v.ToString() + ")");
            return ("(" + u.ToString() + " - " + v.ToString() + ")");  
        }
        public override string OPPrefixToString()
        {
            //do some thing
            return null;
        }
        public override string OPSuffixToString()
        {
            //do some thing
            return null;
        }
    }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1