Implement shift register class to support the plus (‘+’) and minus (‘-‘) operato
ID: 3599052 • Letter: I
Question
Implement shift register class to support the plus (‘+’)
and minus (‘-‘) operator. The semantic of those operators would be that of binary addition and
subtraction. You most modify your command line argument parser to support passing two registers as
follows: (ARGUMENTS MUST BE SUPPORTED IN ANY ORDER.)
by default your register must inject 0 values unless told otherwise via command line arguments.
Here are some of the parameters to support as your command line arguments:
-i “010101”: Initial values stored in your register1. . This option must accept a positive or negative
integer and covert it to binary.
-s 6: Specifies the number of bits in your shift register1.
-r 2: shift the register1 right by two positions.
-l 3: shift left register1 by 3 positions.
-I “010101”: Initial values stored in your register2. This option must accept a positive or negative integer
and covert it to binary.
-S 6: Specifies the number of bits in your shift register2.
-R 2: shift the register2 right by two positions.
-L 3: shift left register2 by 3 positions.
-o +/-: operator to be used among the registers if any
-v 1: value to inject in vacated bits if other than default.
-d: if this option is given the resulting registers are followed by their representation in Decimal as
follows: 0111(7)
-h: as above but in hex:1111(0xF)
-p: prints the value of bits in your register after performing all the operations. Bits must be printed as a
non-spaced string of 0 or 1s and terminated by a new line. One line for register1, one line for register2
and the last line contains the result in binary. Implement the << operator in your register class so you
can output using cout << register
You must turn in ONLY ONE UNCOMPRESSED TEXT FILE (NO INCLUDES, NO BINARIES): the c++ file
named shiftregister.cpp
Multiple operations may be specified and they must be performed in the order specified in the
command line argument list
Explanation / Answer
package com;
public class DList<T> {
private DNode<T> header, trailer;
private int size;
public DList() {
size = 0;
header = new DNode<T>(null, null, null);
trailer = new DNode<T>(null, header, null);
header.setNext(trailer);
}
// utility methods
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
// give clients access to nodes, but not to the header or trailer
public DNode<T> getFirst() throws Exception {
if (isEmpty())
throw new Exception("Empty");
return header.getNext();
}
public DNode<T> getLast() throws Exception {
if (isEmpty())
throw new Exception("Empty");
return trailer.getPrev();
}
public DNode<T> getNext(DNode<T> v) throws Exception {
DNode<T> ans = v.getNext();
if (ans == null || ans == trailer)
throw new Exception("No such node");
return ans;
}
public DNode<T> getPrev(DNode<T> v) throws Exception {
DNode<T> ans = v.getPrev();
if (ans == null || ans == header)
throw new Exception("No such node");
return ans;
}
// methods to change the list
public void addBefore(T d, DNode<T> v) {
DNode<T> u = v.getPrev();
DNode<T> x = new DNode<T>(d, u, v);
u.setNext(x);
v.setPrev(x);
size++;
}
public void addAfter(T d, DNode<T> v) {
DNode<T> w = v.getNext();
DNode<T> x = new DNode<T>(d, v, w);
v.setNext(x);
w.setPrev(x);
size++;
}
public void addFirst(T d) {
addAfter(d, header);
}
public void addLast(T d) {
addBefore(d, trailer);
}
public T remove(DNode<T> v) throws Exception {
if (v == header || v == trailer)
throw new Exception("Sentinel");
DNode<T> u = v.getPrev();
DNode<T> w = v.getNext();
w.setPrev(u);
u.setNext(w);
size--;
return v.getData();
}
// LinkedList testing methods:
public String toString() {
String ans = "";
DNode<T> n = header;
ans += "(H)<-->";
do {
n = n.getNext();
if (n == trailer)
ans += "(T)";
else
ans += (n.getData() + "<-->");
} while (n != trailer);
return ans;
}
}