Quantcast

Maximum PC

It is currently Wed Apr 23, 2014 1:48 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 19 posts ] 
Author Message
 Post subject: Program I'm working on
PostPosted: Sun Apr 03, 2005 7:06 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
No help needed at the moment

Class
Code:
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
#include <fstream>


class Fractions
{
   int numerator, denominator;

   
public:
   Fractions();
   void define_Fractions(int num, int denom);
   Fractions inverse(Fractions right);
   void print_Fractions();
   Fractions operator+(const Fractions &right);
   Fractions operator-(const Fractions &right);
   Fractions operator*(const Fractions &right);
   Fractions operator/(const Fractions &right);
   Fractions operator|(const Fractions &right);
   bool operator==(const Fractions &right);
   bool operator!=(const Fractions &right);
   bool operator>(const Fractions &right);
   bool operator>=(const Fractions &right);
   bool operator<(const Fractions &right);
   bool operator<=(const Fractions &right);



};

Fractions::Fractions()
{
   numerator = 1;
   denominator = 1;
}

void Fractions::define_Fractions(int num, int denom)
{
   numerator= num;
   denominator = denom;
}

void Fractions::print_Fractions()
{
   cout<< numerator <<"/" <<denominator;
}

/////////////////////////////////////////////////
//  Function the returns the inverse of a Fractions
/////////////////////////////////////////////////
Fractions Fractions::inverse(Fractions right)
{
   Fractions temp;
   numerator = temp.denominator;
   denominator = temp.numerator;
   numerator= temp.numerator;
   denominator = temp.denominator;
   return temp;
}



///////////////////////////////////////////////////
//  Overloaded addition operator for class Fractions
///////////////////////////////////////////////////
Fractions Fractions::operator+(const Fractions &right)
{
   Fractions temp;

   temp.numerator = (numerator * right.denominator) + (denominator * right.numerator);
   temp.denominator = denominator * right.denominator;
   return temp;

}

//////////////////////////////////////////////////////
//  Overloaded subtraction operator for class Fractions
//////////////////////////////////////////////////////
Fractions Fractions::operator-(const Fractions &right)
{
   Fractions temp;

   temp.numerator = (numerator * right.denominator) - (denominator * right.numerator);
   temp.denominator = denominator * right.denominator;
   return temp;

}

/////////////////////////////////////////////////////////
//  Overloaded multiplication operator for class Fractions
/////////////////////////////////////////////////////////
Fractions Fractions::operator*(const Fractions &right)
{
   Fractions temp;

   temp.numerator = (numerator * right.denominator) * (denominator * right.numerator);
   temp.denominator = denominator * right.denominator;
   return temp;

}

///////////////////////////////////////////////////
//  Overloaded division operator for class Fractions
///////////////////////////////////////////////////
Fractions Fractions::operator/(const Fractions &right)
{
   Fractions temp;

   temp.numerator = (numerator * right.denominator) / (denominator * right.numerator);
   temp.denominator = denominator * right.denominator;
   return temp;

}

//////////////////////////////////////////////////
//  Overloaded inverse operator for class Fractions
//////////////////////////////////////////////////
Fractions Fractions::operator|(const Fractions &right)
{
   Fractions temp;

   temp.numerator = (numerator * right.denominator) / (denominator * right.numerator);
   temp.denominator = denominator * right.denominator;
   return temp;

}

///////////////////////////////////////////////////
//  Overloaded equal to operator for class Fractions
///////////////////////////////////////////////////
bool Fractions::operator==(const Fractions &right)
{
   if(numerator == right.numerator && denominator == right.denominator)
      return true;
   else
      return false;

}

///////////////////////////////////////////////////////////
//  Overloaded doesn't equal to operator for class Fractions
///////////////////////////////////////////////////////////
bool Fractions::operator!=(const Fractions &right)
{
   if(numerator == right.numerator && denominator == right.denominator)
      return false;
   else
      return true;

}

///////////////////////////////////////////////////////
//  Overloaded greater than operator for class Fractions
///////////////////////////////////////////////////////
bool Fractions::operator>(const Fractions &right)
{
   if(numerator / denominator > right.numerator/right.denominator)
      return true;
   else
      return false;

}

////////////////////////////////////////////////////////////////
//  Overloaded greater than or equal operator for class Fractions
////////////////////////////////////////////////////////////////
bool Fractions::operator>=(const Fractions &right)
{
   if(numerator / denominator > right.numerator/right.denominator)
      return true;
   else if(numerator / denominator == right.numerator/right.denominator)
      return true;
   else
      return false;

}

/////////////////////////////////////////////////////
//  Overloaded less than operator for class Fractions
/////////////////////////////////////////////////////
bool Fractions::operator<(const Fractions &right)
{
   if(numerator / denominator < right.numerator/right.denominator)
      return true;
   else
      return false;

}

/////////////////////////////////////////////////////////////
//  Overloaded less than or equal operator for class Fractions
/////////////////////////////////////////////////////////////
bool Fractions::operator<=(const Fractions &right)
{
   if(numerator / denominator < right.numerator/right.denominator)
      return true;
   else if(numerator / denominator == right.numerator/right.denominator)
      return true;
   else
      return false;

}


Top
  Profile  
 
 Post subject:
PostPosted: Sun Apr 03, 2005 8:49 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Main

Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include "Fractions.h"
using namespace std;





int main()
{

   int num_1 = 1, denom_1 = 1, num_2 = 1, denom_2 = 1;
   int temp, temp_2, count = 0;
   char operation[2];
   bool value, print = false;
   Fractions left, right, together;
   ofstream outfile;
   ifstream infile;

   outfile.open("output.txt",ios::app);
   infile.open("Data5.txt",ios::in);

   cout<<"  Left                  Right"<<endl;
   cout<<"Operand  Operator      Operand   Result"<<endl;

   while(!infile.eof())
   {
      infile >> operation[0];
      infile >> temp;
      infile >> temp_2;

      if( temp < 10)
      {
         num_1 = temp;
         count = 1;
      }
      else
      {   
         operation[1] = temp;
         count = 1;
         if (temp_2 =='-')
         {
            count = 2;
         }

      }

      if(count = 0)
      {
         temp_2 = num_1;
         infile >> denom_1;
         infile >> num_2;
         infile >>denom_2;
      }
      else if(count =2)
      {
         infile >> num_1;
         infile >> denom_1;
         infile >> num_2;
         infile >>denom_2;

      }
      else
      {
   
         temp_2 = denom_1;
         infile >> num_2;
         infile >>denom_2;
         


      }


      left.define_Fractions(num_1, denom_1);
      right.define_Fractions(num_2, denom_2);

      if(operation[0] == '+')
         together = left + right;
      if(operation[0] == '-')
         together = left - right;
      if(operation[0] == '*')
         together = left * right;
      if(operation[0] == '/')
         together = left / right;
      if(operation[0] == '@')
         together = left | right;
      if(operation[0] == '<' && operation[1] == '=')
      {
         value = left <= right;
         print = true;
      }
      if(operation[0] == '<' && operation[1] != '=')
      {
         value = left < right;
         print = true;
      }
      if(operation[0] == '>' && operation[1] == '=')
      {
         value = left >= right;
         print = true;
      }
      if(operation[0] == '>' && operation[1] != '=')
      {
         value = left < right;
         print = true;
      }
      if(operation[0] == '=' && operation[1] == '=')
      {
         value = left == right;
         print = true;
      }
      if(operation[0] == '!' && operation[1] == '=')
      {
         value = left !=right;
         print = true;
      }


      left.print_Fractions();
      cout<<"         "<<operation[0];
      if(operation[1] > 10)
         cout<<operation[1];
      cout<<"            ";
      right.print_Fractions();
      cout<<"      ";
      if(print == false)
         together.print_Fractions();
      else
         cout<<value;

      cout<<endl;




      print = false;
      value = false;
      operation[0]=0;
      operation[1]=0;
      count = 0;
   }





   return 0;
}



Last edited by alex911 on Sun Apr 03, 2005 9:31 pm, edited 2 times in total.

Top
  Profile  
 
 Post subject:
PostPosted: Sun Apr 03, 2005 9:11 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Input file

Code:
+ 1 2 3 4- 5 8 6 7* -7 9 4 5/ 6 7 2 3>= 1 2 3 4== 1 2 3 4< 1 2 3 4@ 1 2


Last edited by alex911 on Mon Apr 04, 2005 7:46 pm, edited 1 time in total.

Top
  Profile  
 
 Post subject:
PostPosted: Sun Apr 03, 2005 11:58 pm 
Smithfield
Smithfield
User avatar

Joined: Sun Sep 05, 2004 9:01 am
Posts: 8091
What do you want? A cookie?


Top
  Profile  
 
 Post subject:
PostPosted: Mon Apr 04, 2005 3:16 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
urmumsacow wrote:
What do you want? A cookie?


Actually I need help with reading the input. I post it there for people I know to see.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Apr 04, 2005 6:06 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Another attempt at input


Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include "Fractions.h"
#include <string>
using namespace std;





int main()
{

   int num_1 = 1, denom_1 = 1, num_2 = 1, denom_2 = 1;
   int temp = 0;
   char operation[2];
   operation[0]='0';
   operation[1]='0';
   bool value, print = false;
   Fractions left, right, together;
   ofstream outfile;
   ifstream infile;




   outfile.open("output.txt",ios::app);
   infile.open("Data5.txt",ios::in);

   cout<<"  Left                  Right"<<endl;
   cout<<"Operand  Operator      Operand   Result"<<endl;

   while(!infile.eof())
   {
   

      operation[0] = infile.get();
      temp = infile.get();
      if(temp == '=')
         operation[1] = temp;
      

      if(operation[0]=='@')
      {
         cout<<"first"<<endl;
         infile>>num_1;
         cout<<num_1<<endl;
         infile>>denom_1;
         cout<<denom_1<<endl;
      }

      else
      {

            cout<<"second"<<endl;
            infile>>num_1;
            cout<<num_1<<endl;
            infile>>denom_1;
            cout<<denom_1<<endl;
            infile>>num_2;
            cout<<num_2<<endl;
            infile>>denom_2;
            cout<<denom_2<<endl;
   
      }




      left.define_Fractions(num_1, denom_1);
      right.define_Fractions(num_2, denom_2);

      if(operation[0] == '+')
         together = left + right;
      if(operation[0] == '-')
         together = left - right;
      if(operation[0] == '*')
         together = left * right;
      if(operation[0] == '/')
         together = left / right;
      if(operation[0] == '@')
         together = left.inverse();
      if(operation[0] == '<' && operation[1] == '=')
      {
         value = left <= right;
         print = true;
      }
      if(operation[0] == '<' && operation[1] != '=')
      {
         value = left < right;
         print = true;
      }
      if(operation[0] == '>' && operation[1] == '=')
      {
         value = left >= right;
         print = true;
      }
      if(operation[0] == '>' && operation[1] != '=')
      {
         value = left < right;
         print = true;
      }
      if(operation[0] == '=' && operation[1] == '=')
      {
         value = left == right;
         print = true;
      }
      if(operation[0] == '!' && operation[1] == '=')
      {
         value = left !=right;
         print = true;
      }


      left.print_Fractions();
      cout<<"         "<<operation[0];
      if(operation[1] == '=')
         cout<<operation[1];
      cout<<"            ";
      right.print_Fractions();
      cout<<"      ";
      if(print == false)
         together.print_Fractions();
      else
         cout<<value;

      cout<<endl;




      print = false;
      value = false;
      operation[0]='0';
      operation[1]='0';
      num_1 = 1;
      denom_1 = 1;
      num_2 = 1;
      denom_2 = 1;
      temp = 0;

   }





   return 0;
}



Last edited by alex911 on Mon Apr 04, 2005 8:25 pm, edited 1 time in total.

Top
  Profile  
 
 Post subject:
PostPosted: Mon Apr 04, 2005 7:57 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Get a book on writing compilers and write a good parser.

Basically with fractions all you'll really need to do is tokenize the input (separate by operators) and use a bit of logic when you interpret the code. You can make a syntax tre if you want, and solve bottom-up if you really want to, but something like this probably doesn't require one.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Apr 04, 2005 8:12 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
I do not understand what you mean by that...


Top
  Profile  
 
 Post subject:
PostPosted: Mon Apr 04, 2005 8:14 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
_alex911 wrote:
Input file

Code:
+ 1 2 3 4- 5 8 6 7* -7 9 4 5/ 6 7 2 3>= 1 2 3 4== 1 2 3 4< 1 2 3 4@ 1 2

Maybe I'm being dense today, but I don't understand that input string. Is it prefix notation? It appears to be both pre and postfix! Can you write part of it out in English, that would help. =)

My current guess is...
1 2 means 1/2, right?

so...
1/2 + 3/4 - 5/8 * 6/7 - 7/9 / 4/5 or something....


Top
  Profile  
 
 Post subject:
PostPosted: Mon Apr 04, 2005 8:26 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
1/2 + 3/4 solve answer
4/8 - 6/7 solve answer
etc

is 1 true or false in a bool?

I updated the code, i think it works now with the final main i have...


Top
  Profile  
 
 Post subject:
PostPosted: Mon Apr 04, 2005 8:34 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Output, think it is doing things right?

Code:
Left                  Right
Operand  Operator      Operand   Result
second
1
2
3
4
1/2         +            3/4      10/8
second
5
8
6
7
5/8         -            6/7      -13/56
second
-7
9
4
5
-7/9         *            4/5      -28/45
second
6
7
2
3
6/7         /            2/3      18/14
second
1
2
3
4
1/2         >=            3/4      1
second
1
2
3
4
1/2         ==            3/4      0
second
1
2
3
4
1/2         <            3/4      0
first
1
2
1/2         @            1/1      2/1
Press any key to continue


Top
  Profile  
 
 Post subject:
PostPosted: Tue Apr 05, 2005 6:22 pm 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
How would I make the class function "print" write to file?

Thanks.


Top
  Profile  
 
 Post subject:
PostPosted: Tue Apr 05, 2005 7:39 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
_alex911 wrote:
How would I make the class function "print" write to file?

Thanks.


If I were in your situation, I'd take an ostream parameter in, with a default parameter of NULL. That way if they didn't specify an output stream, use std::cout, but if they did, you can use the specified output stream. Just use the ofstream class to write to a file and specify that as the stream.

This is where the power of classes and inheritance really starts to shine :)


Top
  Profile  
 
 Post subject:
PostPosted: Wed Apr 06, 2005 4:11 am 
Clawhammer
Clawhammer
User avatar

Joined: Sat Jun 26, 2004 2:16 pm
Posts: 3600
Kybo_Ren wrote:
_alex911 wrote:
How would I make the class function "print" write to file?

Thanks.


If I were in your situation, I'd take an ostream parameter in, with a default parameter of NULL. That way if they didn't specify an output stream, use std::cout, but if they did, you can use the specified output stream. Just use the ofstream class to write to a file and specify that as the stream.

This is where the power of classes and inheritance really starts to shine :)


I passed into the call of the print function (ostream &os) in the prototype and (outfile) and it worked.


Top
  Profile  
 
 Post subject:
PostPosted: Wed Apr 06, 2005 6:38 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
What was I thinking? Just make the default parameter std::cout. You'll save yourself inefficient and ugly code.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 8:48 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
In logic, it is...

true 1 on
false 0 off

In some programming languages, C or C++ for example...

true !0
false 0

I'm not sure if it is a requirement for you project, but it appears that your program doesn't attempt to reduce fractions which makes for some potential errors. For example, 1/2 > 2/4 should return 0! I'm not sure if your program handles correctly.

There are two approaches for addition (off the top of my head)...

1/2 + 3/4 = 4/8 + 6/8 = 10/8 then reduce to 5/4

or check whether one of the divisors can divide the other...

Code:
//freestyle programming - I haven't checked it - use at your own risk
int n1 = 1, d1 = 2, n2 = 3, d2 = 4;
int n3 = 0, d3 = 0;  //the answer

if (Math.max(d1,d2) % Math.min(d1,d2) == 0) {
    if (d1 > d2) {
        n3 = n1 + (n2 * (d1 / d2));
        d3 = d1;
    } else {  //if d2 >= d1
        n3 = n2 + (n1 * (d2 / d1));
        d3 = d2;
    }
} else {
    d3 = (n1 * d2) + (n2 * d1);
}


I'll let someone else take a crack at one of the other ops.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 6:02 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
In pure C++, it would be something like
Code:
struct Rational{
int num, den;
};

std::vector<int> Factors(const int &num)
{
     std::vector<int> factors;

     if(num == 0){
          return factors;
     }

     for(int i = 2; i <= num; ++i)
     {
          if(num% i == 0){
               factors.push_back(i);
          }
     }

     if(num < 0)
     {
          factors.push_back(-1);
     }

     return factors;
}


void ReduceFraction(Rational *frac)
{
     if(frac == NULL){
          return;
     }

     if(frac->den == 0){
          return;//or throw an exception, whatever you want
     }

     if(frac->num == 0){
          frac->den = 1;//not technically neccessary, but I like it
          return;
     }

     std::vector<int> num_factors, den_factors;

     num_factors = Factors(frac->num);
     den_factors =  Factors(frac->den);

     if(num_factors.size() > 0 && den_factors.size() > 0){
          frac->num /= num_factors[0];
          frac->den /= den_factors[0];//could use num_factors, but w/e
          ReduceFraction(frac);//try to reduce more
     }

     else{
     return;
     }
}


Top
  Profile  
 
 Post subject:
PostPosted: Sun May 01, 2005 4:47 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
I haven't tested it, but it looks like you may have an error in there Kybo....

Say I wanted to reduce 5/7's. After Factor is called...
Code:
num_factors = Factors(frac->num);  //contains {5}
den_factors =  Factors(frac->den);  //contains {7}

if(num_factors.size() > 0 && den_factors.size() > 0){
    //shouldn't num_factors[0] also have to equal den_factors[0]???
    frac->num /= num_factors[0];   // 5/5 = 1
    frac->den /= den_factors[0];     // 7/7 = 1
    ReduceFraction(frac);  //and now recursion w/ (1/1)
}

If I were doing it, I would have the Factors function return only prime factors, ie. 24 would return {2,2,2,3} not {2,3,4,6,8,12,24}.

If I were reducing 8 and 24, the factor call would return {2,2,2} and {2,2,2,3}. Then remove the intersection {2,2,2} from each list and multiply the remaining factors, respectively, resulting in 1/3.

The advantedge of not reducing to prime factors is that you may save some time by removing a large common factor quickly - assuming you approach the factors from large to small - but at the expensive of having to recompute the factors and doing multiple recursions.


Top
  Profile  
 
 Post subject:
PostPosted: Mon May 02, 2005 3:26 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Ah, good catch. I never tested it, I just wrote it in my post.

To keep the speed and accuracy, I'd test for primality of the number first, return immediately if prime, and then just return a single int from Factors and just use that instead of calculating all of the factors.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group