Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Zadanie_1a
  8. {
  9.     class Wezel
  10.     {
  11.         public int a, p;
  12.         public Wezel nastepny;
  13.  
  14.     }
  15.     class Drzewo
  16.     {
  17.         public Wezel glowa;
  18.         public void DodajElementy(int[] tab)
  19.         {
  20.             for (int i = 0; i < tab.Length; i++)
  21.             {
  22.                 Wezel tmp = new Wezel();
  23.                 tmp.a = tab[i];
  24.                 tmp.p = i;
  25.                 tmp.nastepny = glowa;
  26.                 if (tab[i]!=0) glowa = tmp;
  27.             }
  28.         }
  29.         public override string ToString()
  30.         {
  31.             string tmp = "";
  32.             Wezel temp = glowa;
  33.             while (temp!=null)
  34.             {
  35.                 if (temp.p == 1) tmp += temp.a + "x + ";
  36.                 else if(temp.p != 0) tmp += temp.a + "x^" + temp.p + " + ";
  37.                 else tmp += temp.a + "";
  38.                 temp = temp.nastepny;
  39.             }
  40.             return tmp;
  41.         }
  42.         public void Rozniczkowanie()
  43.         {
  44.             Wezel tmp = glowa;
  45.             while (tmp!=null)
  46.             {
  47.                 if (tmp.p ==1) {
  48.                     tmp.a = tmp.a * tmp.p;
  49.                     tmp.p--;
  50.                     tmp.nastepny = tmp.nastepny.nastepny;
  51.                     tmp = tmp.nastepny;
  52.                     continue; }
  53.                 tmp.a =tmp.a* tmp.p;
  54.                 tmp.p--;
  55.                
  56.                 tmp = tmp.nastepny;
  57.                
  58.             }
  59.         }
  60.         public void Dodawanie(Drzewo d)
  61.         {
  62.             Wezel tmp = glowa;
  63.             Wezel dmp = d.glowa;
  64.             while (tmp!=null)
  65.             {
  66.                 tmp.a = tmp.a + dmp.a;
  67.                 tmp = tmp.nastepny; dmp = dmp.nastepny;
  68.             }
  69.         }
  70.         public void Odejmowanie(Drzewo d)
  71.         {
  72.             Wezel tmp = glowa;
  73.             Wezel dmp = d.glowa;
  74.             while (tmp != null)
  75.             {
  76.                 tmp.a = tmp.a - dmp.a;
  77.                 tmp = tmp.nastepny; dmp = dmp.nastepny;
  78.             }
  79.         }
  80.     }
  81.     class Program
  82.     {
  83.         static void Main(string[] args)
  84.         {
  85.             Drzewo w = new Drzewo();
  86.             int[] tablica = { 4, 10, 2, 5, 0, 0, 2 };
  87.             w.DodajElementy(tablica);
  88.             Console.WriteLine(w);
  89.             w.Rozniczkowanie();
  90.             Console.WriteLine(w);
  91.             w.Dodawanie(w);
  92.             Console.WriteLine(w);
  93.             w.Odejmowanie(w);
  94.             Console.WriteLine(w);
  95.  
  96.             Console.ReadKey();
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement