Пример #1
0
        internal Polynome AddPolynome(Polynome p2)
        {
            int maxLevel = this.maxLevel;
            int minLevel = this.minLevel;

            if (p2.maxLevel > maxLevel)
            {
                maxLevel = p2.maxLevel;
            }
            if (p2.minLevel < minLevel)
            {
                minLevel = p2.minLevel;
            }
            Polynome res = new Polynome(maxLevel, minLevel);

            Coeficient c;

            for (int i = 0; i < this.coefs.Count; i++)
            {
                c = this.coefs[i];
                res.coefs[res.maxLevel - c.level] = res.coefs[res.maxLevel - c.level].AddCoeficient(c.coeficient);
            }

            for (int i = 0; i < p2.coefs.Count; i++)
            {
                c = p2.coefs[i];
                res.coefs[res.maxLevel - c.level] = res.coefs[res.maxLevel - c.level].AddCoeficient(c.coeficient);
            }

            res.RemoveUnnecessary();

            return(res);
        }
Пример #2
0
        internal Polynome Derive()
        {
            int minLevel;

            if (this.minLevel == 0)
            {
                minLevel = 0;
            }
            else
            {
                minLevel = this.minLevel - 1;
            }
            if (this.maxLevel == this.minLevel && this.minLevel == 0)
            {
                return(new Polynome(0, 0));
            }
            Polynome p = new Polynome(this.maxLevel - 1, minLevel);

            for (int i = 0; i < coefs.Count; i++)
            {
                int pow   = this.coefs[i].level;
                int level = pow - 1;
                int c     = this.coefs[i].coeficient * pow;
                if (i >= p.coefs.Count)
                {
                    continue;
                }
                p.coefs[i] = new Coeficient(level, c);
            }
            p.RemoveUnnecessary();
            return(p);
        }
Пример #3
0
        internal Polynome MultiplyByPolynome(Polynome p2)
        {
            if (p2.coefs.Count == 1 && p2.coefs[0].coeficient == 0)
            {
                return(new Polynome(0, 0));
            }
            int      maxLevel = this.maxLevel + p2.maxLevel;
            int      minLevel = this.minLevel + p2.minLevel;
            Polynome res      = new Polynome(maxLevel, minLevel);

            for (int i = 0; i < this.coefs.Count; i++)
            {
                var thisC = this.coefs[i];
                for (int k = 0; k < p2.coefs.Count; k++)
                {
                    var p2C = p2.coefs[k];
                    int pow = thisC.level + p2C.level;
                    int c   = thisC.coeficient * p2C.coeficient;
                    res.coefs[maxLevel - pow] = res.coefs[maxLevel - pow].AddCoeficient(c);
                }
            }

            res.RemoveUnnecessary();
            return(res);
        }
Пример #4
0
        internal Polynome Substitute(Polynome p2)
        {
            int      maxLevel     = this.maxLevel * p2.maxLevel;
            int      minLevel     = this.minLevel * p2.minLevel;
            var      res          = new Polynome(maxLevel, minLevel);
            Polynome tempConstant = p2.MultiplyByPolynome(new Polynome(new Coeficient[] { new Coeficient(0, 1) }));; //get equal polynome

            for (int i = 0; i < this.coefs.Count; i++)
            {
                var      thisC = this.coefs[i];
                int      pow;
                Polynome temp = p2.MultiplyByPolynome(new Polynome(new Coeficient[] { new Coeficient(0, 1) }));     //get equal polynome
                if (thisC.level >= 1)
                {
                    for (int l = 1; l < thisC.level; l++)
                    {
                        temp = temp.MultiplyByPolynome(tempConstant);
                    }
                }
                else
                {
                    temp = Polynome.FromStringArray(new string[] { "1" });
                }
                for (int l = 0; l < temp.coefs.Count; l++)
                {
                    var tempC = temp.coefs[l];
                    pow = tempC.level;
                    if (thisC.level == 0)
                    {
                    }
                    int c = thisC.coeficient * tempC.coeficient;
                    res.coefs[maxLevel - pow] = res.coefs[maxLevel - pow].AddCoeficient(c);
                }
            }

            res.RemoveUnnecessary();
            return(res);
        }
Пример #5
0
        static void Main(string[] args)
        {
            string line;

            string[] lineParts;
            Polynome p = null;

            while ((line = Console.ReadLine()) != null)
            {
                lineParts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                if (lineParts.Length == 0)
                {
                    Console.WriteLine("Syntax Error");
                }
                else
                {
                    if (Coeficient.IsValid(lineParts[0]))
                    {
                        p = Polynome.FromStringArray(lineParts);
                        if (p == null)
                        {
                            Console.WriteLine("Syntax Error");
                        }
                        Console.WriteLine(p);
                        continue;
                    }

                    string[] temp;
                    Polynome p2;
                    switch (lineParts[0])
                    {
                    case "+":
                        temp = new string[lineParts.Length - 1];
                        Array.Copy(lineParts, 1, temp, 0, lineParts.Length - 1);
                        p2 = Polynome.FromStringArray(temp);
                        if (p2 == null || p == null)
                        {
                            Console.WriteLine("Syntax Error");
                            continue;
                        }
                        p = p.AddPolynome(p2);
                        Console.WriteLine(p);
                        break;

                    case "-":
                        temp = new string[lineParts.Length - 1];
                        Array.Copy(lineParts, 1, temp, 0, lineParts.Length - 1);
                        p2 = Polynome.FromStringArray(temp);
                        if (p2 == null || p == null)
                        {
                            Console.WriteLine("Syntax Error");
                            continue;
                        }
                        p = p.SubtractPolynome(p2);
                        Console.WriteLine(p);
                        break;

                    case "*":
                        temp = new string[lineParts.Length - 1];
                        Array.Copy(lineParts, 1, temp, 0, lineParts.Length - 1);
                        p2 = Polynome.FromStringArray(temp);
                        if (p2 == null || p == null)
                        {
                            Console.WriteLine("Syntax Error");
                            continue;
                        }
                        p = p.MultiplyByPolynome(p2);
                        Console.WriteLine(p);
                        break;

                    case "e":
                        int val;
                        if (p == null || lineParts.Length != 2 || !int.TryParse(lineParts[1], out val))
                        {
                            Console.WriteLine("Syntax Error");
                            continue;
                        }
                        Console.WriteLine(p.Eval(val));
                        break;

                    case "d":
                        if (p == null)
                        {
                            Console.WriteLine("Syntax Error");
                            continue;
                        }
                        p = p.Derive();
                        Console.WriteLine(p);
                        break;

                    case "s":
                        temp = new string[lineParts.Length - 1];
                        Array.Copy(lineParts, 1, temp, 0, lineParts.Length - 1);
                        p2 = Polynome.FromStringArray(temp);
                        if (p2 == null || p == null)
                        {
                            Console.WriteLine("Syntax Error");
                            continue;
                        }
                        p = p.Substitute(p2);
                        Console.WriteLine(p);
                        break;

                    default:
                        Console.WriteLine("Syntax Error");
                        break;
                    }
                }
            }
        }