Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gorjan.lab1;
- public class PolynomTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Polynomal pol1 = new Polynomal(new Term [] {new Term(3,4), new Term(2,2), new Term(4,1), new Term(8,0)});
- Polynomal pol2 = new Polynomal(new Term [] {new Term(3,2), new Term(8,0)});
- Polynomal rez = pol1.add(pol2);
- System.out.println(rez);
- }
- }
- final class Term {
- final double c;
- final int e;
- public Term(double c, int e) {
- this.c = c;
- this.e = e;
- }
- public double getCoeficient() {
- return c;
- }
- public int getExponent() {
- return e;
- }
- public Term negate(){
- Term tmp = new Term(this.c*(-1), this.e);
- return tmp;
- }
- public Term add(Term t){
- Term tmp;
- double c_t;
- if(this.e == t.e){
- c_t = this.c + t.c;
- tmp = new Term(c_t, this.e);
- return tmp;
- }
- else return null;
- }
- @Override
- public String toString() {
- return c+"x^"+e;
- }
- public Term multiply(Term t){
- Term tmp;
- double c_t;
- int e_t;
- c_t = this.c * t.c;
- e_t = this.e + t.e;
- tmp = new Term(c_t, e_t);
- return tmp;
- }
- public double evaluate(double x){
- return this.c * Math.pow(x, this.e);
- }
- public boolean equals(Object obj){
- Term tmp = (Term) obj;
- return((this.e == tmp.e)&&(this.c == tmp.c));
- }
- public int compareTo(Term t){
- if(this.e<t.e) return -1;
- else if(this.e==t.e) return 0;
- else return 1;
- }
- }
- final class Polynomal {
- Term [] terms;
- public Polynomal(Term []terms){
- this.terms = new Term[terms.length];
- for(int i = 0; i < terms.length ; i++){
- this.terms[i] = terms[i];
- }
- }
- public Term getTerm(int exp){
- for(int i = 0; i < this.terms.length ; i++){
- if(terms[i].e == exp) return terms[i];
- }
- return null;
- }
- public Polynomal negate(){
- Polynomal tmp = new Polynomal(this.terms);
- for(int i = 0; i < this.terms.length ; i++){
- tmp.terms[i] = tmp.terms[i].negate();
- }
- return tmp;
- }
- public Polynomal add(Polynomal t){;
- int cur1=0, cur2=0, i=0;
- Polynomal tmp = new Polynomal(new Term[100]);
- while((cur1<=this.terms.length- 1)||(cur2<=t.terms.length-1)){
- if(this.terms[cur1].e == t.terms[cur2].e){
- tmp.terms[i] = new Term(this.terms[cur1].c + t.terms[cur2].c,this.terms[cur1].e);
- cur1++;
- cur2++;
- System.out.println("Prv");
- }
- else if(this.terms[cur1].e > t.terms[cur2].e){
- tmp.terms[i] = new Term(this.terms[cur1].c,this.terms[cur1].e);
- cur1++;
- System.out.println("Vtor");
- }
- else if(this.terms[cur1].e < t.terms[cur2].e){
- tmp.terms[i] = new Term(t.terms[cur2].c,t.terms[cur2].e);
- cur2++;
- System.out.println("Tret");
- }
- i++;
- if((cur1 == this.terms.length)||(cur2 == t.terms.length)) break;
- }
- System.out.println("Krajno Cur1: "+cur1+" Cur2: "+cur2);
- while(cur1 < this.terms.length){
- tmp.terms[i] = new Term(this.terms[cur1].c, this.terms[cur1].e);
- cur1++;
- i++;
- System.out.println("Cur1: "+cur1+" Cur2: "+cur2);
- }
- while(cur2 < t.terms.length){
- tmp.terms[i] = new Term(t.terms[cur2].c, t.terms[cur2].e);
- cur2++;
- i++;
- }
- Polynomal nova = new Polynomal(new Term[i]);
- for(int n = 0; n < i ; n++){
- nova.terms[n] = tmp.terms[n];
- }
- return nova;
- }
- @Override
- public String toString(){
- String a = "";
- System.out.println("A: "+ this.terms.length);
- for(int i = 0; i < this.terms.length;i++){
- a += this.terms[i].toString() + " + ";
- }
- return a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement