------------------------------- -- CISC 672 -- -- programming assignment 1 -- -- author: Timo Koetzing -- ------------------------------- -- The program: taking coefficients to a polynomial as input, -- returning the derivative of the polynomial -- the following class provides a single linked list for integers class SingleLinkedListInteger { next : SingleLinkedListInteger; data : Int; init(l : SingleLinkedListInteger, n : Int) : SELF_TYPE { { next <- l; data <- n; self; } }; setData(n : Int) : Int { data <- n }; getData(): Int { data }; setNext(l : SingleLinkedListInteger) : SingleLinkedListInteger { next <- l }; getNext() : SingleLinkedListInteger { next }; }; -- the following class makes use of the single-linked list to implement a queue class QueueInteger { frontBumper : SingleLinkedListInteger <- new SingleLinkedListInteger; last : SingleLinkedListInteger <- frontBumper; size : Int <- 0; enqueue(n: Int) : SELF_TYPE { { last.setNext(new SingleLinkedListInteger); last <- last.getNext(); last.setData(n); size <- size + 1; self; } }; dequeue() : Int { { if 0 < size then let n : Int <- frontBumper.getNext().getData() in { frontBumper.setNext(frontBumper.getNext().getNext()); size <- size - 1; n; } else 0 fi; } }; getSize() : Int { size }; isEmpty() : Bool { getSize() = 0 }; }; -- it follows the main class for with in- and output class Main { main() : Int { let myIO : IO <- (new IO), i : Int, j : Int, q : QueueInteger <- new QueueInteger, n : Int in { myIO.out_string("### CALCULATING THE DERIVITIVE OF A POLYNOMIAL ###\n"); myIO.out_string("What shall the degree of your polynomial be? "); i <- myIO.in_int(); j <- i - 1; -- j is the variable for the countdown later for the output of the derivative myIO.out_string("Please start entering the coefficients, starting with the highest:\n"); while 0 < i + 1 loop { myIO.out_string("Coefficient for x^"); myIO.out_int(i); myIO.out_string(": "); n <- myIO.in_int(); -- forget the coefficient for the constant part: if i = 0 then 0 else q.enqueue(n) fi; i <- i - 1; } pool; while not q.isEmpty() loop { n <- (1 + j) * q.dequeue(); myIO.out_int(n); myIO.out_string("x^"); myIO.out_int(j); j <- j - 1; if not q.isEmpty() then myIO.out_string(" + ") else myIO.out_string("\n") fi; } pool; 1; } }; };