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
|
# include <iostream>
using namespace std;
class Fraction{
public:
void sum(int a, int b, int c, int d, int& numerator, int& denominator) ();
void mult(int a, int b, int c, int d, int& numerator, int& denominator) ();
private:
int num1,num2,num3,num4; // ints to hold values of numerator and denominators
int numerator,denominator; // multiplied or added up fraction
char again='y';
};
// fraction sum function
void sum(int a, int b, int c, int d, int& numerator, int& denominator)
{
int array[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101}; // array filled with primes
numerator = (a * d) + ( b * c);
denominator = b * d;
cout << "The addition of your fractions is: " <<numerator;
cout << "/" << denominator << endl;
// for loops that finds the greatest common factor by moding with an array filed with prime numbers
for(int i=0;i<25;i++){
if(numerator % array[i]==0 && denominator % array[i]==0)
{
numerator/=array[i];
denominator/=array[i];
i=0;
}
}
// display the fraction is reduced form.
cout << "The reduced addition of your fractions is: " <<numerator;
cout << "/" << denominator << endl;
}
// function for multiplication of two fractions
void mult(int a, int b, int c, int d, int& numerator, int& denominator)
{
int array[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101};
numerator = a * c;
denominator = b* d;
cout << "The multiple of your fractions is: " <<numerator;
cout << "/" << denominator << endl;
// for loops that finds the greatest common factor by moding with an array filed with prime numbers
for(int i=0;i<25;i++){
if(numerator % array[i]==0 && denominator % array[i]==0)
{
numerator/=array[i];
denominator/=array[i];
i=0;
}
}
// display the fraction is reduced form.
cout << "The reduced multiple of your fractions is: " <<numerator;
cout << "/" << denominator << endl;
}
int main()
{
// lets user go as many times as desired
while (again == 'Y' || again== 'y') {
cout << "Enter numerator of 1st fraction: ";
cin >> num1;
cout << "Enter denominator of 1st fraction: ";
cin >> num2;
// if user enters 0 it prompts to enter number !=0
while(num2==0){
cout << "Denominator cannot be 0. Enter a new number: ";
cin >> num2;
}
cout << "Enter numerator of 2nd fraction: ";
cin >> num3;
cout << "Enter denominator of 2nd fraction: ";
cin >> num4;
while(num4==0){
cout << "Denominator cannot be 0. Enter a new number: ";
cin >> num2;
}
// call functions for addition and subtraction
sum(num1,num2,num3,num4,numerator,denominator);
mult(num1,num2,num3,num4,numerator,denominator);
cout << " Would you like to go again (Y or N): ";
cin >> again;
}
return 0;
}
|