# | Problem | Pass Rate (passed user / total user) |
---|---|---|
13771 | EE2310_Lec_13_1 |
|
13772 | EE2310_Lec_13_2 |
|
Description
Automatic Type Conversions for Fraction Class
In this lecture, we are going to facilitate the expressions below
2/3 + 5.6
4 + 8/17
In which, the first one is the addition of a Fraction
and a double
while the second one is the addition of an int
and a Fraction
. In the first case, we need to convert a Fraction
to a double
and then perform the addition for double
's, while in the second case we convert an int
to a Fraction
and then perform the addition for Fraction
's.
What You Need to Do
- Implement a special member function
operator double()
without a returning type (not evenvoid
) just like constructors. - Implement a constructor
Fraction(int)
that converts the givenint
to aFraction
. - Implement a default constructor
Fraction()
that takes no argument. - Submit everything.
- DO NOT CHANGE MAIN, otherwise your work will not be regarded as correct even if you pass all test cases. You may get PENALTY POINTS for changing main().
int main() {
Fraction num1, num2;
double x;
int y;
char oper1, oper2;
cin >> num1 >> oper1 >> x;
cin.ignore(1024, ',');
cin >> y >> oper2 >> num2;
switch (oper1) {
case '+':
cout << '(' << num1 << ") + (" << x << ") = "<< (double)num1 + x << endl; break;
case '-':
cout << '(' << num1 << ") - (" << x << ") = "<< (double)num1 - x << endl; break;
case '*':
cout << '(' << num1 << ") * (" << x << ") = "<< (double)num1 * x << endl; break;
case '/':
cout << '(' << num1 << ") / (" << x << ") = "<< (double)num1 / x << endl; break;
}
switch (oper2) {
case '+':
cout << '(' << y << ") + (" << num2 << ") = "<< (Fraction)y + num2 << endl; break;
case '-':
cout << '(' << y << ") - (" << num2 << ") = "<< (Fraction)y - num2 << endl; break;
case '*':
cout << '(' << y << ") * (" << num2 << ") = "<< (Fraction)y * num2 << endl; break;
case '/':
cout << '(' << y << ") / (" << num2 << ") = "<< (Fraction)y / num2 << endl; break;
}
return 0;
}
Sample I/O
Input1
8/17 + 3.4 , 5* 6/17
Output1
(8/17) + (3.4) = 3.87059
(5) * (6/17) = 30/17
Input2
9/38 / 7.234,18- 634/29
Output2
(9/38) / (7.234) = 0.0327401
(18) - (634/29) = -112/29
Input
Output
Sample Input Download
Sample Output Download
Tags
Discuss
Description
Friend Classes
A friend class has access to all private members of another class. In this exercise, you are going to practice friend classes by re-implementing the linked list structure.
What You Need to Do
- Declare a class called
Node
that has two private data members:data
andnext
. - Declare another class called
Linked_list
that has two private data members:head
andtail
and four public member functions:output()
,add_node(int)
,reverse()
, as well as a default constructor. - In the class
Node
, declarefriend class Linked_list;
- Implement those four member functions of
Linked_list
, in which theadd_node(int)
function adds a node at thetail
. - You are given the following main function. You should make your class implementations compatible with the given main function. Do not change
main()
. - Submit everything.
int main(){
Linked_list my_list;
int data, input_size;
/* when you add a node, you add it at the tail */
cin >> input_size;
for(int i=0; i < input_size; ++i) {
cin >> data;
my_list.add_node(data);
}
my_list.output();
my_list.reverse();
my_list.output();
return 0;
}
DO NOT CHANGE MAIN, otherwise your work will not be regarded as correct even if you pass all test cases. You may get PENALTY POINTS for changing main().
Sample I/O
Input1
5
1 2 3 4 5
Output1
1 2 3 4 5
5 4 3 2 1
Input2
9
3 98 34 9 1 4 8 8 54
Output2
3 98 34 9 1 4 8 8 54
54 8 8 4 1 9 34 98 3