BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan Right Header

HOME BZU Mail Box Online Games Radio and TV Cricket All Albums
Go Back   BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan > Institute of Computing > Bachelor of Science in Information Technology > BsIT 3rd Semester > Object Oriented Programming

Object Oriented Programming By Mam Sidra Malik


Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 30-05-2009, 02:28 AM
vidi's Avatar
First Time Poster!

 
Join Date: May 2009
Age: 40
Posts: 1
Program / Discipline: Engineering
vidi is on a distinguished road
Default Ex 5 Ch#7

Hello I'm new in this forum and working many hours at this problem's solving but my current problem is that i cant make the do-while loop stops fractions inserting when giving 0/1 fraction. If anybody can post the answer.. Thank you!!
I am uploading 2 files that working and i think are the combination for this solution

Code:
#include <iostream>
#include <cmath>               

using namespace std;

class frac
{
   private:
      long num;                
      long den;                
   public:
      frac() : num(0), 
               den(1)  {}

      frac(long d) : num(d),
                     den(1)  {}          
         
      frac(long n,  long d) : num(n),
                              den(d) {}     
      long getnum(void){return num;}
      long getden(void){return den;}
      void fadd( frac f1,  frac f2);
      bool equals( frac f);
      bool notequals( frac f);
      void putfrac();
      void getfrac();
      void lowterms();
};


void frac::fadd(frac f1, frac f2)
   {                                  
   num = f1.num*f2.den + f1.den*f2.num;
   den = f1.den*f2.den;
   lowterms();
   }

bool frac::equals(frac f)       
   {
   return (num==f.num && den==f.den) ? true : false; 
   }

bool frac::notequals(frac f)    
   {
   return (num!=f.num || den!=f.den) ? true : false;
   }

void frac::putfrac()                  
   {
   cout << num << '/' << den;
   }

void frac::getfrac()                
   {
   char dummychar;

   while(1)
      {
      cin >> num;        
      cin >> dummychar;  
      cin >> den;       
      if(den != 0)//......??
         break; 
      cout << "Denominator can't be 0. Try again: ";
      }
   }


void frac::lowterms()    
{
   long tnum, tden, temp, gcd;
   tnum = labs(num);   //......??      
   tden = labs(den);
   
   if( tnum!=0 && tden==0 )  
   { 
      cout << "Division by 0, illegal fraction: "; 
      exit(1); 
   }
   else if( tnum==0 )       
        { 
            num=0; 
            den = 1; //......??
            return; 
        }


   while(tnum != 0)
      {
      if(tnum < tden)        
         { 
            temp=tnum; 
            tnum=tden; 
            tden=temp; 
         } 
      tnum = tnum - tden;  
      }
            gcd = tden;
            num = num / gcd;         
            den = den / gcd;         
}


int main()
{
   frac frarr[100];
   frac total(0, 0), average;
   int count = 0;
   char ch;

       while(true)    
       {
      cout << "\nEnter fraction (0/1 for exit): "  ;  
      frarr[count++].getfrac();

      }while(frarr[count].getnum()==0 && frarr[count].getden()==1);
  
  
   cout << endl;
  system("PAUSE");
   }
************ ********* ********* **
Start with the fraction class from Exercises 11 and 12 in Chapter 6. Write a main() program that obtains an arbitrary number of fractions from the user, stores them in an array of type fraction, averages them, and displays the result.

I am sorry this is what Ex06Ch6 wants.
************ ********* ********* **
Start with the fraction class from Exercises 11 and 12 in Chapter 6. Write a main() program that obtains an arbitrary number of fractions from the user, stores them in an array of type fraction, averages them, and displays the result.

I am sorry i forgot write what this lab asks.
Attached Files
File Type: txt Ex11Ch6.txt (3.6 KB, 252 views)
File Type: txt Ex3Ch7.txt (1.7 KB, 240 views)
Reply With Quote
Reply

Tags
ch#7


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Best view in Firefox
Almuslimeen.info | BZU Multan | Dedicated server hosting
Note: All trademarks and copyrights held by respective owners. We will take action against any copyright violation if it is proved to us.

All times are GMT +5. The time now is 11:45 AM.
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.