$25
Exercise 1.
(40 points) You are writing a very simple calculator to be able to calculate six operators. The following describes the features of the simple calculator.
(1) The calculator supports six operations: addition (+), subtraction (-), multiplication (*), division (/), remainder (%), and exponentiation (∧). The remainder (also known as a modulo operation) is the integer “left over” after dividing one integer by another. For instance, 13%3 would be 1, because the quotient of the equation is 4, and its remainder becomes 1 due to 13−(3∗4). Exponentiation is a mathematical operation, producing n times of multiplication of b, bn, where a base (b) and a power (n) are given, For example, three to the power of four would become 34 = 3∗3∗3∗3 = 81.
(2) The calculator supports an assignment (=) operation. Then, the calculator allows one to allocate up to three variables, x, y, and z for further use. For instance, it is possible to enter x = 10, followed by x +20. Each variable should be initialized as 0 in the beginning. In case of wrong variables given like a or xy, your code emits Invalid input.
(3) The calculator takes a single equation as an input at a time for evaluation. For example, if one wants to multiply two numbers, the input equation must be 3∗4. Likewise, 3%8 and 7∧4 are examples of possible inputs. The input may or may not have a space between a number and an operator.
(4) The calculator keeps evaluating or computing with a single equation at a time. If a user enters quit, then it terminates a program.
Here are considerations for your implementation.
• The type of two input numbers are positive numbers with the type of either int or float for addition, subtraction, multiplication and division whereas the input numbers are treated as int only for remainder.
For example, 9.5%4 would be regarded as 9%4.
• You may assume that the results of all operations can be expressed with int and float.
• Division by zero must be avoided, that is, your code should raise an error with a message, Operation disallowed.
• In case of invalid inputs or operations, your program emits an error message. The program keeps working until quit.
• You must build a class, no point will be given otherwise.
• There is no skeleton code. However, you must follow the class prototype in Listing 1.
• You may assume that a benign equation or assignment will be given as test cases.
• Listing 1 shows various output examples.
// Class Prototype in q1.cc
Class SimpleCalc { public:
// Declare your member functions private:
// Declare your member variables
}
// Output example (1) Addition, subtraction, multiplication and quit
Enter your equation to calculate: 5 + 9 // Enter (5+9 without a space
is
allowed)
Answer: 14 // Moving on
Enter your equation to calculate: 7.5 - 20 // Enter Answer: -12.5 // Moving on
Enter your equation to calculate: 82 * 14 // Enter Answer: 1148 // Moving on
Enter your equation to calculate: quit // Terminating
// Output example (2) Division and terminating with a wrong
Enter your equation to calculate: 144.0 / 12 // Enter Answer: 12.0 // Moving on
Enter your equation to calculate: 8 / 0 // Enter Answer: Operation disallowed // Terminating
// Output example (3) Remainder, exponentiation and quit
Enter your equation to calculate: 30.9 % 7 // Enter (30.9 Answer: 2 // Moving on
Enter your equation to calculate: 5 ^ 3 // Enter Answer: 125 // Moving on
Enter your equation to calculate: 2.5 ^ 3 // Enter Answer: 15.625 // Moving on
Enter your equation to calculate: quit // Terminating
// Output example (4) Assignment and quit
Enter your equation to calculate: x = 9.3 // Enter
Enter your equation to calculate: x // Enter
operation
interpreted
as
30)
Answer: 9.3
Enter your equation to calculate: quit
// Terminating
// Output example (5) More assignments
and
quit
Enter your equation to calculate: x = 3
//
Enter
Enter your equation to calculate: x + 4
//
Enter
Answer: 7 // Moving on
Enter your equation to calculate: x = 6
//
Enter
Enter your equation to calculate: y = 7
//
Enter
Enter your equation to calculate: x * y
//
Enter
Answer: 42 // Moving on
Enter your equation to calculate: z // Enter Answer: 0 // Moving on (initialized as 0)
Enter your equation to calculate: x * z // Enter Answer: 0 // Moving on
Enter your equation to calculate: quit // Terminating
// Output example (6) Wrong variable
Enter your equation to calculate: a // Enter (x,y,z only) Answer: Invalid input // Terminating
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Listing 1: SimpleCalc class prototype and output examples for Exercise 1.
Exercise 2.
(60 points) Now you are developing an advanced class, named Fraction that represents a fraction value. The Fraction contains three private member variables: K (integer), D (denominator), and N (numerator), that is, where F represents a zero or positive fraction (F >= 0). For example, means 1.5.
The class has three member functions to be able to compute addition and multiplication of two inputs with a form of Fraction. A reduction is a helper member function to reduce a fraction; for instance, the reduction form of would be after dividing by G.C.D (Great Common Divisor). Note that print() outputs a fraction with the form of “K and N/D” where N < D. K may hold a zero when .
A few things for your implementation:
(1) A denominator will not have a zero.
(2) You may add more member variables or functions if needed.
// Class Prototype in q2.cc
Class Fraction { public:
// constructor
Fraction(int k, int n, int d = 1): K(k), N(n), D(d) {
}
// member functions
Fraction add(Fraction f);
Fraction mul(Fraction f);
// helper member functions void print(); // Printing the value of a fraction ("K and N/D") void reduction(); // Generating a reduction form of the Fraction
private:
// member variables int K = 0; // integer int D = 1; // denominator
int N = 0; // numerator
}
// The following contains a handful of examples for Fraction computations. int main() {
Fraction f1(1, 2, 3); // 1(2/3) = 5/3 = 1.666...
Fraction f2(0, 2, 6); // 0(2/6) = 1/3 = 0.333...
Fraction f3(2, 3); // 2(3/1) = 5
Fraction f4(4, 1, 5); // 4(1/5) = 4.2
// Output example (1) Addition of two fractions Fraction res1 = f1.add(f2); // 1(2/3)+0(2/6) = 2 res1.print(); // 2 and 0/1 or 0 and 2/1
// Output example (2) Multiplication of two fractions Fraction res2 = f2.mul(f3); // 0(2/6)*2(3/1) = 5/3 = 1(2/3) res2.print(); // 1 and 2/3
// Output example (3) Reduction f2.reduction(); // 0(2/6) = 0(1/3) f2.print(); // 0 and 1/3
// Output example (4) Note that the result should be always a reduction form
Fraction res3 = f4.mul(f3.add(f2)) // 4(1/5) * {2(3/1) + 0(2/6)} = 21/5 * (5 +
1/3) = 21/5 * 16/3 = 112/5 res3.reduction().print(); // 22 and 2/5
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Listing 2: Fraction class prototype for Exercise 2.