CSE – Solved

$ 20.99
Category:

Description

/ Đại Học Chính Qui (Bacherlor program (Full-time study))
/ Khoa Khoa học và Kỹ thuật Máy tính (Faculty of Computer Science and Engineering ) / Khoa Học Máy Tính
/ Nguyên lý ngôn ngữ lập trình (CO3005)_Nguyễn Hứa Phùng (DH_HK211) / 4-OOP / OOP Programming
Đã bắt đầu vào
Tình trạng Đã hoàn thành
Hoàn thành vào
Thời gian thực
hiện 6 ngày 9 giờ
Điểm 3,00/3,00
Điểm 10,00 của 10,00 (100%)
1
To express an arithmetic expression, there are 5 following classes:
Exp: general arithmetic expression
BinExp: an arithmetic expression that contains one binary operators (+,-,*,/) and two operands
UnExp: an arithmetic expression that contains one unary operator (+,-) and one operand
IntLit: an arithmetic expression that contains one integer number
FloatLit: an arithmetic expression that contains one floating point number
Define these classes in Python (their parents, attributes, methods) such that their objects can response to eval() message by returning the value of the expression. For example, let object x express the arithmetic expression 3 + 4 * 2.0, x.eval() must return 11.0 Answer: (penalty regime: 0 %)
1
2 ▼
3 ▼
4
5
6
7 ▼
8 ▼
9
10
11
12 ▼
13 ▼ 14
15 ▼
16
17
18
19 ▼
20 ▼
21
22
23 class Exp: def eval(): pass
class UnExp: def __init__(self, operator, arg):
self.operator = operator self.arg = arg
def eval(self): if self.operator == ‘+’: return self.arg.value if self.operator == ‘-‘: return -self.arg.value
class BinExp(): def __init__(self, left, operator, right):
self.operator = operator self.left = left self.right = right

Test Expected Got
 print(x1.eval()) 1 1 
 print(x2.eval()) 2.0 2.0 
 print(x3.eval()) 2 2 
 print(x4.eval()) -1 -1 
 print(x5.eval()) 7.0 7.0 
Passed all tests! 
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
2
Extend the contents of classes Exp, BinExp, UnExp, IntLit, FloatLit such that they can response to printPrefix() message to return the string corresponding to the expression in prefix format. Note that, unary operator +/- is printed as +./-. in prefix format and there is a space after each operator or operand. For example, when receiving message printPrefix(), the object expressing the expression -4 + 3 * 2 will return the string “+ -. 4 * 3 2 ”
Answer: (penalty regime: 0 %)
1
2 ▼
3 ▼
4
5
6
7 ▼
8 ▼
9
10
11
12 ▼
13 ▼ 14
15 ▼
16
17
18 ▼
19
20
21
22 ▼23 ▼ class Exp: def eval(): pass
class UnExp: def __init__(self, operator, arg):
self.operator = operator self.arg = arg
def eval(self): if self.operator == ‘+’: return self.arg.value if self.operator == ‘-‘: return -self.arg.value
def printPrefix(self): return self.operator + ‘. ‘+self.arg.printPrefix()
class BinExp(): def __init__(self, left, operator, right):

Test Expected Got
 print(x1.printPrefix()) 1 1 
 print(x2.printPrefix()) 2.0 2.0 
 print(x3.printPrefix()) + 1 1 + 1 1 
 print(x4.printPrefix()) -. 1 -. 1 
 print(x5.printPrefix()) + -. 1 * 4 2.0 + -. 1 * 4 2.0 
Passed all tests! 
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
3
As in the previous question, when a task is added into expression classes, new methods are added into these classes. Please change the way these classes are implemented in such a way that these classes do not change their contents when new tasks are added into these classes: – Define class Eval to calculate the value of an expression
– Define class PrintPrefix to return the string corresponding to the expression in prefix format
– Define class PrintPostfix to return the string corresponding to the expression in postfix format
Let x be an object expressing an expression, x.accept(Eval()) will return the value of the expression x, x.accept(PrintPrefix()) will return the expression in prefix format and x.accept(PrintPostfix()) will return the expression in postfix format. Be careful that you are not allowed to use type(), isinstance() when implementing this exercise Tip: Use Visitor pattern.
Answer: (penalty regime: 0 %)
1
2 ▼
3 ▼
4
5
6
7 ▼
8 ▼
9
10
11
12 ▼
13 ▼ 14
15 ▼
16
17
18 ▼
19
20
21 ▼
22
23 class Exp: def accept(self, visitor): return visitor.visit(self)
class UnExp(Exp): def __init__(self, operator, arg):
self.operator = operator self.arg = arg
def eval(self): if self.operator == ‘+’: return self.arg.value if self.operator == ‘-‘: return -self.arg.value
def printPrefix(self):
return self.operator + ‘. ‘+self.arg.printPrefix()
def printPostfix(self):
return self.arg.printPrefix()+’ ‘+self.operator + ‘.’

Test Expected Got
 print(x1.accept(Eval())) print(x1.accept(PrintPrefix())) print(x1.accept(PrintPostfix())) 1
1
1 1
1
1 
 print(x2.accept(Eval())) print(x2.accept(PrintPrefix())) print(x2.accept(PrintPostfix())) 2.0
2.0
2.0 2.0
2.0
2.0 
 print(x3.accept(Eval())) print(x3.accept(PrintPrefix())) print(x3.accept(PrintPostfix())) 2
+ 1 1
1 1 + 2
+ 1 1
1 1 + 
 print(x4.accept(Eval())) print(x4.accept(PrintPrefix())) print(x4.accept(PrintPostfix())) -1
-. 1
1 -. -1
-. 1
1 -. 
 print(x5.accept(Eval())) print(x5.accept(PrintPrefix())) print(x5.accept(PrintPostfix())) 7.0
+ -. 1 * 4 2.0
1 -. 4 2.0 * + 7.0
+ -. 1 * 4 2.0
1 -. 4 2.0 * + 
Passed all tests! 
Chính xác

Điểm cho bài nộp này: 1,00/1,00.
◄ OOP Quiz
Chuyển tới…
Địa chỉ: Nhà A1- 268 Lý Thường Kiệt, Phường 14, Quận 10, Tp.HCM.
Email: elearning@hcmut.edu.vn
Phát triển dựa trên hệ thống Moodle

Reviews

There are no reviews yet.

Be the first to review “CSE – Solved”

Your email address will not be published. Required fields are marked *