From: Doni Pracner Date: Wed, 6 May 2015 00:09:55 +0000 (+0200) Subject: PolinomL, primer Paskalov trougao X-Git-Url: https://svarog.pmf.uns.ac.rs/gitweb/?a=commitdiff_plain;h=6c5ac2e54e219a8130d7f42023d3faf287841ab2;p=spa1-materijali.git PolinomL, primer Paskalov trougao --- diff --git a/kodovi/polinomi/Paskal.java b/kodovi/polinomi/Paskal.java new file mode 100644 index 0000000..fecbfb1 --- /dev/null +++ b/kodovi/polinomi/Paskal.java @@ -0,0 +1,73 @@ +public class Paskal { + + static final int d = 4; /* koliko mesta zauzima pojedinacni broj */ + + static String sep = ""; + + static void postaviSeparator() { + sep = ""; + for (int i = 0; i < d; i++) { + sep += ' '; + } + } + + static void poravnanje(int mesta) { + for (int i = 0; i < mesta; i++) { + System.out.print(sep); + } + } + + static void stampajKoeficijente(PolinomL p, int mestaLevo) { + poravnanje(mestaLevo); + int maksimalniStepen = p.maksimalniStepen(); + for (int stepen = 0; stepen <= maksimalniStepen; stepen++) { + Svetovid.out.print(String.format("%" + d + ".0f", + p.koeficijentUz(stepen))); + System.out.print(sep); + } + } + + static void paskalovTrougao(int n) { + PolinomL poli = new PolinomL(); + poli.postaviClan(1.0, 0); + + PolinomL xPlus1 = new PolinomL(); + xPlus1.postaviClan(1.0, 1); + xPlus1.postaviClan(1.0, 0); + + stampajKoeficijente(poli, n - 1); + System.out.println(); + for (int i = 2; i <= n; i++) { + poli = poli.puta(xPlus1); + stampajKoeficijente(poli, n - i); + System.out.println(); + } + } + + // jednostavna varijanta koja odmah stampa polinom na ekran - tacno, ali + // nije predstavljeno kako treba + static void paskalovTrougaoDirektno(int n) { + PolinomL poli = new PolinomL(); + poli.postaviClan(1.0, 0); + + PolinomL xPlus1 = new PolinomL(); + xPlus1.postaviClan(1.0, 1); + xPlus1.postaviClan(1.0, 0); + + System.out.println(poli); + for (int i = 2; i <= n; i++) { + poli = poli.puta(xPlus1); + System.out.println(poli); + } + } + + public static void main(String[] args) { + System.out.println("Program stampa Paskalov trougao"); + System.out.println(); + System.out.println("Unesite broj do koga stampamo: "); + System.out.println(); + int broj = Svetovid.in.readInt(); + postaviSeparator(); + paskalovTrougao(broj); + } +}