Click here to Skip to main content
15,887,683 members
Home / Discussions / C#
   

C#

 
AnswerRe: SerialPort.ReadLine() ? Pin
Gerry Schmitz2-Apr-18 6:49
mveGerry Schmitz2-Apr-18 6:49 
Questiondesigning around the need for an abstract static method Pin
Alexander Kindel1-Apr-18 15:21
Alexander Kindel1-Apr-18 15:21 
AnswerRe: designing around the need for an abstract static method Pin
Alexander Kindel1-Apr-18 16:02
Alexander Kindel1-Apr-18 16:02 
GeneralRe: designing around the need for an abstract static method Pin
Alexander Kindel1-Apr-18 20:25
Alexander Kindel1-Apr-18 20:25 
GeneralRe: designing around the need for an abstract static method Pin
#realJSOP2-Apr-18 1:05
mve#realJSOP2-Apr-18 1:05 
GeneralRe: designing around the need for an abstract static method Pin
Alexander Kindel2-Apr-18 8:45
Alexander Kindel2-Apr-18 8:45 
AnswerRe: designing around the need for an abstract static method Pin
Gerry Schmitz2-Apr-18 6:44
mveGerry Schmitz2-Apr-18 6:44 
AnswerRe: designing around the need for an abstract static method Pin
BillWoodruff2-Apr-18 21:43
professionalBillWoodruff2-Apr-18 21:43 
@alexanderkindle

I wonder if a combination of Classes and Extension Methods would be useful ?

Note that in the 'Polynomial class, the overloaded + operator concatenates the two internal List<int> ... but, in the Extension Method 'Add for two 'PolyNomial instances, an actual addition is performed based on ordinal position ... this variation is not meant to be anything but my guessing at the functionality you want Smile | :)
using System.Collections.Generic;
using System.Linq;

namespace YourNameSpace
{
    public interface IArithmetic<T>
    {
        T TValue { set; get; }
    }

    public interface INumber : IArithmetic<int>
    {
    }

    public interface IPolynomial : IArithmetic<List<int>>
    {
    }

    public interface IOperators<T>
    {
        T TAdd(T t1, T t2);
    }

    public class Number : INumber, IOperators<Number>
    {
        public Number(int value = 0)
        {
            TValue = value;
        }

        public int TValue { get; set; }

        public static Number operator +(Number n1, Number n2)
        {
            Number newNumber = new Number();
            newNumber.TValue = n1.TValue + n2.TValue;
            return newNumber;
        }

        public Number TAdd(Number t1, Number t2)
        {
            return t1 + t2;
        }
    }

    public class Polynomial : IPolynomial, IOperators<Polynomial>
    {
        public Polynomial(params int[] ints)
        {
            TValue = ints.ToList();
        }

        public List<int> TValue { get; set; }

        // concatenation here
        public static Polynomial operator +(Polynomial p1, Polynomial p2)
        {
            Polynomial newPolynomial = new Polynomial();
            newPolynomial.TValue = p1.TValue.Concat(p2.TValue).ToList();
            return newPolynomial;
        }

        public Polynomial TAdd(Polynomial t1, Polynomial t2)
        {
            return t1 + t2;
        }
    }

    public static class ArithmeticExtensions
    {
        public static Number Add(this Number n1, Number n2)
        {
            Number n = new Number();

            n.TValue += n1.TValue + n2.TValue;

            return n;
        }

        public static Number Add(this Number n1, Polynomial p1)
        {
            Number n = new Number(n1.TValue);

            foreach (int i in p1.TValue)
            {
                n.TValue += i;
            }

            return n;
        }

        // addition performed here
        public static Polynomial Add(this Polynomial p1, Polynomial p2)
        {
            Polynomial p;

            if (p1.TValue.Count == p2.TValue.Count)
            {
                p = new Polynomial(p1.TValue.ToArray());

                for (int i = 0; i < p.TValue.Count; i++)
                {
                    p.TValue[i] += p2.TValue[i];
                }
            } else if (p1.TValue.Count < p2.TValue.Count)
            {
                // p1 has fewer values
                p = new Polynomial(p2.TValue.ToArray());

                for (int i = 0; i < p1.TValue.Count; i++)
                {
                    p.TValue[i] += p1.TValue[i];
                }
            }
            else
            {
                // p1  has more values
                p = new Polynomial(p1.TValue.ToArray());

                for (int i = 0; i < p2.TValue.Count; i++)
                {
                    p.TValue[i] += p2.TValue[i];
                }
            }

            return p;
        }

        public static Polynomial Add(this Polynomial p1, Number n1)
        {
            Polynomial p = new Polynomial(p1.TValue.ToArray());

            for (int i = 0; i < p.TValue.Count; i++)
            {
                p.TValue[i] += n1.TValue;
            }

            return p;
        }
    }
}
Use example:
public void Teat()
{
    Number n1, n2;
    Polynomial p1, p2;
    
    n1 = new Number(100);
    n2 = new Number(200);
    p1 = new Polynomial(1,2,3, 4, 5, 6);
    p2 = new Polynomial(7,8,9);
    
    var n3 = n1 + n2;  // use of operator overloading
    var p3 = p1 + p2;  // => {1,2,3,4,5,6,7,8,9}
    
    var p4 = p1.Add(p2); // use of Extension Methods
    var p5 = p2.Add(p1); // both => {8,10,12,4,5,6}
    
    var p6 = p1.Add(n1); // add Number to Polynomial => {101 ~ 106}
    var n4 = n2.Add(p2); // add Polynomial to Number => 224
}

«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12


modified 3-Apr-18 8:55am.

GeneralRe: designing around the need for an abstract static method Pin
Alexander Kindel7-Apr-18 16:06
Alexander Kindel7-Apr-18 16:06 
GeneralRe: designing around the need for an abstract static method Pin
Alexander Kindel8-Apr-18 0:57
Alexander Kindel8-Apr-18 0:57 
AnswerC# Pin
Member 137573041-Apr-18 8:59
Member 137573041-Apr-18 8:59 
GeneralRe: C# Pin
OriginalGriff1-Apr-18 8:31
mveOriginalGriff1-Apr-18 8:31 
GeneralRe: C# Pin
PIEBALDconsult1-Apr-18 9:01
mvePIEBALDconsult1-Apr-18 9:01 
QuestionPlease help me with my basic c# calculator loop function Pin
Florence Gray31-Mar-18 15:46
Florence Gray31-Mar-18 15:46 
AnswerRe: Please help me with my basic c# calculator loop function Pin
OriginalGriff31-Mar-18 20:00
mveOriginalGriff31-Mar-18 20:00 
AnswerRe: Please help me with my basic c# calculator loop function Pin
Richard MacCutchan31-Mar-18 20:58
mveRichard MacCutchan31-Mar-18 20:58 
RantCoding error missing reference Pin
uighgg ojgeojge31-Mar-18 1:36
uighgg ojgeojge31-Mar-18 1:36 
AnswerRe: Coding error missing reference Pin
OriginalGriff31-Mar-18 1:47
mveOriginalGriff31-Mar-18 1:47 
QuestionRe: Coding error missing reference Pin
uighgg ojgeojge31-Mar-18 1:58
uighgg ojgeojge31-Mar-18 1:58 
GeneralRe: Coding error missing reference Pin
OriginalGriff31-Mar-18 2:28
mveOriginalGriff31-Mar-18 2:28 
GeneralRe: Coding error missing reference Pin
uighgg ojgeojge31-Mar-18 2:34
uighgg ojgeojge31-Mar-18 2:34 
GeneralRe: Coding error missing reference Pin
Dave Kreskowiak31-Mar-18 8:36
mveDave Kreskowiak31-Mar-18 8:36 
GeneralRe: Coding error missing reference Pin
Pete O'Hanlon31-Mar-18 10:12
mvePete O'Hanlon31-Mar-18 10:12 
QuestionProblem with Rotating Platforms in Unity 3D Pin
O.G.I.30-Mar-18 8:06
O.G.I.30-Mar-18 8:06 
AnswerRe: Problem with Rotating Platforms in Unity 3D Pin
Eddy Vluggen30-Mar-18 22:48
professionalEddy Vluggen30-Mar-18 22:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.