Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- polinomi.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Polinomi
- {
- class Polinom
- {
- public List<Element> el=new List<Element>();
- public Polinom(Element elem)
- {
- el.Add(elem);
- }
- public Polinom()
- {
- }
- public Polinom(Polinom a)
- {
- el = a.el;
- }
- public Polinom(string s)
- {
- s.ToCharArray();
- List<int> indeksiZnakova = new List<int>();
- List<string> delovi=new List<string>();
- indeksiZnakova.Add(0);
- //delimo string na manje delove
- for (int i = 0; i < s.Length; i++)
- {
- if (String.Equals(s[i], '+') || String.Equals(s[i], '-'))
- if(i!=0)
- indeksiZnakova.Add(i);
- }
- //znak na prvom delu polinoma dal je + ili - na pocetku to gleda
- indeksiZnakova.Add(s.Length);
- for (int i = 0; i < indeksiZnakova.Count-1; i++)
- {
- string deo=s.Substring(indeksiZnakova[i],indeksiZnakova[i+1]-indeksiZnakova[i]);
- if (!String.Equals(deo, ""))
- {
- delovi.Add(deo);
- }
- }
- //podelili string na delove , sad pravimo elemente
- for (int i = 0; i < delovi.Count; i++)
- {
- delovi[i].ToCharArray();
- string prefiks = "1";
- int j = 0;
- if (String.Equals(delovi[i][0], '+'))//prefiks preskacemo jer je po defaultu vec 1
- {
- j = 1;
- }
- if (String.Equals(delovi[i][0], '-'))//prefiks menjamo u -1 ako ispred x-a nema nista
- {
- j = 1;
- prefiks = "-1";
- }
- string sufiks = "0";//po defaultu
- if (delovi[i][j] >= '0' && delovi[i][j] <= '9')
- {
- prefiks = "";
- if (String.Equals(delovi[i][0], '-'))
- prefiks = "-";
- while (delovi[i][j] >= '0' && delovi[i][j] <= '9')//ako je broj ispred dodaje do x-a kao koef
- {
- prefiks += delovi[i][j];//dodajemo do pocetka x-a
- j++;
- if (j == delovi[i].Length)
- break;
- if (String.Equals('.', delovi[i][j]))
- {
- prefiks += ".";//u slucaju da je realan broj/double
- j++;
- }
- }
- }
- if (j < delovi[i].Length)
- {
- if (String.Equals('x',delovi[i][j]))
- {
- sufiks = "1";
- j++;
- }
- }
- if (j < delovi[i].Length)
- {
- sufiks = "";
- while (delovi[i][j] >= '0' && delovi[i][j] <= '9')//sufiks ide do kraja dodaje i ako je double i ne moze biti negativan
- {
- sufiks += delovi[i][j];
- j++;
- if (j == delovi[i].Length)
- break;
- if (String.Equals('.', delovi[i][j]))
- {
- sufiks += ".";
- j++;
- }
- }
- }
- Element a = new Element(Convert.ToDouble(prefiks), Convert.ToDouble(sufiks));
- el.Add(a);
- }
- sredjivanje();
- sortiraj();
- }
- public void sortiraj()
- {
- //sortiranjeListePoOpadajucemRedosledu
- List<Element> sort = new List<Element>();//pravi se nova lista koja je sortirana
- while (el.Count > 0)
- {
- double maxStepen = -1;
- int indeksMaxStepen = 0;
- for (int i = 0; i < el.Count; i++)
- {
- if (el[i].stepen > maxStepen)
- {
- maxStepen = el[i].stepen;
- indeksMaxStepen = i;
- }
- }
- sort.Add(el[indeksMaxStepen]);//dodamo u listu njega sortiran
- el.RemoveAt(indeksMaxStepen);//brisemo iz starog nesortiranog izbrise
- }
- el = sort;
- }
- public void sredjivanje() //ako ima vise njih istog stepena sabere ih proverava da li ima
- {
- for (int i = 0; i < el.Count-1; i++)
- {
- for (int j = i + 1; j < el.Count; j++)
- {
- if (el[i].stepen == el[j].stepen)
- {
- el[i].koeficijent += el[j].koeficijent;
- el.RemoveAt(j);//izbrise taj drugi sto je nasao posle sabiranja
- j--;//dolazi neki novi el na indeks njegov pa smanjuje j
- }
- }
- }
- }
- public double racunanjeVrednosti(double x)//sredjivanje i racuna
- {
- sredjivanje();
- double vrednost = 0;
- for (int i = 0; i < el.Count; i++)
- {
- vrednost += el[i].koeficijent * (Math.Pow(x, el[i].stepen));
- }
- return vrednost;
- }
- public static Polinom operator +(Polinom aa,Polinom bb)
- {
- Polinom a=new Polinom(aa);
- Polinom b = new Polinom(bb);
- Polinom c = new Polinom();//zbir ova prethodna dva
- for (int i = 0; i < a.el.Count; i++)//prolazi kroz prvi
- {
- bool imaIstiStepen = false;
- for (int j = 0; j < b.el.Count; j++)//proveravamo da li u ovom drugom ima neki sa istim stepenom
- {
- if (a.el[i].stepen == b.el[j].stepen)
- {
- imaIstiStepen = true;//nasao
- Element zbir=new Element(a.el[i].koeficijent+b.el[j].koeficijent,a.el[i].stepen);
- a.el.RemoveAt(i);
- i--;
- b.el.RemoveAt(j);//brisemo i jedan i drugi iz polinoma jer smo dobili jedan novi
- j--;
- c.el.Add(zbir);//zbir
- break;
- }
- }
- if (!imaIstiStepen)
- {
- c.el.Add(a.el[i]);//ako nije naslo onda samo doda njega na polinom
- }
- }
- //dodajemo ono sto je ostalo od b
- for(int i=0;i<b.el.Count;i++)
- {
- c.el.Add(b.el[i]);//ako je nesto ostalo u drugom polinomu to dodamo na konacni polinom
- }
- c.sortiraj();//
- return c;
- }
- public static Polinom operator -(Polinom aa, Polinom bb)
- {
- Polinom a = new Polinom(aa);
- Polinom b = new Polinom(bb);
- Polinom c = new Polinom();
- for (int i = 0; i < a.el.Count; i++)
- {
- bool imaIstiStepen = false;
- for (int j = 0; j < b.el.Count; j++)
- {
- if (a.el[i].stepen == b.el[j].stepen)
- {
- imaIstiStepen = true;
- Element raz = new Element(a.el[i].koeficijent - b.el[j].koeficijent, a.el[i].stepen);
- a.el.RemoveAt(i);
- i--;
- b.el.RemoveAt(j);
- j--;
- c.el.Add(raz);
- break;
- }
- }
- if (!imaIstiStepen)
- {
- c.el.Add(a.el[i]);
- }
- }
- //dodajemo ono sto je ostalo od b, ali menjamo znak
- for (int i = 0; i < b.el.Count; i++)
- {
- Element suprotnoOdB = new Element(-1 * b.el[i].koeficijent, b.el[i].stepen);
- c.el.Add(suprotnoOdB);//u drugom polinomu se menja znak svakome delu pa zato mnozimo sa -1
- }
- c.sredjivanje();
- c.sortiraj();
- return c;
- }
- public static Polinom operator *(Polinom aa, Polinom bb)
- {
- Polinom a = new Polinom(aa);
- Polinom b = new Polinom(bb);
- Polinom c = new Polinom();
- for (int i = 0; i < a.el.Count; i++)
- {
- for (int j = 0; j < b.el.Count; j++)
- {
- Element proizvod = new Element(a.el[i].koeficijent * b.el[j].koeficijent, a.el[i].stepen + b.el[j].stepen);
- c.el.Add(proizvod);//svaki sa svakim se pomnozi(koef),a svaki stepen se sabira
- }
- }
- c.sredjivanje();//sredimo
- return c;
- }
- public static string operator /(Polinom aa, Polinom bb)
- {
- Polinom a = new Polinom(aa);
- Polinom b = new Polinom(bb);
- Polinom c = new Polinom();
- Polinom ostatak=new Polinom();
- while(true)//beskonacna petlja do kraja kada vidimo da nema nista vise
- {
- //prvo delimo, pa mnozimo ,i oduzmemo do kraja
- Element kolicnik = new Element(a.el[0].koeficijent / b.el[0].koeficijent, a.el[0].stepen - b.el[0].stepen);
- c.el.Add(kolicnik);//c je kolicnik
- Polinom proiz = new Polinom(kolicnik);
- proiz = proiz * b;//rezultat pomnozimo sa deliocem
- a = a - proiz;//ono sto smo dobili oduzmemo x*delioc od a
- //izbacivanje nula
- for (int j = 0; j < a.el.Count; j++)
- {
- if (a.el[j].koeficijent == 0)//ako imamo nule u rez izbacujemo
- {
- a.el.RemoveAt(j);
- j--;
- }
- }
- if (a.el.Count == 0)
- {
- break;//ako nema vise nista u petlji brejk
- }
- if (a.el[0].stepen < b.el[0].stepen)//ako je stepen a manji od stepena b ,onda je a ostatak
- {
- ostatak=a;
- break;
- }
- }
- return c.ToString()+" "+ostatak.ToString();//returnuje u stringu kolicnik i ostatak
- }
- public override string ToString()//da ne izbacuje u kada se napise x2 i x da ne pise 1x2 i 1x1
- {
- string s = "";
- for (int i = 0; i < el.Count; i++)//radi lepseg prikaza da ne pisemo -1x umesto -x jer je lepsi
- {
- if (el[i].koeficijent > 0 && i > 0)
- s += "+";
- if (el[i].koeficijent == 1 && el[i].stepen == 0)
- s += el[i].koeficijent.ToString();
- if (el[i].koeficijent > 0 && el[i].koeficijent != 1)
- s += el[i].koeficijent.ToString();
- else
- {
- if (el[i].koeficijent < 0)
- {
- if (el[i].koeficijent == -1)
- {
- s += "-";
- if (el[i].stepen == 0)
- s += "1";
- }
- else
- {
- s += el[i].koeficijent.ToString();
- }
- }
- }
- if (el[i].stepen != 0)//ako nije 0 stavljamo x, ako jeste 0 nema x
- {
- s += "x";
- if (el[i].stepen != 1)
- {
- s += el[i].stepen.ToString();
- }
- }
- }
- return s;
- }
- }
- }
- form1.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Polinomi
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- listBox1.Items.Clear();
- listBox2.Items.Clear();
- listBox3.Items.Clear();
- string s = textBox1.Text;
- Polinom a = new Polinom(s);
- s = textBox2.Text;
- Polinom b = new Polinom(s);
- for (int i = 0; i < a.el.Count; i++)
- {
- listBox1.Items.Add(a.el[i].koeficijent+" "+a.el[i].stepen);
- }
- for (int i = 0; i < b.el.Count; i++)
- {
- listBox3.Items.Add(b.el[i].koeficijent + " " + b.el[i].stepen);
- }
- Polinom zbir = a + b;
- for (int i = 0; i < zbir.el.Count; i++)
- {
- listBox2.Items.Add(zbir.el[i].koeficijent + " " + zbir.el[i].stepen);
- }
- label1.Text = zbir.ToString();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- listBox1.Items.Clear();
- listBox2.Items.Clear();
- listBox3.Items.Clear();
- string s = textBox1.Text;
- Polinom a = new Polinom(s);
- s = textBox2.Text;
- Polinom b = new Polinom(s);
- for (int i = 0; i < a.el.Count; i++)
- {
- listBox1.Items.Add(a.el[i].koeficijent + " " + a.el[i].stepen);
- }
- for (int i = 0; i < b.el.Count; i++)
- {
- listBox3.Items.Add(b.el[i].koeficijent + " " + b.el[i].stepen);
- }
- Polinom razlika = a - b;
- for (int i = 0; i < razlika.el.Count; i++)
- {
- listBox2.Items.Add(razlika.el[i].koeficijent + " " + razlika.el[i].stepen);
- }
- label1.Text = razlika.ToString();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- listBox1.Items.Clear();
- listBox2.Items.Clear();
- listBox3.Items.Clear();
- string s = textBox1.Text;
- Polinom a = new Polinom(s);
- s = textBox2.Text;
- Polinom b = new Polinom(s);
- for (int i = 0; i < a.el.Count; i++)
- {
- listBox1.Items.Add(a.el[i].koeficijent + " " + a.el[i].stepen);
- }
- for (int i = 0; i < b.el.Count; i++)
- {
- listBox3.Items.Add(b.el[i].koeficijent + " " + b.el[i].stepen);
- }
- Polinom proizvod = a * b;
- for (int i = 0; i < proizvod.el.Count; i++)
- {
- listBox2.Items.Add(proizvod.el[i].koeficijent + " " + proizvod.el[i].stepen);
- }
- label1.Text = proizvod.ToString();
- }
- private void button4_Click(object sender, EventArgs e)
- {
- string s = textBox3.Text;
- double x=Convert.ToDouble(textBox4.Text);
- Polinom a = new Polinom(s);//racunanje vrednosti
- label4.Text = a.racunanjeVrednosti(x).ToString();
- }
- private void button5_Click(object sender, EventArgs e)
- {
- string s = textBox5.Text;
- Polinom a = new Polinom(s);//imamo vec metodu u konstruktoru metode sredjivanje i sortiranje(po defaultu)
- label6.Text = a.ToString();//i nismo ovde nista pisali samo pretvorimo i ispisemo
- }
- private void button6_Click(object sender, EventArgs e)
- {
- listBox1.Items.Clear();
- listBox2.Items.Clear();
- listBox3.Items.Clear();
- string s = textBox1.Text;
- Polinom a = new Polinom(s);
- s = textBox2.Text;
- Polinom b = new Polinom(s);
- for (int i = 0; i < a.el.Count; i++)
- {
- listBox1.Items.Add(a.el[i].koeficijent + " " + a.el[i].stepen);
- }
- for (int i = 0; i < b.el.Count; i++)
- {
- listBox3.Items.Add(b.el[i].koeficijent + " " + b.el[i].stepen);
- }
- string proizvod = a / b;
- // for (int i = 0; i < proizvod.el.Count; i++)
- // {
- // listBox2.Items.Add(proizvod.el[i].koeficijent + " " + proizvod.el[i].stepen);
- // }
- label1.Text = proizvod;
- }
- }
- }
- element.cs
- tu nema nijedan komentar hahahah
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement