Danielos168

Wielomian

Jan 21st, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. class Element
  2.     {
  3.         public int wykladnik,wspolczynnik;
  4.         public Element nastepny;
  5.  
  6.         public Element(int wspolczynnik, int wykladnik)
  7.         {
  8.             this.wspolczynnik = wspolczynnik;
  9.             this.wykladnik = wykladnik;
  10.         }
  11.  
  12.         public override string ToString()
  13.         {
  14.             return$"{wspolczynnik}, {wykladnik}";
  15.         }
  16.  
  17.         public static Element operator +(Element e1, Element e2)
  18.         {
  19.             if (e1.wykladnik == e2.wykladnik) return new Element(e1.wspolczynnik + e2.wspolczynnik, e1.wykladnik);
  20.             else return null;
  21.         }
  22.  
  23.         public static Element operator -(Element e1, Element e2)
  24.         {
  25.             if (e1.wykladnik == e2.wykladnik) return new Element(e1.wspolczynnik - e2.wspolczynnik,e1.wykladnik);
  26.             else return null;
  27.         }
  28.  
  29.     }
  30.  
  31.     class Wielomian
  32.     {
  33.         Element head;
  34.  
  35.         public Wielomian(int[] tab )
  36.         {
  37.             if (tab.Length < 1) return;
  38.             head = new Element(tab.Last(),tab.Length-1);
  39.  
  40.             Element e = head;
  41.             for (int i = tab.Length-2; i >=0; --i)
  42.             {
  43.                 if(tab [ i ]==0)continue;
  44.  
  45.                 e.nastepny = new Element(tab[i],i);
  46.                 e = e.nastepny;
  47.             }
  48.         }
  49.  
  50.         public Wielomian()
  51.         {
  52.             head = null;
  53.         }
  54.  
  55.         public void Rozniczkuj()
  56.         {
  57.             Element e = head;
  58.             while (e != null)
  59.             {
  60.                 e.wspolczynnik *= e.wykladnik;
  61.  
  62.                 e.wykladnik--;
  63.                 if (e.nastepny.wykladnik == 0) e.nastepny = null;
  64.  
  65.                 e = e.nastepny;
  66.             }
  67.         }
  68.  
  69.         public void WypisZ()
  70.         {
  71.             Element e = head;
  72.             while (e != null)
  73.             {
  74.                 Console.Write(e);
  75.                 if(e.nastepny != null) Console.Write("->");
  76.                 e = e.nastepny;
  77.             }
  78.  
  79.             Console.WriteLine();
  80.         }
  81.     }
Add Comment
Please, Sign In to add comment