Mathematical actions C++

Hello,

I'm newbie in this forum and new to programming languages... I need to somehow make a mathematical console app, but have no idea where to begin.. Maybe somebody have any material, examples or can help me with the code, as it shouldn't be that difficult :( If needed, I can pay for help.

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
Using an object-oriented programming model, write a program that allows you to perform mathematical actions (addition, subtraction, multiplication, division, comparison) with rational numbers. Rational number is any number that can be expressed as the  fraction p/q of two integers, a numerator p and a non-zero denominator q.
For example: 1/2, 2/3, 15/32, 65/4, 16/5.

Write a class for rational numbers. Class data - rational numbers i.e. pair of integer type variables (numerator and denominator). The class must contain the following methods: addition, subtraction, multiplication, division, more, negative operations +, -, *, /> and multiples of -1.
Methods of additionl, subtraction, multiplication, division, negative  must return rational number. The function more returns the value of the bool type.

 Additional operation a + b in this class will be performed as a.add(b), and the operation a>b is performed as a.more(b).

Each abstract data type requires a constructor that creates objects. In our case, the constructor must create objects using a pair of integers numbers. Since any integer is a rational number (we can express it as the same number divided by 1), so keep in mind that a constructor with one integer parameter is also needed.
Include methods to input and output to the class. The first one must have an istream type argument and read a rational number from the keyboard or file in the form of a numerator / denominator. The second method must have an ostream type argument and show the rational number in the form of a numerator/denominator on the disply or file.

Include methods to enter and bring to the class. The first one must have an istream type argument and scan a rational number from the keyboard or file in the form of a numerator / denominator. The second method must have an ostream type argument and display the rational number in the form of a numerator / denominator on the screen or file.

Write the main () function and test your class implementation by counting the results of the following actions:
a / b + c / d
(a * d + b * c) / (b * d)
a / b-c / d
(a * d-b * c) / (b * d)
(a / b) / (c / d)
(a * d) / (c * b)
- (a / b)
(a / b)> (c / d)
(a / b) = = (c / d)

where a and b are rational numbers.
The numerator can be positive or negative. The denominator must be positive.

Start with what you know!

Do you know how to declare a class? If yes, declare one with the name of your choice! If not, google it!

Do you know how classes work? If yes, great, let's declare our members! If not, google and learn about classes!

Do you know about constructors? If yes, make two, one which takes both numerator and denominator, and another which takes only numerator. If not, google it!

Do you know about methods? If yes, good, declare the methods in the class with no body. And now think about what should come in the body of these methods.

Implement everything you know and try to break down what you don't know and solve it individually in parts. Well if you don't know anything, you can always learn!

All you need to know about is basic C++ and classes for this question, so start by learning classes if you don't know about them already.
The program calls for a class to represent rational numbers and whole bunch of methods. Start by defining the class with:
- the data members
- methods to read from an istream and write to an ostream. The format read/written should be numerator / denominator
- Write a main program that declares a rational number, reads it from cin and writes it to cout.

Once you have this program working, go back and add a method to multiply rational numbers. Modify the main program to input two rationals and print out their product.

Now you're on a roll! Read through the whole assignment and add declarations to the class for all of the other methods. Them implement the code for them one by one.

When it comes to addition and subtraction, think carefully about how to add/subtract fractions.

One thing that isn't in the assignment that I strongly recommend is a method to cast the rational into "standard form." That means numerator and denominator are in lowest terms and the denominator is always positive. Then you can call this method at the end of each arithmetic function.

Good luck. If you find yourself stuck for more than an hour, post the code that you have and ask a specific question.
Topic archived. No new replies allowed.