# | Problem | Pass Rate (passed user / total user) |
---|---|---|
11459 | Cheat Sheet |
|
13521 | Let's Play with More Polymorphic Codecs! |
|
13522 | Infinitely Big Integer: Darray Implementation |
|
13523 | Infinitely Big Integer: Big Integer Implementation |
|
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;
Operator overloading
Binary operators
/*1.non-static member function */
complex operator+ (const complex&);
/* 2. non-member function */
friend complex operator* (const
complex&, const complex&);
Unary 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
Description
This is a partial judge problem.
You're asked to implement several Codecs that inherit the Codec.
class Codec {
public:
virtual void encode() = 0;
virtual ~Codec() { } // Do nothing
virtual void show() = 0;
};
There are three types of Codecs.
The first one is Number Codec, which maps the letters to numbers, where the letters a ~ z correspond to 1 ~ 26, respectively.
For example:
the text: helloworld
the text after encoding: 85121215231518124
The second one is Two digits Number Codec, which maps the letters to numbers, where the letters a ~ z correspond to 1 ~ 26, respectively, and if the number is smaller than 10, add a 0 in front.
For example:
the text: helloworld
the text after encoding: 08051212152315181204
The third one is The Rail-Fence Cipher Codec, which divides the text into two lines, and then combines them into a sentence in the order up and down. If the length of the text is odd, the upper length is (n+1)/2 and the lower length is (n-1)/2.
For example:
the text: HelloWorld!
divide the text into two lines:
H e l l o W
o r l d !
the text after encoding: Hoerllldo!W
In this problem, you only need to implement three functions in your 'function.cpp':
#include<iostream>
#include<string>
#include "function.h"
using namespace std;
void Number_Codec::encode()
{
// your code here
}
void Two_Number_Codec::encode()
{
// your code here
}
void The_Rail_Fence_Cipher_Codec::encode()
{
// your code here
}
Input
The input contains only one line, the text and the type of the Codec.
Test cases:
(3/10) 0 < | text | <= 100, Codec = Number
(3/10) 0 < | text | <= 100, Codec = Two_Number
(4/10) 0 < | text | <= 100, Codec = The_Rail_Fence_Cipher
Output
Output the text after encoding.
Sample Input Download
Sample Output Download
Partial Judge Code
13521.cppPartial Judge Header
13521.hTags
Discuss
Description
The problem "Infinitely Big Integer" is splitted into two subproblems:
- Darray Implementation (3/10)
- Big Integer Implementation (7/10)
You should solve this subproblem first.
Based on the definition of Dynamic Array in Problem 13520 (nthu.edu.tw), you need to implement a dynamic array with the following functions:
- int& operator[](int): access data like using array. Users and main.cpp should/will not access the index which is greater or equal to size.
- void pushback(int x): append the element x
- void popback(void): pop the last element. Don't do anything if the array is empty.
- void clear(void): clear the array (set size to 0) so that the next pushbacks will place elements in data[0],data[1] and so on.
- int length(void): return the current size.
- void resize(void): double the capacity and copy the data.
- ~Darray(): destructor
Note that main.cpp acts like a functional tester for your Darray. There's no need to understand it. You should test your Darray by yourself. Furthermore, a new function void popback(void)
is introduced in this problem.
class Darray {
public:
Darray() {
capacity = 100;
size = 0;
data = new int[capacity];
};
~Darray();
int& operator[](int);
void pushback(int x);
void popback(void);
void clear(void);
int length(void);
private:
void resize(void); // double the capacity
int *data;
int capacity;
int size;
};
Darray arr;
for (int i = 0; i < 5; i++) arr.pushback(i*i);
arr[2] += 100 + arr[3];
for (int i = 0; i < arr.length(); i++)
cout << arr[2] << ' '; // Print: 0 1 113 9 16
cout << endl << arr.length() << endl; // Print: 5
arr.clear();
cout << arr.length() << endl; // Print: 0
arr.pushback(9487); arr.pushback(9487);
arr.popback();
cout << arr.length() << ' ' << arr[0] << endl; // Print: 1 9487
std::vector
is prohibited (both subproblems 1 and 2) and will result in 0 score if you use it.
Input
This is handled by the main function.
1/3 of the test cases will not test popback()
. If you have problems with it, try writing an empty popback function so that it can be compiled successfully.
void popback() {
}
Output
This is handled by the main function.
Sample Input Download
Sample Output Download
Partial Judge Code
13522.cppPartial Judge Header
13522.hTags
Discuss
Description
The problem "Infinitely Big Integer" is splitted into two subproblems:
- Darray Implementation (3/10)
- Big Integer Implementation (7/10)
You should solve the former, Darray Implementation, first.
You may have practiced the problem Big Integer before. Instead of using traditional int, we store the integer in a char array. However, it still has size limitation because we declare a static array for the class. In this problem, we'll use the Darray in problem 13522 - Infinitely Big Integer: Darray Implementation as the data array in class INT to handle arbitary large numbers as long as the memory of the computer is enough.
The following is the structure of INT.
public:
void operator+=(INT&);
friend std::istream &operator>>(std::istream &, INT &);
friend std::ostream &operator<<(std::ostream &, INT &);
private:
Darray data;
};
For simplicity, you only need to implement three functions, <<, >> and +=. The streaming operators are used for cin and cout. The operator += does the same as the traditional operator +=. For instance, A += B, A will be the value of A+B and B remains the same. Note that all the parameters are passed by reference, which is different from the previous problem.
Your function.cpp should contain the implementation of both Darray and INT. If you passed the subproblem 1 first, just copy and paste the Darray. For example:
#include "function.h"
// Darray
void Darray::pushback(int x) { ... }
...
// INT
void INT::operator+= (INT &b) { ... }
...
It is okay if you have problems with the popback
function in subproblem 1 as long as the Darray has basic functionality to pass the test cases, because Darray in INT is fully controlled by you.
The following is an alternative main.cpp to test your code:
#include "function.h"
using namespace std;
int main() {
INT a, b;
cin >> a;
cin >> b;
a += b;
cout << "a + b = " << a << endl;
return 0;
}