exam 2 class definitions and code questions Flashcards
(58 cards)
what are the private members of the Darray class?
T *ptr;
int cap;
make the default constructor for the Darray class.
template <typename>
Darray<T>::Darray(){
ptr = 0;
cap = 0;
}</T></typename>
make a constructor for the Darray class that takes an int as a parameter.
template <typename>
Darray<T>::Darray(int capacity){
cap = capacity;
ptr = new T[cap];
}</T></typename>
make the copy constructor for the Darray class.
template <typename>
Darray<T>::Darray(const Darray<T>& actual){
cap = actual.cap;
ptr = new T[cap];
for(int i = 0; i < cap; ++i){
ptr[i] = actual.ptr[i];
}
}</T></T></typename>
make a swap function for the Darray class.
template <typename>
void Darray<T>::swap(Darray<T> & rhs){
T *temp = ptr;
ptr = rhs.ptr;
rhs.ptr = temp;
int tmp = cap;
cap = rhs.cap;
rhs.cap = tmp;
}</T></T></typename>
make a method for the Darray class called resize that takes an int parameter and is a void function.
template <typename>
void Darray<T>::resize(int newCap){
int smaller = cap;
if(newCap < smaller){
smaller = newCap;
}
T *tmp = new T[newCap];
for(int i = 0; i < smaller; ++i){
tmp[i] = ptr[i];
}
delete[] ptr;
cap = newCap;
ptr = tmp;
}</T></typename>
make a destructor for the Darray class.
template <typename>
Darray<T>::~Darray(){
delete[] ptr;
}</T></typename>
overload operator[] for the Darray class so that it takes an int as a parameter and is non const.
template <typename>
T& Darray<T>::operator[](int i){
return ptr[i];
}</T></typename>
overload operator[] for the Darray class so that it takes an int as a parameter and is const.
template <typename>
T Darray<T>::operator[](int i) const{
return ptr[i];
}</T></typename>
make a method for the Darray class called capacity that returns the capacity of ptr.
int Darray<T>::capacity() const{
return cap;
}</T>
what are the private variables for the Stack class?
T s[size];
int tos; //top of stack
make a method for the Stack class called isEmpty that takes no parameters and returns whether the Stack is empty.
bool Stack::isEmpty() const{
return (tos == -1);
}
make an in-line default constructor for the Stack class.
Stack() : tos(-1){}:
make a method for the Stack class called isFull that takes no parameters and returns whether the Stack is full.
template <typename>
bool Stack<T>::isFull() const{
return (tos >= size);
}</T></typename>
make a method for the Stack class called push that pushes an item onto the Stack.
template <typename>
void Stack<T>::push(const T& item){
assert(!isFull());
s[++tos] = item //increment then return
}</T></typename>
make a method for the Stack class called pop that returns the newest item in the Stack and ‘pops’ it.
template <typename>
T Stack<T>::pop(){
assert(!isEmpty());
return s[tos--]; //return then decrement
}</T></typename>
make a method for the Stack class called pop that returns the newest item in the Stack.
template <typename>
T Stack<T>::top(){
assert(!isEmpty());
return s[tos];
}</T></typename>
write a method for the String class called infix_to_postfix that uses a Stack variable and returns the postfix version of the equation entered.
String infix_to_postfix(const char expr[]){
Stack<String> s;
char token;
String lhs, rhs, op;
int i = 0;
while(expr[i] != 0){
token = expr[i];
\++i;
if(token == ')'){
rhs = s.pop();
op = s.pop();
lhs = s.pop();
s.push(lhs + rhs + op);
}
else if(token !+ '('){
String temp = " ";
temp[0] = token;
s.push(temp);
}
}
return s.pop();
}</String>
write a method for the String class called infix_to_prefix that uses a Stack variable and returns the prefix version of the equation entered.
String infix_to_prefix(const char expr[]){
Stack<String> s;
char token;
String lhs, rhs, op;
int i = 0;
while(expr[i] != 0){
token = expr[i];
\++i;
if(token == ')'){
rhs = s.pop();
op = s.pop();
lhs = s.pop();
s.push(op + lhs + rhs);
}
else if(token !+ '('){
String temp = " ";
temp[0] = token;
s.push(temp);
}
}
return s.pop();
}</String>
write a helper method for the EvalPostFix method called isoperand that returns whether a character is an operand.
bool isoperand(char ch){
return (ch == ‘+’ || ch == ‘-‘ || ch == ‘/’ ||
ch == ‘*’);
}
write a helper method for the EvalPostFix method called EVAL that evaluates an expression.
int EVAL(int lhs, char op, int rhs){
switch(op){
case ‘+’: return lhs + rhs;
case ‘-‘: return lhs - rhs;
case ‘/’: return lhs / rhs;
case ‘*’: return lhs * rhs;
}
return 0;
}
make a method for the String class called EvalPostFix that evaluates the postfix expression.
int EvalPostFix(const char& expr[]){
Stack<int> s;
int i = 0;
while(expr[i] != 0){
if(!isoperand()){
int lhs, rhs;
rhs = s.pop();
lhs = s.pop();
s.push(EVAL(lhs, expr[i], rhs);
}
else{
s.push((int(ch) - int('0'));
}
\++i;
}
return s.pop();
}</int>
write a recursive method for the Fibonacci sequence.
int fib(int n){
if(n <= 2){
return 1;
}
else{
return fib(n - 1) + fib(n - 2);
}
}
write a recursive method to find a factorial.
int nfact(int N){
if(n <= 1){
return 1;
}
else{
return nfact(N - 1);
}
}