Advertisement
Guest User

Polinomicii bratici

a guest
Apr 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.17 KB | None | 0 0
  1. polinomi.cs
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace Polinomi
  10. {
  11.     class Polinom
  12.     {
  13.         public List<Element> el=new List<Element>();
  14.         public Polinom(Element elem)
  15.         {
  16.             el.Add(elem);
  17.         }
  18.         public Polinom()
  19.         {
  20.  
  21.         }
  22.         public Polinom(Polinom a)
  23.         {
  24.             el = a.el;
  25.         }
  26.         public Polinom(string s)
  27.         {
  28.             s.ToCharArray();
  29.             List<int> indeksiZnakova = new List<int>();
  30.             List<string> delovi=new List<string>();
  31.             indeksiZnakova.Add(0);
  32.             //delimo string na manje delove
  33.             for (int i = 0; i < s.Length; i++)
  34.             {
  35.                 if (String.Equals(s[i], '+') || String.Equals(s[i], '-'))
  36.                     if(i!=0)
  37.                     indeksiZnakova.Add(i);
  38.             }
  39.             //znak na prvom delu polinoma dal je + ili - na pocetku to gleda
  40.             indeksiZnakova.Add(s.Length);
  41.             for (int i = 0; i < indeksiZnakova.Count-1; i++)
  42.             {
  43.                 string deo=s.Substring(indeksiZnakova[i],indeksiZnakova[i+1]-indeksiZnakova[i]);
  44.                 if (!String.Equals(deo, ""))
  45.                 {
  46.                     delovi.Add(deo);
  47.                 }                
  48.             }
  49.             //podelili string na delove , sad pravimo elemente
  50.             for (int i = 0; i < delovi.Count; i++)
  51.             {
  52.                 delovi[i].ToCharArray();
  53.                 string prefiks = "1";
  54.                 int j = 0;
  55.                 if (String.Equals(delovi[i][0], '+'))//prefiks preskacemo jer je po defaultu vec 1
  56.                 {
  57.                      j = 1;
  58.                 }
  59.                 if (String.Equals(delovi[i][0], '-'))//prefiks menjamo u -1 ako ispred x-a nema nista
  60.                 {
  61.                     j = 1;
  62.                     prefiks = "-1";
  63.                 }
  64.                
  65.                 string sufiks = "0";//po defaultu
  66.                 if (delovi[i][j] >= '0' && delovi[i][j] <= '9')
  67.                 {
  68.                     prefiks = "";
  69.                     if (String.Equals(delovi[i][0], '-'))
  70.                         prefiks = "-";
  71.                     while (delovi[i][j] >= '0' && delovi[i][j] <= '9')//ako je broj ispred dodaje do x-a kao koef
  72.                     {
  73.                         prefiks += delovi[i][j];//dodajemo do pocetka x-a
  74.                         j++;
  75.                         if (j == delovi[i].Length)
  76.                             break;
  77.                         if (String.Equals('.', delovi[i][j]))
  78.                         {
  79.                             prefiks += ".";//u slucaju da je realan broj/double
  80.                             j++;
  81.                         }
  82.                     }
  83.                 }
  84.                 if (j < delovi[i].Length)
  85.                 {
  86.                     if (String.Equals('x',delovi[i][j]))
  87.                     {
  88.                         sufiks = "1";
  89.                         j++;
  90.                     }
  91.                 }
  92.                 if (j < delovi[i].Length)
  93.                 {
  94.                     sufiks = "";
  95.                     while (delovi[i][j] >= '0' && delovi[i][j] <= '9')//sufiks ide do kraja dodaje i ako je double i ne moze biti negativan
  96.                     {
  97.                         sufiks += delovi[i][j];
  98.                         j++;
  99.                         if (j == delovi[i].Length)
  100.                             break;
  101.                         if (String.Equals('.', delovi[i][j]))
  102.                         {
  103.                             sufiks += ".";
  104.                             j++;
  105.                         }
  106.                     }
  107.                 }
  108.                 Element a = new Element(Convert.ToDouble(prefiks), Convert.ToDouble(sufiks));
  109.                 el.Add(a);
  110.             }
  111.             sredjivanje();
  112.             sortiraj();            
  113.         }
  114.         public void sortiraj()
  115.         {
  116.             //sortiranjeListePoOpadajucemRedosledu
  117.             List<Element> sort = new List<Element>();//pravi se nova lista koja je sortirana
  118.             while (el.Count > 0)
  119.             {
  120.                 double maxStepen = -1;
  121.                 int indeksMaxStepen = 0;
  122.                 for (int i = 0; i < el.Count; i++)
  123.                 {
  124.                     if (el[i].stepen > maxStepen)
  125.                     {
  126.                         maxStepen = el[i].stepen;
  127.                         indeksMaxStepen = i;
  128.                     }
  129.                 }
  130.                 sort.Add(el[indeksMaxStepen]);//dodamo u listu njega sortiran
  131.                 el.RemoveAt(indeksMaxStepen);//brisemo iz starog nesortiranog izbrise
  132.             }
  133.             el = sort;
  134.         }
  135.         public void sredjivanje() //ako ima vise njih istog stepena sabere ih proverava da li ima
  136.         {
  137.             for (int i = 0; i < el.Count-1; i++)
  138.             {
  139.                 for (int j = i + 1; j < el.Count; j++)
  140.                 {
  141.                     if (el[i].stepen == el[j].stepen)
  142.                     {
  143.                         el[i].koeficijent += el[j].koeficijent;
  144.                         el.RemoveAt(j);//izbrise taj drugi sto je nasao posle sabiranja
  145.                         j--;//dolazi neki novi el na indeks njegov pa smanjuje j
  146.                     }
  147.                 }
  148.             }
  149.         }
  150.         public double racunanjeVrednosti(double x)//sredjivanje i racuna
  151.         {
  152.             sredjivanje();
  153.             double vrednost = 0;
  154.             for (int i = 0; i < el.Count; i++)
  155.             {
  156.                 vrednost += el[i].koeficijent * (Math.Pow(x, el[i].stepen));
  157.             }
  158.             return vrednost;
  159.         }
  160.  
  161.         public static Polinom operator +(Polinom aa,Polinom bb)
  162.         {
  163.             Polinom a=new Polinom(aa);
  164.             Polinom b = new Polinom(bb);
  165.             Polinom c = new Polinom();//zbir ova prethodna dva
  166.             for (int i = 0; i < a.el.Count; i++)//prolazi kroz prvi
  167.             {
  168.                 bool imaIstiStepen = false;
  169.                 for (int j = 0; j < b.el.Count; j++)//proveravamo da li u ovom drugom ima neki sa istim stepenom
  170.                 {
  171.                     if (a.el[i].stepen == b.el[j].stepen)
  172.                     {
  173.                         imaIstiStepen = true;//nasao
  174.                         Element zbir=new Element(a.el[i].koeficijent+b.el[j].koeficijent,a.el[i].stepen);
  175.                         a.el.RemoveAt(i);
  176.                         i--;
  177.                         b.el.RemoveAt(j);//brisemo i jedan i drugi iz polinoma jer smo dobili jedan novi
  178.                         j--;
  179.                         c.el.Add(zbir);//zbir
  180.                         break;
  181.                     }
  182.                 }
  183.  
  184.                 if (!imaIstiStepen)
  185.                 {
  186.                     c.el.Add(a.el[i]);//ako nije naslo onda samo doda njega na polinom
  187.                 }
  188.             }
  189.  
  190.             //dodajemo ono sto je ostalo od b
  191.             for(int i=0;i<b.el.Count;i++)
  192.             {
  193.                 c.el.Add(b.el[i]);//ako je nesto ostalo u drugom polinomu to dodamo na konacni polinom
  194.             }
  195.             c.sortiraj();//
  196.             return c;
  197.         }
  198.         public static Polinom operator -(Polinom aa, Polinom bb)
  199.         {
  200.             Polinom a = new Polinom(aa);
  201.             Polinom b = new Polinom(bb);
  202.             Polinom c = new Polinom();
  203.             for (int i = 0; i < a.el.Count; i++)
  204.             {
  205.                 bool imaIstiStepen = false;
  206.                 for (int j = 0; j < b.el.Count; j++)
  207.                 {
  208.                     if (a.el[i].stepen == b.el[j].stepen)
  209.                     {
  210.                         imaIstiStepen = true;
  211.                         Element raz = new Element(a.el[i].koeficijent - b.el[j].koeficijent, a.el[i].stepen);
  212.                         a.el.RemoveAt(i);
  213.                         i--;
  214.                         b.el.RemoveAt(j);
  215.                         j--;
  216.                         c.el.Add(raz);
  217.                         break;
  218.                     }
  219.                 }
  220.  
  221.                 if (!imaIstiStepen)
  222.                 {
  223.                     c.el.Add(a.el[i]);
  224.                 }
  225.             }
  226.  
  227.             //dodajemo ono sto je ostalo od b, ali menjamo znak
  228.             for (int i = 0; i < b.el.Count; i++)
  229.             {
  230.                 Element suprotnoOdB = new Element(-1 * b.el[i].koeficijent, b.el[i].stepen);
  231.                 c.el.Add(suprotnoOdB);//u drugom polinomu se menja znak svakome delu pa zato mnozimo sa -1
  232.             }
  233.             c.sredjivanje();
  234.             c.sortiraj();
  235.             return c;
  236.         }
  237.         public static Polinom operator *(Polinom aa, Polinom bb)
  238.         {
  239.             Polinom a = new Polinom(aa);
  240.             Polinom b = new Polinom(bb);
  241.             Polinom c = new Polinom();
  242.  
  243.             for (int i = 0; i < a.el.Count; i++)
  244.             {
  245.                 for (int j = 0; j < b.el.Count; j++)
  246.                 {
  247.                     Element proizvod = new Element(a.el[i].koeficijent * b.el[j].koeficijent, a.el[i].stepen + b.el[j].stepen);
  248.                     c.el.Add(proizvod);//svaki sa svakim se pomnozi(koef),a svaki stepen se sabira
  249.                 }
  250.             }
  251.             c.sredjivanje();//sredimo
  252.             return c;
  253.         }
  254.         public static string operator /(Polinom aa, Polinom bb)
  255.         {
  256.             Polinom a = new Polinom(aa);
  257.             Polinom b = new Polinom(bb);
  258.             Polinom c = new Polinom();
  259.             Polinom ostatak=new Polinom();
  260.             while(true)//beskonacna petlja do kraja kada vidimo da nema nista vise
  261.             {
  262.                 //prvo delimo, pa mnozimo ,i oduzmemo do kraja
  263.                 Element kolicnik = new Element(a.el[0].koeficijent / b.el[0].koeficijent, a.el[0].stepen - b.el[0].stepen);
  264.                 c.el.Add(kolicnik);//c je kolicnik
  265.                 Polinom proiz = new Polinom(kolicnik);
  266.                 proiz = proiz * b;//rezultat pomnozimo sa deliocem
  267.                 a = a - proiz;//ono sto smo dobili oduzmemo x*delioc od a
  268.                
  269.                 //izbacivanje nula
  270.                 for (int j = 0; j < a.el.Count; j++)
  271.                 {
  272.                     if (a.el[j].koeficijent == 0)//ako imamo nule u rez izbacujemo
  273.                     {
  274.                         a.el.RemoveAt(j);
  275.                         j--;
  276.                     }
  277.                 }
  278.                 if (a.el.Count == 0)
  279.                 {
  280.  
  281.                     break;//ako nema vise nista u petlji brejk
  282.                 }
  283.                 if (a.el[0].stepen < b.el[0].stepen)//ako je stepen a manji od stepena b ,onda je a ostatak
  284.                 {
  285.                     ostatak=a;
  286.                     break;
  287.             }
  288.             }
  289.             return c.ToString()+"    "+ostatak.ToString();//returnuje u stringu kolicnik i ostatak
  290.         }
  291.  
  292.         public override string ToString()//da ne izbacuje u kada se napise x2 i x da ne pise 1x2 i 1x1
  293.         {
  294.             string s = "";
  295.             for (int i = 0; i < el.Count; i++)//radi lepseg prikaza da ne pisemo -1x umesto -x jer je lepsi
  296.             {          
  297.                     if (el[i].koeficijent > 0 && i > 0)
  298.                         s += "+";
  299.                     if (el[i].koeficijent == 1 && el[i].stepen == 0)
  300.                         s += el[i].koeficijent.ToString();
  301.                     if (el[i].koeficijent > 0 && el[i].koeficijent != 1)
  302.                         s += el[i].koeficijent.ToString();
  303.  
  304.                     else
  305.                     {
  306.                         if (el[i].koeficijent < 0)
  307.                         {
  308.                             if (el[i].koeficijent == -1)
  309.                             {
  310.                                 s += "-";
  311.                                 if (el[i].stepen == 0)
  312.                                     s += "1";
  313.                             }
  314.                             else
  315.                             {
  316.                                 s += el[i].koeficijent.ToString();
  317.                             }
  318.                         }
  319.                     }
  320.                
  321.                 if (el[i].stepen != 0)//ako nije 0 stavljamo x, ako jeste 0 nema x
  322.                 {
  323.                     s += "x";
  324.                     if (el[i].stepen != 1)
  325.                     {
  326.                         s += el[i].stepen.ToString();
  327.                     }
  328.                 }
  329.  
  330.             }
  331.             return s;
  332.         }
  333.  
  334.     }
  335. }
  336.  
  337.  
  338.  
  339.  
  340.  
  341. form1.cs
  342.  
  343. using System;
  344. using System.Collections.Generic;
  345. using System.ComponentModel;
  346. using System.Data;
  347. using System.Drawing;
  348. using System.Linq;
  349. using System.Text;
  350. using System.Threading.Tasks;
  351. using System.Windows.Forms;
  352.  
  353. namespace Polinomi
  354. {
  355.     public partial class Form1 : Form
  356.     {
  357.         public Form1()
  358.         {
  359.             InitializeComponent();
  360.         }
  361.  
  362.         private void button1_Click(object sender, EventArgs e)
  363.         {
  364.             listBox1.Items.Clear();
  365.             listBox2.Items.Clear();
  366.             listBox3.Items.Clear();
  367.             string s = textBox1.Text;
  368.             Polinom a = new Polinom(s);
  369.             s = textBox2.Text;
  370.             Polinom b = new Polinom(s);
  371.             for (int i = 0; i < a.el.Count; i++)
  372.             {
  373.                 listBox1.Items.Add(a.el[i].koeficijent+"  "+a.el[i].stepen);
  374.             }
  375.             for (int i = 0; i < b.el.Count; i++)
  376.             {
  377.                 listBox3.Items.Add(b.el[i].koeficijent + "  " + b.el[i].stepen);
  378.             }
  379.             Polinom zbir = a + b;
  380.             for (int i = 0; i < zbir.el.Count; i++)
  381.             {
  382.                 listBox2.Items.Add(zbir.el[i].koeficijent + "  " + zbir.el[i].stepen);
  383.             }
  384.             label1.Text = zbir.ToString();
  385.         }
  386.  
  387.         private void button2_Click(object sender, EventArgs e)
  388.         {
  389.             listBox1.Items.Clear();
  390.             listBox2.Items.Clear();
  391.             listBox3.Items.Clear();
  392.             string s = textBox1.Text;
  393.             Polinom a = new Polinom(s);
  394.             s = textBox2.Text;
  395.             Polinom b = new Polinom(s);
  396.             for (int i = 0; i < a.el.Count; i++)
  397.             {
  398.                 listBox1.Items.Add(a.el[i].koeficijent + "  " + a.el[i].stepen);
  399.             }
  400.             for (int i = 0; i < b.el.Count; i++)
  401.             {
  402.                 listBox3.Items.Add(b.el[i].koeficijent + "  " + b.el[i].stepen);
  403.             }
  404.             Polinom razlika = a - b;
  405.             for (int i = 0; i < razlika.el.Count; i++)
  406.             {
  407.                 listBox2.Items.Add(razlika.el[i].koeficijent + "  " + razlika.el[i].stepen);
  408.             }
  409.             label1.Text = razlika.ToString();
  410.         }
  411.  
  412.         private void button3_Click(object sender, EventArgs e)
  413.         {
  414.             listBox1.Items.Clear();
  415.             listBox2.Items.Clear();
  416.             listBox3.Items.Clear();
  417.             string s = textBox1.Text;
  418.             Polinom a = new Polinom(s);
  419.             s = textBox2.Text;
  420.             Polinom b = new Polinom(s);
  421.             for (int i = 0; i < a.el.Count; i++)
  422.             {
  423.                 listBox1.Items.Add(a.el[i].koeficijent + "  " + a.el[i].stepen);
  424.             }
  425.             for (int i = 0; i < b.el.Count; i++)
  426.             {
  427.                 listBox3.Items.Add(b.el[i].koeficijent + "  " + b.el[i].stepen);
  428.             }
  429.             Polinom proizvod = a * b;
  430.             for (int i = 0; i < proizvod.el.Count; i++)
  431.             {
  432.                 listBox2.Items.Add(proizvod.el[i].koeficijent + "  " + proizvod.el[i].stepen);
  433.             }
  434.             label1.Text = proizvod.ToString();
  435.         }
  436.  
  437.         private void button4_Click(object sender, EventArgs e)
  438.         {
  439.             string s = textBox3.Text;
  440.             double x=Convert.ToDouble(textBox4.Text);
  441.             Polinom a = new Polinom(s);//racunanje vrednosti
  442.             label4.Text = a.racunanjeVrednosti(x).ToString();
  443.         }
  444.  
  445.         private void button5_Click(object sender, EventArgs e)
  446.         {
  447.             string s = textBox5.Text;
  448.             Polinom a = new Polinom(s);//imamo vec metodu u konstruktoru metode sredjivanje i sortiranje(po defaultu)
  449.             label6.Text = a.ToString();//i nismo ovde nista pisali samo pretvorimo i ispisemo
  450.         }
  451.  
  452.         private void button6_Click(object sender, EventArgs e)
  453.         {
  454.             listBox1.Items.Clear();
  455.             listBox2.Items.Clear();
  456.             listBox3.Items.Clear();
  457.             string s = textBox1.Text;
  458.             Polinom a = new Polinom(s);
  459.             s = textBox2.Text;
  460.             Polinom b = new Polinom(s);
  461.             for (int i = 0; i < a.el.Count; i++)
  462.             {
  463.                 listBox1.Items.Add(a.el[i].koeficijent + "  " + a.el[i].stepen);
  464.             }
  465.             for (int i = 0; i < b.el.Count; i++)
  466.             {
  467.                 listBox3.Items.Add(b.el[i].koeficijent + "  " + b.el[i].stepen);
  468.             }
  469.             string proizvod = a / b;
  470.           //  for (int i = 0; i < proizvod.el.Count; i++)
  471.          //   {
  472.          //       listBox2.Items.Add(proizvod.el[i].koeficijent + "  " + proizvod.el[i].stepen);
  473.          //   }
  474.             label1.Text = proizvod;
  475.         }
  476.     }
  477. }
  478.  
  479.  
  480. element.cs
  481. tu nema nijedan komentar hahahah
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement