forked from matkatmusic/PFMCPP_Project4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
133 lines (107 loc) · 5.45 KB
/
main.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Project 4 - Part 1 / 9
video: Chapter 2 Part 7
Returning Values tasks
Create a branch named Part1
Purpose: This project will take you through the process of writing a class that wraps a numeric type, beginning with writing simple member functions and ending with a fully templated class with lots of functionality.
1) write 3 UDTs named FloatType, DoubleType, IntType.
2) give each UDT the following member functions:
add( lhs, rhs );
subtract( lhs, rhs );
multiply( lhs, rhs );
divide( lhs, rhs );
lhs is the left-hand-side of the operation, rhs is the right-hand-side
e.g. x = 2 + 3
'2' is the lhs
'3' is the rhs
'+' is the operation
3) implement the appropriate action in the member function.
a) Be aware that floating point division by 0 is legal, but integer division by 0 will crash your program.
b) Handle this possible input when you write your divide() functions.
c) you should warn the user if they're doing floating-point-division-by-zero but not prevent the division from happening
d) you should warn AND prevent the division from happening if it is an integer-division-by-zero.
4) make them return the correct primitive type. e.g. if you're implementing the FloatType::add function, your implementation would start like this:
float FloatType::add( float lhs, float rhs )
5) Do not edit main(). your job is to make your UDTs work correctly with the existing main.
the expected program output is listed after main along with instructions on how to verify it.
6) After you finish defining each type/function, click the [run] button. Clear up any errors or warnings as best you can.
*/
/*
your program should generate the following output EXACTLY.
This includes the warnings.
Use a service like https://www.diffchecker.com/diff to compare your output.
you should have no errors or warnings.
clang++ -std=c++17 -Weverything -Wno-c++98-compat -Wno-missing-prototypes main.cpp && ./a.out
result of ft.add(): 555.556
result of ft.subtract(): -308.644
result of ft.multiply(): 53345.3
result of ft.divide(): 0.285712
result of ft.add(): 4444.56
result of ft.subtract(): 4444.56
result of ft.multiply(): 0
result of ft.divide():
warning, floating point division by zero returns 'inf' !
inf
result of db.add(): 555.556
result of db.subtract(): -308.644
result of db.multiply(): 53345.3
result of db.divide(): 0.285712
result of db.add(): 123.456
result of db.subtract(): 123.456
result of db.multiply(): 0
result of db.divide():
warning, floating point division by zero returns 'inf' !
inf
result of i.add(): 30
result of i.subtract(): -10
result of i.multiply(): 200
result of i.divide(): 0
result of i.add(): 10
result of i.subtract(): 10
result of i.multiply(): 0
result of i.divide(): error, integer division by zero will crash the program!
returning lhs
10
good to go!
*/
/*
MAKE SURE YOU ARE NOT ON THE MASTER BRANCH
Commit your changes by clicking on the Source Control panel on the left, entering a message, and click [Commit and push].
If you didn't already:
Make a pull request after you make your first commit
pin the pull request link and this repl.it link to our DM thread in a single message.
send me a DM to review your pull request when the project is ready for review.
Wait for my code review.
*/
#include <iostream>
int main()
{
FloatType ft;
std::cout << "result of ft.add(): " << ft.add( 123.456f, 432.1f) << std::endl;
std::cout << "result of ft.subtract(): " << ft.subtract( 123.456f, 432.1f) << std::endl;
std::cout << "result of ft.multiply(): " << ft.multiply( 123.456f, 432.1f) << std::endl;
std::cout << "result of ft.divide(): " << ft.divide( 123.456f, 432.1f) << std::endl;
std::cout << "result of ft.add(): " << ft.add( 4444.56f, 0.0f) << std::endl;
std::cout << "result of ft.subtract(): " << ft.subtract( 4444.56f, 0.0f) << std::endl;
std::cout << "result of ft.multiply(): " << ft.multiply( 4444.56f, 0.0f) << std::endl;
std::cout << "result of ft.divide(): " << ft.divide( 4444.56f, 0.0f) << std::endl;
DoubleType db;
std::cout << "result of db.add(): " << db.add( 123.456, 432.1) << std::endl;
std::cout << "result of db.subtract(): " << db.subtract( 123.456, 432.1) << std::endl;
std::cout << "result of db.multiply(): " << db.multiply( 123.456, 432.1) << std::endl;
std::cout << "result of db.divide(): " << db.divide( 123.456, 432.1) << std::endl;
std::cout << "result of db.add(): " << db.add( 123.456, 0.0) << std::endl;
std::cout << "result of db.subtract(): " << db.subtract( 123.456, 0.0) << std::endl;
std::cout << "result of db.multiply(): " << db.multiply( 123.456, 0.0) << std::endl;
std::cout << "result of db.divide(): " << db.divide( 123.456, 0.0) << std::endl;
IntType i;
std::cout << "result of i.add(): " << i.add( 10, 20) << std::endl;
std::cout << "result of i.subtract(): " << i.subtract( 10, 20) << std::endl;
std::cout << "result of i.multiply(): " << i.multiply( 10, 20) << std::endl;
std::cout << "result of i.divide(): " << i.divide( 10, 20) << std::endl;
std::cout << "result of i.add(): " << i.add( 10, 0) << std::endl;
std::cout << "result of i.subtract(): " << i.subtract( 10, 0) << std::endl;
std::cout << "result of i.multiply(): " << i.multiply( 10, 0) << std::endl;
std::cout << "result of i.divide(): " << i.divide( 10, 0) << std::endl;
std::cout << "good to go!" << std::endl;
}