Fraction Calculator

closed account (Diz60pDG)
I am writing a program where the user can enter two fractions and an operand (either +, -, *, or /) and the program will calculate the new fraction. The program also has to simplify the fraction.

Here is what I am so far and it is not working correctly, any suggestions?
I have just started coding so I am unsure of some things.

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
#include <iostream>
using namespace std;

int add(int n1, int d1, int n2, int d2, int &n3, int &d3);
int subtract(int n1, int d1, int n2, int d2, int &n3, int &d3);
int multiply(int n1, int d1, int n2, int d2, int &n3, int &d3);
int divide(int n1, int d1, int n2, int d2, int &n3, int &d3);
int calculate(int choice, int n1, int d1, int n2, int d2, int &n3, int &d3);


int main()
{

	cout << "Fraction Calculator" << endl;

	int n1, n2, d1, d2;
	int choice;
	int n3, d3;

	cout << "Enter the numerator for the first fraction: ";
	cin >> n1;
	cout << "Enter the denominator for the first fraction: ";
	cin >> d1;

	cout << "Enter the operation: ";
	cin >> choice;

	cout << "Enter the numerator of the second fraction: ";
	cin >> n2;
	cout << "Enter the demonminator of the second fraction: ";
	cin >> d2;

	calculate(choice, n1, d1, n2, d2, n3, d3);

	cout << "Your new fraction is " << n3 << "/" << d3 << endl;

	return 0;
}

int add(int n1, int d1, int n2, int d2, int &n3, int &d3)
{
	n3 = ((n1*d2) + (n2*d1)) / (d1* d2);
	d3 = d1 * d2;

	return n3 / d3;
}

int subtract(int n1, int d1, int n2, int d2, int &n3, int &d3)
{
	n3 = (n1*d2) - (n2*d1);
	d3 = d1 * d2;

	return n3 / d3;
}

int multiply(int n1, int d1, int n2, int d2, int &n3, int &d3)
{
	n3 = n1 * n2;
	d3 = d1 * d2;

	return n3 / d3;
}

int divide(int n1, int d1, int n2, int d2, int &n3, int &d3)
{
	n3 = n1 * d2;
	d3 = n2 * d1;

	return n3 / d3;
}
int calculate(int choice, int n1, int d1, int n2, int d2, int &n3, int &d3)
{

	switch (choice){
	case '0':
		exit(-1);
		break;
	case '1':
		add(n1, d1, n2, d2, n3, d3);
		break;
	case '2':
		subtract(n1, d1, n2, d2, n3, d3);
		break;
	case '3':
		multiply(n1, d1, n2, d2, n3, d3);
		break;
	case '4':
		divide(n1, d1, n2, d2, n3, d3);
		break;
	default:
		return choice;
	}
}
Hi there,

a good start! Some points come to mind...

1. Why are you always passing n3 and d3 as references?

2. You are not handling any errors. I can crash your program by entering 0 as for n2 or d1.

3. The cleanest (and more C++-like) approach would be to write a class "Fraction", which has numerator and denominator as class members, and provides the calculations as member functions or overloaded operators.
That would also enable you to chain calculations with more fractions, and return fractions as results.

4. I would make the reduction of the fraction a separate member function of that class.

5. You might want to add a function that approximates the fraction as a float value.

Cheers,
Frank
Topic archived. No new replies allowed.