Advertisement
mvujas

mnozenje polinoma

May 9th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.75 KB | None | 0 0
  1. object App {
  2.     class Polinom(koeficijenti: Array[Int]) {
  3.         def *(A: Polinom): Polinom = {
  4.             val n = this.level() + A.level() - 1
  5.             val P1 = this.getK()
  6.             val P2 = A.getK()
  7.            
  8.             var res = Array[Int]()
  9.             for(i <- 0 until n)
  10.                 res = res :+ 0
  11.            
  12.             for(i <- 0 until this.level(); j <- 0 until A.level())
  13.                 res(i + j) += P1(i) * P2(j)
  14.  
  15.             new Polinom(res)
  16.         }
  17.    
  18.         def level(): Int = this.koeficijenti.length
  19.         def getK(): Array[Int] = koeficijenti
  20.     }
  21.     def main(args: Array[String]) {
  22.         var A = new Polinom(Array(1, 2, 3))
  23.         var B = new Polinom(Array(3, 2, 1))
  24.         var C = A * B
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement