#include <iostream>
usingnamespace std;
class fraction {
public:
void print();
void set(int, int);
private:
int multipliedBy(int, int); // i'm stuck with here program says too much argument not sure what do i need to put in as argument??
int dividedBy();
int addedTo();
int subtract();
bool isEqualTo();
void fraction::print()
{
cout << first_num << "/" << second_num;
}
void fraction::set(int first_num, int second_num)
{
int dummy;
cin >> first_num;
cin >> dummy;
cin >> second_num;
cin >> dummy;
}
};
int main() // i want to set main as fixed can not be changed
{
fraction f1;
fraction f2;
fraction result;
f1.set(9, 8);
f2.set(2, 3);
cout << "The product of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.multipliedBy(f2);
result.print();
cout << endl;
cout << "The quotient of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.dividedBy(f2);
result.print();
cout << endl;
cout << "The sum of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.addedTo(f2);
result.print();
cout << endl;
cout << "The difference of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.subtract(f2);
result.print();
cout << endl;
if (f1.isEqualTo(f2)) {
cout << "The two fractions are equal." << endl;
}
else {
cout << "The two fractions are not equal." << endl;
}
}
/*
result should looks like this
The product of 9/8 and 2/3 is 18/24
The quotient of 9/8 and 2/3 is 27/16
The sum of 9/8 and 2/3 is 43/24
The difference of 9/8 and 2/3 is 11/24
The two fractions are not equal.
*/
#include <iostream>
usingnamespace std;
class fraction
{ int numerator; // Added member variables
int denominator;
public:
fraction () // Added default constructor
{ numerator = 0;
denominator = 0;
}
fraction (int first, int second) // Added value constructor
{ numerator = first;
denominator = second;
}
fraction multipliedBy (const fraction & f) // The argument and return type should be a fraction
{ return fraction (this->numerator * f.numerator, this->denominator * f.denominator);
}
fraction dividedBy (const fraction & f); // TODO
fraction addedTo (const fraction & f); // TODO
fraction subtract (const fraction & f); // TODO
bool isEqualTo (const fraction & f); // TODO
friend ostream & operator << (ostream & os, const fraction & frac) // Change print to overload the >> operator
{ os << frac.numerator << "/" << frac.denominator;
return os;
}
};
int main() // i want to set main as fixed can not be changed
{ fraction f1 (9,8);
fraction f2 (2,3);
fraction result;
cout << "The product of " << f1 << " and " << f2 << " is: ";
result = f1.multipliedBy(f2);
cout << result << endl;
cout << "The quotient of " << f1 << " and " << f2 << " is: ";
result = f1.dividedBy(f2);
cout << result << endl;
cout << "The sum of " << f1 << " and " << f2 << " is: ";
result = f1.addedTo(f2);
cout << result << endl;
cout << "The difference of " << f1 << " and " << f2 << " is: ";
result = f1.subtract(f2);
cout << result << endl;
if (f1.isEqualTo(f2))
{ cout << "The two fractions are equal." << endl;
}
else
{ cout << "The two fractions are not equal." << endl;
}
system ("pause");
}