I am trying to make this project and im very new to this but i really want to learn this can anyone check this code for me and tell me whats wrong with it. I have to get it to add 2 fractions display result then display result in decimal form Thanks.
//Rational class interface
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
public:
Rational(int numerator = 0,int denominator = 1);//default constructor
int setTotal;
Rational add(const Rational&);//add prototype
Rational subtract(const Rational&);//subtract prototype
Rational multiply(const Rational&);//multiply prototype
Rational divide(const Rational&);//divide prototype
void printRational();//function prototype
void printRationalAsDouble();//function prototype
void reduction();//utility prototype
int setNumerator;
int setDenominator;
private:
int numerator;//data member
int denominator;//data member
};
#endif
// Lab5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Rational.h"
#include <iostream>
#include<cmath>
using namespace std;
int numerator;
int denominator;
int t;
int total;
int setNumerator;
int setDenominator;
Rational::Rational(int n,int d)
{
numerator = d < 0 ? -n : n;
denominator = d < 0 ? -d : d;
reduction();
}
Rational Rational::add(const Rational& a)
{
Rational t;
t.numerator = a.numerator * denominator + a.denominator * numerator;
t.denominator = a.denominator * denominator;
t.reduction();
return t;
}
Rational Rational::subtract(const Rational& s)
{
Rational t;
t.numerator = s.numerator * denominator - s.denominator * numerator;
t.denominator = s.denominator * denominator;
t.reduction();
return t;
}
Rational Rational::multiply(const Rational& m)
{
Rational t;
t.numerator = m.numerator * numerator;
t.denominator = m.denominator * denominator;
t.reduction();
return t;
}
Rational Rational::divide(const Rational& v)
{
Rational t;
t.numerator = v.denominator * numerator;
t.denominator = denominator * v.numerator;
t.reduction();
return t;
}
void Rational::printRational()
{
if(denominator == 0)
cout<<"DIVIDE BY ZERO ERROR"<<endl;
else if(numerator == 0)
cout<<0;
else
cout<<numerator<<"/"<<denominator<<endl;
}
void Rational::printRationalAsDouble()
{
static_cast<double>(numerator / denominator);
}
void Rational::reduction()
{
int largest = numerator > denominator ? numerator : denominator;
int gcd = 0;
for(int loop = largest; loop >= 2; loop--)
if(numerator % loop == 0 && denominator % loop == 0)
gcd = loop;
else if(gcd != 0)
numerator /= gcd;
denominator /= gcd;
}
int _tmain(int argc, _TCHAR* argv[])
{
Rational obj1,obj2,obj3;
{
obj1 = numerator,denominator;
obj2 = numerator,denominator;
obj3 = t;
}
return 0;
}