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
|
#include<iostream>
#include<cmath>
using namespace std;
void getFraction(int&, int&);
void readFracProblem(int&, int&, int&, int&, char&); //For later use with +,-,*,/
void add(int&, int&, int, int, int); //I added the last "int" for my program
int gcd(int, int);
int lcm(int, int, int); //I added the last "int" for program
void normalizeFraction(int&, int&);
void displayFraction(int, int);
int main(){
int n1, d1, n2, d2, gcdNumber;
cout << "Enter a fraction (n / d): ";
getFraction(n1, d1);
cout << "Enter a fraction (n / d): ";
getFraction(n2, d2);
gcdNumber = gcd(d1, d2);
add(n1, d1, n2, d2, gcdNumber);
displayFraction(n1, d1);
return 0;
}
void getFraction(int& n, int& d){
char slash; //Should it be "char = slash;" ?
cin >> n >> slash >> d;
}
int lcm(int accD, int d, int gcd){
int lcm;
lcm = (accD * d) / gcd;
return lcm;
}
void add(int& accN, int& accD, int n, int d, int gcd){
int lcmNumber = lcm(accD, d, gcd); //*I'm not sure where these should go
int multiplier = lcmNumber / accD; //*
accN = accN * multiplier; //*
multiplier = lcmNumber / d; //*
n = n * multiplier; //*
accN = accN + n; //*
accD = lcmNumber; //*I'm not sure where these should go
}
int gcd(int a, int b){
int remainder, gcdNumber;
do{
remainder = a % b;
gcdNumber = a / b;
}while(remainder != 0);
return gcdNumber;
}
void normalizeFraction(int& n, int& d){
if(d < 0){
d = -d;
n = -n;
}
int absN = abs(n);
int gcdNumber = gcd(absN, d);
n = n / gcdNumber;
d = d / gcdNumber;
}
void displayFraction(int n, int d){
normalizeFraction(n, d); //Does this belong here?
cout << n << " / " << d << endl;
}
|