1214 - CS I2P (II) 2017 Chen Mid2 Scoreboard

Time

2017/05/23 13:10:00 2017/05/23 15:30:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
10997 Queue
11432 Binary search trees using polymorphism (Bonus)
11446 Rational (I2P Chen)
11454 Polynomial Multiplication
11459 Cheat Sheet

10997 - Queue   

Description

A queue is an abstract data type that serves as a collection of elements, where nodes are removed only from the head of the queue and are inserted only at the tail of the queue. Two principal operations can be used to manipulate a queue: enqueue, which inserts an element at the tail, and dequeue, which removes the element at the head of the collection.

Let’s see how the queue data structure can be realized in C++.We have an approach to implement queue: linked list. Thus, we define a class as follows:

 class List_queue {

    public:

        List_queue();

        ~List_queue();

        void enqueue(const int &);

        void dequeue();

        void print();

    private:

        ListNode *head;

        ListNode *tail;

};

where List_queue implements the queue data structure.

REQUIREMENTS:

Implement the constructor, destructor, enqueue(), dequeue() and print() member functions of the List_queue class.

Note:

1.This problem involves three files.

  • function.h: Class definitions.
  • function.cpp: Member-function definitions.
  • main.cpp: A driver program to test your class implementation.

You will be provided with main.cpp and function.h, and asked to implement function.cpp.

function.h

main.cpp

2.For OJ submission:

       Step 1. Submit only your function.cpp into the submission block.

       Step 2. Check the results and debug your program if necessary.

Input

There are three kinds of commands:

  • “enqueue integerA” represents inserting an element with int value A at the tail of the queue.
  • “dequeue” represents removing the element at the head of the queue.
  • “print” represents showing the current content of the queue.

Each command is followed by a new line character.

Input terminated by EOF.

Output

The output should consist of the current state of the queue.

When the queue is empty, you don’t need to print anything except a new line character.

Sample Input  Download

Sample Output  Download

Partial Judge Code

10997.cpp

Partial Judge Header

10997.h

Tags

10402HW6



Discuss




11432 - Binary search trees using polymorphism (Bonus)   

Description

A binary search tree (BST) is a binary tree, whose internal nodes each store a key and each have two sub-trees, commonly denoted left and right. The tree additionally satisfies the property: the key in each node must be greater than all keys stored in the left sub-tree, and smaller than all keys in the right sub-tree.

 

Based on the above property of BSTs, when a node is to be inserted into an existing BST, the location for the node can be uniquely determined.

 

For example, if a node with key 6 needs to be inserted into the following BST

the BST will become


Implementation of the BST Data Structure

There are two approaches to BST implementation: array and linked list.

1. Array:

An approach to storing a BST is to use a single, contiguous block of memory cells, i.e., an array, for the entire tree. We store the tree’s root node in the first cell of the array. (Note that, for ease of implementation, we ignore the 0th cell and start from the 1st cell.) Then we store the left child of the root in the second cell, store the right child of the root in the third cell, and in general, continue to store the left and right children of the node found in cell n in the cells 2n and 2n+1, respectively. Using this technique, the tree below

would be stored as follows

 

2. Linked list:

We set a special memory location, call a root pointer, where we store the address of the root node. Then each node in the tree must be set to point to the left or right child of the pertinent node or assigned the NULL value if there are no more nodes in that direction of the tree.


REQUIREMENTS:

Implement the destructor, insert(), search(), and height() member functions of both the Array_ BST and List_ BST classes.

Input

There are four kinds of commands:

  • “I A”: insert a node with int value A(A>0) into the BST
  • “S A”: if the integer A exists in the BST, print “yes”; otherwise, print “no”.
  • “P”: show the current content of the BST in Level-order.
  • “H”: print the BST’s height.

Each command is followed by a new line character.

Input terminated by EOF.

Output

The output shows the result (in both Array and LinkedList method) of each command.

When the BST is empty, you don’t need to print anything except a new line character.

But there's already a new line character in the main function, so you don't have to do anything.

Sample Input  Download

Sample Output  Download

Partial Judge Code

11432.cpp

Partial Judge Header

11432.h

Tags




Discuss




11446 - Rational (I2P Chen)   

Description

Develop a class called Rational for performing arithmetic with fraction.

  • Use integer variables to represent the private data of the class ─ the numerator and the denominator.
     
  • Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction 2/4 would be stored in object as 1 in the numerator and 2 in the denominator.
     
  • Develop a complete class containing proper constructor functions. The class should also provide the following overloaded operator capabilities:

1.Overload the addition operator (+) to add two Rational numbers.

   The result should be stored in reduced form.

2.Overload the subtraction operator (-) to subtract two Rational numbers. 

   The result should be stored in reduced form.

3.Overload the stream insertion operator (<<).

   Printing Rational numbers in form a/b, where a is the numerator and b is the denominator.) 

 

  • You need to implement a private member function reduce() that will be used in your constructor and the about public arithmetic member functions to derive the required reduced form. ***In reduce(), pay attention to the case that one of the numerator and denominator of a rational number is negative. In this case, the negative sign “-” should be placed at the numerator.

class foo {
public:
    int a;
    int b;
    foo(int = 0, int = 1);
};

foo::foo(int a, int b) {
    foo::a = a;
    foo::b = b;
}

Input

There are five strings in each line: S1 S2 S3 S4 S5

  • ŸS1 represents an operator (+,-).
  • S2 and S3 represent the numerator and denominator of the first operand, respectively.
  • S4 and S5 represent the numerator and denominator of the second operand, respectively.

Input terminated by EOF.

Output

For every given operation, your program should print the corresponding result followed by a new line character.

Sample Input  Download

Sample Output  Download

Partial Judge Code

11446.cpp

Partial Judge Header

11446.h

Tags




Discuss




11454 - Polynomial Multiplication   

Description

Create a class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent, e.g., the term 2x4 has the coefficient 2 and the exponent 4.

 

Provide public member functions that perform Multiplication tasks.

Input

There are four lines.

The first two lines represent the greatest power and the corresponding coefficients of the first polynomial.

The last two lines represent the greatest power and the corresponding coefficients of the second polynomial.

The greatest power is in the range of 0-25.

Note that the coefficients are in descending order and each element is separated by a space.

Output

Output the coefficients of the product of these two polynomials in descending order.

If the result of coefficient is 0, just print it.

Sample Input  Download

Sample Output  Download

Partial Judge Code

11454.cpp

Partial Judge Header

11454.h

Tags




Discuss




11459 - Cheat Sheet   

Description

Array.cpp
// overloaded assignment operator;
// const return avoids: ( a1 = a2 ) = a3
const Array &Array::operator=( const Array &right )
{
   if ( &right != this ) // avoid self-assignment
   {
      // for Arrays of different sizes, deallocate original
      // left-side Array, then allocate new left-side Array
      if ( size != right.size )
      {
         delete [] ptr; // release space
         size = right.size; // resize this object
         ptr = new int[ size ]; // create space for Array copy
      } // end inner if

      for ( size_t i = 0; i < size; ++i )
         ptr[ i ] = right.ptr[ i ]; // copy array into object
   } // end outer if

   return *this; // enables x = y = z, for example
} // end function operator=

// determine if two Arrays are equal and
// return true, otherwise return false
bool Array::operator==( const Array &right ) const
{
   if ( size != right.size )
      return false; // arrays of different number of elements

   for ( size_t i = 0; i < size; ++i )
      if ( ptr[ i ] != right.ptr[ i ] )
         return false; // Array contents are not equal

   return true; // Arrays are equal
} // end function operator==

// overloaded subscript operator for non-const Arrays;
// reference return creates a modifiable lvalue
int &Array::operator[]( int subscript )
{
   // check for subscript out-of-range error
   if ( subscript < 0 || subscript >= size )
      throw out_of_range( "Subscript out of range" );

   return ptr[ subscript ]; // reference return
} // end function operator[]

// overloaded subscript operator for const Arrays
// const reference return creates an rvalue
int Array::operator[]( int subscript ) const
{
   // check for subscript out-of-range error
   if ( subscript < 0 || subscript >= size )
      throw out_of_range( "Subscript out of range" );

   return ptr[ subscript ]; // returns copy of this element
} // end function operator[]

// overloaded input operator for class Array;
// inputs values for entire Array
istream &operator>>( istream &input, Array &a )
{
   for ( size_t i = 0; i < a.size; ++i )
      input >> a.ptr[ i ];

   return input; // enables cin >> x >> y;
} // end function

// overloaded output operator for class Array
ostream &operator<<( ostream &output, const Array &a )
{
   // output private ptr-based array
   for ( size_t i = 0; i < a.size; ++i )
   {
      output << setw( 12 ) << a.ptr[ i ];

      if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output
         output << endl;
   } // end for

   if ( a.size % 4 != 0 ) // end last line of output
      output << endl;

   return output; // enables cout << x << y;
} // end function operator<<

Inheritance type

Public

Protected

Private

Public data/ functions in the base class

Public in the derived class

Protected in the derived class

Private in the derived class

Protected data/ functions in the base class

Protected in the derived class

Protected in the derived class

Private in the derived class

Private data/fun in the base class

Not accessible in the derived class

Not accessible in the derived class

Not accessible in the derived clas

 
 
 
 
 
 

 

 

 

 

 

Polymorphism
  In the base class
  virtual void foo();
  In the derived class
  virtual void foo() override;

Abstract class and pure virtual function
  virtual void foo() =0;

Prefix/postfix operators
  complex &operator++();  // prefix
  complex operator++(int); // postfix

Operator overloading
  Binary operators
  /*1.non-static member function */
     complex operator+ (const complex&);
  /* 2. non-member function */
     friend complex operator* (const
                      complex&, const complex&);
  Unitary operators
  /*1.non-static member function */
      complex operator-();
  /*2. non-member function */
      friend complex operator~(const complex&);

Inheritance
  class A {
  // A is a base class }
  class B: public A {
  // B inherits A.
  // B is a derived class}

Dynamic allocation
  int *ptr = new int;
  delete ptr;
  int *ptr = new int[100];
  delete [] ptr;

String object
  #include<string>
  using namespace std;
  ...
  string str1 = "Hello";
  string str2 = "World";

Standard C++ Library - <string>

operator[]

For string a, a[pos]:

Return a reference to the character at position pos in the string a.

operator+=

Append additional characters at the end of current string.

operator+

Concatenate strings.

operator<

Compare string values.

For string a and b, a < b:

if a is in front of b in dictionary order, return 1, else return 0.

operator>

Compare string values.

For string a and b, a > b:

if a is in back of b in dictionary order, return 1, else return 0.

operator==

Compare string values.

For string a and b, a == b:

if equal, return 1, else return 0;

operator!=

Compare string values.

For string a and string b, a != b:

if not equal, return 1, else return 0;

compare()

Compare string.

For string a and b, a.compare(b):

if equal, return 0. If string a is in front of string b in dictionary order, return -1. If string a is in back of string b in dictionary order, return 1.

length()

Return length of string.

swap()

Swap string values.

push_back()

For string a, a.push_back(c):

append character c to the end of the string a, increasing its length by one.

 

Standard C++ Library - <sstream>

operator<<

Retrieves as many characters as possible into the stream.

operator>>

Extracts as many characters as possible from the stream.

str

Returns a string object with a copy of the current contents of the stream.

For example(1):
stringstream ss{“Hello”};    // constructor
string str = ss.str();    // a = “Hello”

For example(2):
stringstream ss;
ss<<”75”;
ss<<”76”;
int num;
ss>>num;    // num = 7576

Input

Output

Sample Input  Download

Sample Output  Download

Tags




Discuss