# | Problem | Pass Rate (passed user / total user) |
---|---|---|
13873 | Dynamic Array 2 |
|
13896 | Darray stack for postfix calculation (4/10) |
|
13897 | Darray stack for postfix calculation (6/10) |
|
Description
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.
- int back(void): return a copy of the last element. Return -1 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, two new functions void popback(void) and int back(void) are introduced in this problem.
// function.h
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);
int back(void);
void print(void){
if(this->size == 0){
cout << endl;
}
else{
for(int i=0; i<this->size; i++){
cout << this->data[i] << " ";
}
cout << endl;
}
};
private:
void resize(void); // double the capacity
int *data;
int capacity;
int size;
};
Input
There are six kinds of commands:
- “pushback integerA” represents adding an element with int value A at the end of the dynamic array.
- “popback “ represents removing the element at the end of the dynamic array.
- “clear” represents clearing the dynamic array.
- “length” represents showing how many elements are in the dynamic array currently.
- “back” represents showing the element at the end of the dynamic array.
- “print” represents showing the current content of the dynamic array.
Each command is followed by a new line character.
Output
The output should follow the instructions.
("pushback", "popback" and "clear" will not print anything)
When the dynamic array is empty, you don’t need to print anything except a new line character.
Sample Input Download
Sample Output Download
Partial Judge Code
13873.cppPartial Judge Header
13873.hTags
Discuss
Description
The problem “Darray stack” is splitted into 2 subproblems:
- Darray & stack implementation (4/10)
- Postfix expression calculation using stack (6/10)
You should solve this subproblem first.
In this subproblem, you have to implement a stack class, Darray_stack, which uses the Darray class you have implemented in "13873 - Dynamic Array 2" as its internal representation.
Here is the structure of Darray_stack:
class Darray_stack{
public:
Darray_stack();
void operator << (const int&);
void operator >> (int&);
int size(){
return arr.length();
}
private:
Darray arr;
};
You have to implement 3 public member functions for Darray_stack:
- Darray_stack(): the constructor
- void operator << (const int&): overloads << to push an element onto the stack
- void operator >> (int&): overloads >> to pop an element from the stack and store it at the right operand
To compile your code:
- Your function.cpp should include function.h and contain the implementation of both Darray (copied from your code in "13873 - Dynamic Array 2") and Darray_stack. Our main.cpp would run the function ‘verify_code’ to test whether your implementation is correct.
- Please use C++17.
Input
The first line is a string which only contains digits, representing the input of the verify function.
The second line is a postfix expression.
Constraints:
All numbers in the expression are nonnegative and are less than or equal to 100.
The length of the expression is no more than 250.
The length of the verify input code is no more than 10000.
The operators would only be '+', '-' or '*'.
It is guaranteed the numbers won't exceed 109 during the whole calculation process.
Output
Output the verify code.
Sample Input Download
Sample Output Download
Partial Judge Code
13896.cppPartial Judge Header
13896.hTags
Discuss
Description
The problem “Darray stack” is splitted into 2 subproblems:
- Darray & stack implementation (4/10)
- Postfix expression calculation using stack (6/10)
You should solve the former subproblem first.
In this subproblem, you are asked to implement a class, Postfix_calculator, which calculates a postfix expression using the Darray_stack class you have implemented in "13896".
Here is the structure of Postfix_calculator:
public:
Postfix_calculator(){}
Postfix_calculator(string s): postfix(s){}
void calculate();
int answer(){
int ans;
st >> ans;
return ans;
}
private:
string postfix;
Darray_stack st;
};
Postfix_calculator contains 2 data members:
- string postfix: the input postfix expression
- Darray_stack st: the stack you are going to use during the whole calculation process.
You only have to implement the member function void calculate(), which calculates the postfix expression using Darray_stack st and stores the final result on the top of Darray_stack st.
To compile your code:
- Your function.cpp should include function.h and contain the implementation of all Darray (copied from your code in "13873 - Dynamic Array 2"), Darray_stack (copied from your code in "13896"), and Postfix_calculator. Our main.cpp would run the function ‘verify_code’ to test whether your implementation is correct.
- Please use C++17.
Input
The first line is a string which only contains digits, representing the input of the verify function.
The second line is a postfix expression. The numbers and the operators are separated by spaces.
Constraints:
All numbers in the expression are nonnegative and are less than or equal to 100.
The length of the expression is no more than 250.
The length of the verify input code is no more than 10000.
The operators would only be '+', '-' or '*'.
It is guaranteed the numbers won't exceed 109 during the whole calculation process.
Output
Output contains 2 lines.
The first line contains the verify code.
The second line contains an integer, representing the result of the postfix expression.