Am back again please help me.

0 Teemmy Ellorry · February 20, 2015
Design and code a class named Fraction that holds information about a rational number.  Place your class definition and implementation in a file named Fraction.h (so there is no file Fraction.cpp).
You can include the Fraction class in the file main.cpp. You can see that it is not always necessary to include your application in separate files.
 
Upon instantiation, a Fraction may receive two integer values holding the numerator and denominator of the rational number.  If the object receives no values, it set numerator = 0 and denominator = 1. 
 
Your class includes the following data members and member functions:
  private:
   int n,d; //numerator and the denominator
   bool reduction(); // reduct the fraction and return true if real reduction appeared
  public:
   Fraction(int p=0, int q=1) {n=p;d=q;} //constructor
   friend istream & operator>>(istream &is, Fraction &a); // Input fraction a
   friend ostream & operator<<(ostream &os, const Fraction &a); // display fraction
   double getValue(); //return value
   void display(); // display fraction with reduction and value
   Fraction operator+(Fraction b); // overload + operator
   Fraction operator-(Fraction b);
   Fraction operator*(Fraction b);
   Fraction operator/(Fraction b);
   bool operator==(Fraction b);
   bool operator!=(Fraction b);
   bool operator>(Fraction b);
   bool operator>=(Fraction b);
   bool operator<(Fraction b);
   bool operator<=(Fraction b);
 
The main program that tests the Fraction class is:
//=========================================
#include "Fraction.h"
int main()
  { Fraction a, b, c;
    int choice;
    while(true)
       { system("CLS");
         cout<<endl;
         cout<<"  Choose one of the following options:"<<endl;
         cout<<"  1. Enter fraction a"<<endl;
         cout<<"  2. Display fraction a"<<endl;
         cout<<"  3. Enter fraction b"<<endl;
         cout<<"  4. Calculate c = a + b "<<endl;
         cout<<"  5. Calculate c = a - b "<<endl;
         cout<<"  6. Calculate c = a * b "<<endl;
         cout<<"  7. Calculate c = a / b "<<endl;
         cout<<"  8. Test a == b "<<endl;
         cout<<"  9. Test a != b "<<endl;
         cout<<" 10. Test a > b "<<endl;
         cout<<" 11. Test a >= b "<<endl;
         cout<<" 12. Test a < b "<<endl;
         cout<<" 13. Test a <= b "<<endl;
         cout<<"  0. Quit"<<endl;
         cout<<"  Your selection: ";cin>>choice;
         if(choice==0)
            {cout<<"Have a nice day!"<<endl;break;}
         switch(choice)
            { case  1: cin>>a;break;
              case  2: cout<<"a = ";a.display();break;
              case  3: cin>>b;break;
              case  4: c = a+b;
                           cout<<endl<<a<<" + "<<b<<" = "<<c<<" = "<<c.getValue()<<endl;break;
              case  5: c = a-b;
                           cout<<endl<<a<<" - "<<b<<" = "<<c<<" = "<<c.getValue()<<endl;break;
              case  6: c = a*b;
                           cout<<endl<<a<<" * "<<b<<" = "<<c<<" = "<<c.getValue()<<endl;break;
              case  7: c = a/b;
                           cout<<endl<<a<<" / "<<b<<" = "<<c<<" = "<<c.getValue()<<endl;break;
              case  8: if(a==b)
                            cout<<endl<<a<<" = "<<b<<endl;
                            else
                            cout<<endl<<a<<" # "<<b<<endl;
                            break;
              case  9: if(a!=b)
                            cout<<endl<<a<<" # "<<b<<endl;
                            else
                            cout<<endl<<a<<" = "<<b<<endl;
                            break;
              case 10: if(a>b)
                            cout<<endl<<a<<" > "<<b<<endl;
                            else
                            cout<<endl<<a<<" not greater than "<<b<<endl;
                            break;
              case 11: if(a>=b)
                            cout<<endl<<a<<" >= "<<b<<endl;
                            else
                            cout<<endl<<a<<" < "<<b<<endl;
                            break;
              case 12: if(a<b)
                            cout<<endl<<a<<" < "<<b<<endl;
                            else
                            cout<<endl<<a<<" not less than "<<b<<endl;


                            break;
              case 13: if(a<=b)
                            cout<<endl<<a<<" <= "<<b<<endl;
                            else
                            cout<<endl<<a<<" > "<<b<<endl;
                            break;
              default: cout<< "**Invalid choice. Try again.**" << endl;
            }
         cout<<endl;
         system("PAUSE");
       }
     return(0);
  }

Post a Reply

Replies

Oldest  Newest  Rating
0 Teemmy Ellorry · February 20, 2015
THAT IS MY OWN CODE BUT IT IS NOT WORKING THE WAY I WANT IT TO WORK JUST HELP ME CORRECT MY MISTAKE IN ACCEPTING VALUES FROM THE USER AND I CAN FINISH THE REST... THANKS
0 Teemmy Ellorry · February 20, 2015
#include <iostream>
using namespace std;

class Fraction
{
private:
 int n, d, c;
 bool reduction()
 {
  for(int k=1; k<=n && k <= d; k++)
{
if(n % k == 0 && d % k == 0)   //Finding the greatest common divisor
   {
   c = k;  
   }
}
  return true;
 }
 public:
  int p, q,num;
  Fraction(int p=0, int q=1)
 {
    n=p;d=q;                     //constructor
 }
        friend istream & operator>>(istream &is, Fraction &a)
{
cout<<"Enter Numerator: ";
cin>>a.p;
cout<<"Enter Denominator: ";
cin>>a.q;
return is>>a.num;                // Input fraction a
}      
        friend ostream & operator<<(ostream &os, const Fraction &a)
{
return os<<a.num;                          // display fraction
}
        double getValue()
{                           
                            //return value
}
        void display()
{
cout<<n<<"/"<<d<<" :";                               // display fraction with reduction and value 
   cout<<n/reduction()<<" / "<<d/reduction()<<endl;
}
        Fraction operator+(Fraction b)
{
                                      // overload + operator
}
        Fraction operator-(Fraction b)
        {
       
}
        Fraction operator*(Fraction b)
        {
       
}
        Fraction operator/(Fraction b)
        {
       
}
        bool operator==(Fraction b)
        {
       
}
        bool operator!=(Fraction b)
        {
       
}
        bool operator>(Fraction b)
        {
       
}
        bool operator>=(Fraction b)
        {
       
}
        bool operator<(Fraction b)
        {
       
}
        bool operator<=(Fraction b)
        {
       
}

};
int main()
{
Fraction a, b, c;
    int choice; 
    while(true)
       { system("CLS");
         cout<<endl;
         cout<<"  Choose one of the following options:"<<endl;
         cout<<"  1. Enter fraction a"<<endl;
         cout<<"  2. Display fraction a"<<endl;
         cout<<"  3. Enter fraction b"<<endl;
         cout<<"  4. Calculate c = a + b "<<endl;
         cout<<"  5. Calculate c = a - b "<<endl;
         cout<<"  6. Calculate c = a * b "<<endl;
         cout<<"  7. Calculate c = a / b "<<endl;
         cout<<"  8. Test a == b "<<endl;
         cout<<"  9. Test a != b "<<endl;
         cout<<" 10. Test a > b "<<endl;
         cout<<" 11. Test a >= b "<<endl;
         cout<<" 12. Test a < b "<<endl;
         cout<<" 13. Test a <= b "<<endl;
         cout<<"  0. Quit"<<endl;
         cout<<"  Your selection: ";cin>>choice;
         if(choice==0) 
            {
  cout<<"Have a nice day!"<<endl;
  break;
}
         switch(choice)
            { case  1: cin>>a;
 break;
              case  2: cout<<"a = ";a.display();break;
              case  3: cin>>b;break;
              case  4: c = a+b;
                           cout<<endl<<a<<" + "<<b<<" = "<<c<<" = "<<c.getValue()<<endl;break;
              case  5: c = a-b;
                           cout<<endl<<a<<" - "<<b<<" = "<<c<<" = "<<c.getValue()<<endl;break;
              case  6: c = a*b;
                           cout<<endl<<a<<" * "<<b<<" = "<<c<<" = "<<c.getValue()<<endl;break;
              case  7: c = a/b;
                           cout<<endl<<a<<" / "<<b<<" = "<<c<<" = "<<c.getValue()<<endl;break;
              case  8: if(a==b)
                            cout<<endl<<a<<" = "<<b<<endl;
                            else
                            cout<<endl<<a<<" # "<<b<<endl;
                            break;
              case  9: if(a!=b)
                            cout<<endl<<a<<" # "<<b<<endl;
                            else
                            cout<<endl<<a<<" = "<<b<<endl;
                            break;
              case 10: if(a>b)
                            cout<<endl<<a<<" > "<<b<<endl;
                            else
                            cout<<endl<<a<<" not greater than "<<b<<endl;
                            break;
              case 11: if(a>=b)
                            cout<<endl<<a<<" >= "<<b<<endl;
                            else
                            cout<<endl<<a<<" < "<<b<<endl;
                            break;
              case 12: if(a<b)
                            cout<<endl<<a<<" < "<<b<<endl;
                            else
                            cout<<endl<<a<<" not less than "<<b<<endl;
                            break;
              case 13: if(a<=b)
                            cout<<endl<<a<<" <= "<<b<<endl;
                            else
                            cout<<endl<<a<<" > "<<b<<endl;
                            break;
              default: cout<< "**Invalid choice. Try again.**" << endl;
            }
         cout<<endl;
         system("PAUSE");
       }
return 0;
}
  • 1

C++

106,982 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator