SHARE
TWEET

Untitled

a guest Oct 13th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using Newtonsoft.Json;
  7. using PolynomialTask.Exceptions;
  8.  
  9. namespace PolynomialTask
  10. {
  11.     class PolynomialType
  12.     {
  13.         /// <summary>
  14.         /// Key is degree, value is coefficient
  15.         /// </summary>                
  16.         public Dictionary<int, double> Polynomial { get; private set; }
  17.         public int DegreeOfPolynomial { get; private set; }
  18.         private static Random _random;
  19.         static PolynomialType()
  20.         {
  21.             _random = new Random();
  22.         }
  23.         /// <summary>
  24.         /// Determines whether polynomial contains specified degree
  25.         /// </summary>
  26.         /// <param name="degree"></param>
  27.         /// <returns></returns>
  28.         public bool ContainsDegree(int degree)
  29.         {
  30.             return Polynomial.ContainsKey(degree);
  31.         }
  32.         /// <summary>
  33.         /// Determines whether polynomial contains specified coefficient
  34.         /// </summary>
  35.         /// <param name="coefficient"></param>
  36.         /// <returns></returns>
  37.         public bool ContainsCoefficient(double coefficient)
  38.         {
  39.             return Polynomial.ContainsValue(coefficient);
  40.         }
  41.         /// <summary>
  42.         /// Creates polynomial that equals to 0
  43.         /// </summary>
  44.         public PolynomialType()
  45.         {
  46.             Polynomial = new Dictionary<int, double> { {0, 0.0 } };
  47.             DegreeOfPolynomial = 0;
  48.         }
  49.         /// <summary>
  50.         /// Creates polynomial with custom degree        
  51.         /// </summary>
  52.         public PolynomialType(int degree)
  53.         {
  54.             Polynomial = new Dictionary<int, double> ();
  55.             int digitNumber = 100;
  56.             //PolynomialType polynomial = new PolynomialType();
  57.             for (int i = 1; i <= degree; i++)
  58.             {
  59.                 //Add(i, _random.NextDouble() * digitNumber);
  60.                 Polynomial.Add(i, _random.NextDouble() * digitNumber);
  61.             }
  62.             DegreeOfPolynomial = FindDegreeOfPolynomial(Polynomial);
  63.         }
  64.         /// <summary>
  65.         /// Creates polynomial from custom data        
  66.         /// </summary>
  67.         public PolynomialType(Dictionary<int, double> polynomial)
  68.         {
  69.             Polynomial = polynomial;
  70.             DegreeOfPolynomial = FindDegreeOfPolynomial(polynomial);
  71.         }
  72.         //indexer
  73.         public double this[int degree]
  74.         {
  75.             get =>
  76.                 Polynomial.ContainsKey(degree) ? Polynomial[degree]
  77.                     : throw new IncorrectPolymonialKeyIndexException(degree);
  78.             set => Polynomial.TryAdd(degree, value);
  79.         }
  80.         /// <summary>
  81.         /// Gets max degree in polynomial
  82.         /// </summary>
  83.         /// <param name="polynomial"></param>
  84.         /// <returns></returns>
  85.         private int FindDegreeOfPolynomial(Dictionary<int, double> polynomial)
  86.         {
  87.             if (polynomial == null)
  88.                 throw new PolymonialIsNullException();
  89.             var degrees = new int[polynomial.Count];
  90.             Polynomial.Keys.CopyTo(degrees, 0);
  91.             return degrees.Max();
  92.         }
  93.         public void Add(int degree, double coefficient)
  94.         {
  95.             if (double.IsNaN(coefficient)
  96.                 || degree == int.MaxValue
  97.                 || degree == int.MinValue)
  98.             {
  99.                 throw double.IsNaN(coefficient)
  100.                     ? new IncorrectPolymonialDataException("Incorrect data: coefficient equals to " + coefficient)
  101.                     : new IncorrectPolymonialDataException("Incorrect data: degree equals to " + degree);
  102.             }
  103.             Polynomial.Add(degree, coefficient);
  104.             DegreeOfPolynomial = FindDegreeOfPolynomial(Polynomial);
  105.         }
  106.         /// <summary>
  107.         /// Looks for coefficient in polynomial and gets corresponding degrees
  108.         /// </summary>
  109.         /// <param name="coefficient"></param>
  110.         /// <returns></returns>
  111.         public int[] GetDegree(double coefficient)
  112.         {
  113.             if (!Polynomial.ContainsValue(coefficient))
  114.                 throw new IncorrectPolymonialValueException();
  115.             return (int[]) Polynomial.Where(pair => pair.Value == coefficient).Select(pair => pair.Key);
  116.         }
  117.         /// <summary>
  118.         /// Looks for coefficient in polynomial and sets new degree
  119.         /// </summary>
  120.         /// <param name="coefficient"></param>
  121.         /// <param name="degreeToSet"></param>
  122.         /// <returns></returns>
  123.         public bool SetDegree(double coefficient, int degreeToSet)
  124.         {
  125.             if (!Polynomial.ContainsValue(coefficient))
  126.                 throw new IncorrectPolymonialValueException();
  127.             var degreesToChange = Polynomial.Where(pair => pair.Value == coefficient)
  128.                 .Select(pair => pair.Key)
  129.                 .ToArray();
  130.             for (var i = 0; i < degreesToChange.Length; i++)
  131.             {
  132.                 degreesToChange[i] = degreeToSet;
  133.             }
  134.             return true;
  135.         }
  136.  
  137.         public override string ToString()
  138.         {
  139.             StringBuilder result = new StringBuilder();
  140.             var degreesArray = Polynomial.Keys.ToArray();
  141.             var coefficientsArray = Polynomial.Values.ToArray();
  142.             for (int i = 0; i < Polynomial.Count; i++)
  143.             {
  144.                 string nextSign = i + 1 < coefficientsArray.Length ? coefficientsArray[i + 1] > 0 ? " + " : " " : " ";
  145.                 result.Append(coefficientsArray[i] + "x^" + degreesArray[i] + nextSign);
  146.             }
  147.  
  148.             return result.ToString();
  149.         }
  150.  
  151.         public static PolynomialType operator +(PolynomialType polynomial1, PolynomialType polynomial2)
  152.         {
  153.             if (polynomial1 == null || polynomial2 == null)
  154.             {
  155.                 throw new ArgumentNullException();
  156.             }
  157.             var result = new PolynomialType();
  158.             var resultDegree = polynomial1.DegreeOfPolynomial > polynomial2.DegreeOfPolynomial
  159.                 ? polynomial1.DegreeOfPolynomial
  160.                 : polynomial2.DegreeOfPolynomial;
  161.             for (var degree = resultDegree; degree >= 0; degree--)
  162.             {
  163.                 if (polynomial1.ContainsDegree(degree) && polynomial2.ContainsDegree(degree))
  164.                 {
  165.                     result.Add(degree, polynomial1[degree] + polynomial2[degree]);
  166.                 }
  167.                 else if (polynomial1.ContainsDegree(degree) && !polynomial2.ContainsDegree(degree))
  168.                 {
  169.                     result.Add(degree, polynomial1[degree]);
  170.                 }
  171.                 else if (!polynomial1.ContainsDegree(degree) && polynomial2.ContainsDegree(degree))
  172.                 {
  173.                     result.Add(degree, polynomial2[degree]);
  174.                 }
  175.             }
  176.             return result;
  177.         }
  178.         public static PolynomialType operator -(PolynomialType polynomial1, PolynomialType polynomial2)
  179.         {
  180.             if (polynomial1 == null || polynomial2 == null)
  181.             {
  182.                 throw new ArgumentNullException();
  183.             }
  184.             var result = new PolynomialType();
  185.             var resultDegree = polynomial1.DegreeOfPolynomial > polynomial2.DegreeOfPolynomial
  186.                 ? polynomial1.DegreeOfPolynomial
  187.                 : polynomial2.DegreeOfPolynomial;
  188.             for (var degree = resultDegree; degree >= 0; degree--)
  189.             {
  190.                 if (polynomial1.ContainsDegree(degree) && polynomial2.ContainsDegree(degree))
  191.                 {
  192.                     result.Add(degree, polynomial1[degree] - polynomial2[degree]);
  193.                 }
  194.                 else if (polynomial1.ContainsDegree(degree) && !polynomial2.ContainsDegree(degree))
  195.                 {
  196.                     result.Add(degree, polynomial1[degree]);
  197.                 }
  198.                 else if (!polynomial1.ContainsDegree(degree) && polynomial2.ContainsDegree(degree))
  199.                 {
  200.                     result.Add(degree, -polynomial2[degree]);
  201.                 }
  202.             }
  203.             return result;
  204.         }
  205.         public static PolynomialType operator *(PolynomialType polynomial1, PolynomialType polynomial2)
  206.         {
  207.             if (polynomial1 == null || polynomial2 == null)
  208.             {
  209.                 throw new ArgumentNullException();
  210.             }
  211.             var result = new PolynomialType();
  212.             foreach (var (degree1, coefficient1) in polynomial1.Polynomial)
  213.             {
  214.                 foreach (var (degree2, coefficient2) in polynomial2.Polynomial)
  215.                 {
  216.                     result.Add(degree1 + degree2, coefficient1 * coefficient2);
  217.                 }
  218.             }
  219.             return result;
  220.         }
  221.        
  222.         public static void Serialize(PolynomialType polynomialToSerialize)
  223.         {
  224.             if (polynomialToSerialize == null)
  225.                 throw new InvalidSerializationException();
  226.             string path = Environment.CurrentDirectory + @"\Polynomial.json";
  227.             var jsonString = JsonConvert.SerializeObject(polynomialToSerialize, Formatting.Indented);
  228.             File.WriteAllText(path, jsonString);
  229.         }
  230.         public static PolynomialType Deserialize()
  231.         {
  232.             string path = Environment.CurrentDirectory + @"\Polynomial.json";
  233.             var jsonString = File.ReadAllText(path);
  234.             return JsonConvert.DeserializeObject<PolynomialType>(jsonString);
  235.         }
  236.     }
  237. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top