#include <iostream> #include <iomanip> #include \"fractionType.h\" using namespa
ID: 3699611 • Letter: #
Question
#include <iostream> #include <iomanip> #include "fractionType.h"using namespace std;
int main() { fractionType num1(5, 6); //Line 1 fractionType num2; //Line 2 fractionType num3; //Line 3
cout << fixed; //Line 4 cout << showpoint; //Line 5 cout << setprecision(2); //Line 6
cout << "Line 7: Num1 = " << num1 << endl; //Line 7 cout << "Line 8: Num2 = " << num2 << endl; //Line 8
cout << "Line 9: Enter the fraction " << "in the form a / b: "; //Line 9 cin >> num2; //Line 10 cout << endl; //Line 11
cout << "Line 12: New value of num2 = " << num2 << endl; //Line 12
num3 = num1 + num2; //Line 13
cout << "Line 14: Num3 = " << num3 << endl; //Line 14 cout << "Line 15: " << num1 << " + " << num2 << " = " << num1 + num2 << endl; //Line 15 cout << "Line 16: " << num1 << " * " << num2 << " = " << num1 * num2 << endl; //Line 16
num3 = num1 - num2; //Line 17
cout << "Line 18: Num3 = " << num3 << endl; //Line 18 cout << "Line 19: " << num1 << " - " << num2 << " = " << num1 - num2 << endl; //Line 19 cout << "Line 20: (" << num1 << ") / (" << num2 << ") = " << num1 / num2 << endl; //Line 20 cout << "Line 21: (" << num1 << ") <= (" << num2 << ") = " << (num1 <= num2) << endl; //Line 21 cout << "Line 22: (" << num1 << ") == (" << num2 << ") = " << (num1 == num2) << endl ; //Line 22 cout << "Line 23: (" << num1 << ") >= (" << num2 << ") = " << (num1 >= num2) << endl ; //Line 23 cout << "Line 24: (" << num1 << ") != (" << num2 << ") = " << (num1 != num2) << endl ; //Line 24 cout << "Line 25: (" << num1 << ") > (" << num2 << ") = " << (num1 > num2) << endl; //Line 25 cout << "Line 26: (" << num1 << ") < (" << num2 << ") = " << (num1 < num2) << endl; //Line 26 return 0; } #include <iostream> #include <iomanip> #include "fractionType.h"
using namespace std;
int main() { fractionType num1(5, 6); //Line 1 fractionType num2; //Line 2 fractionType num3; //Line 3
cout << fixed; //Line 4 cout << showpoint; //Line 5 cout << setprecision(2); //Line 6
cout << "Line 7: Num1 = " << num1 << endl; //Line 7 cout << "Line 8: Num2 = " << num2 << endl; //Line 8
cout << "Line 9: Enter the fraction " << "in the form a / b: "; //Line 9 cin >> num2; //Line 10 cout << endl; //Line 11
cout << "Line 12: New value of num2 = " << num2 << endl; //Line 12
num3 = num1 + num2; //Line 13
cout << "Line 14: Num3 = " << num3 << endl; //Line 14 cout << "Line 15: " << num1 << " + " << num2 << " = " << num1 + num2 << endl; //Line 15 cout << "Line 16: " << num1 << " * " << num2 << " = " << num1 * num2 << endl; //Line 16
num3 = num1 - num2; //Line 17
cout << "Line 18: Num3 = " << num3 << endl; //Line 18 cout << "Line 19: " << num1 << " - " << num2 << " = " << num1 - num2 << endl; //Line 19 cout << "Line 20: (" << num1 << ") / (" << num2 << ") = " << num1 / num2 << endl; //Line 20 cout << "Line 21: (" << num1 << ") <= (" << num2 << ") = " << (num1 <= num2) << endl; //Line 21 cout << "Line 22: (" << num1 << ") == (" << num2 << ") = " << (num1 == num2) << endl ; //Line 22 cout << "Line 23: (" << num1 << ") >= (" << num2 << ") = " << (num1 >= num2) << endl ; //Line 23 cout << "Line 24: (" << num1 << ") != (" << num2 << ") = " << (num1 != num2) << endl ; //Line 24 cout << "Line 25: (" << num1 << ") > (" << num2 << ") = " << (num1 > num2) << endl; //Line 25 cout << "Line 26: (" << num1 << ") < (" << num2 << ") = " << (num1 < num2) << endl; //Line 26 return 0; } Problem Description: (30 marks) Task-1: Rational fractions are of the form a /b, in which a and b are integers and b 0. In this exercise, by fractions" we mean rational fractions. Suppose a/b andc/dare fractions. Arithmetic operations and relational operations on fractions are defined by the following rules: Arithmetic Operations: a/b tcld-(ad+bc)/bd (a/b)/(c/d)-adbe, in which cel #0 Relational Operations: (abs-cia)- (ad-be) (ab-c/d) = (ad-bc) Fractions are compared as follows: a/b op c / d if ad op bc, in which op is any of the relational operations. For example, a/b
Explanation / Answer
here is your fractionType.h file : ------------>>>>>>>>>>
#include<iostream>
#include<sstream>
using namespace std;
class fractionType
{
private:
int numerator;
int denominator;
public:
fractionType(){
numerator = 0;
denominator = 1;
}
fractionType(int num){
numerator = num;
denominator = 1;
}
fractionType(int num, int den){
numerator = num;
if(den > 0){
denominator = den;
}
}
const fractionType operator+(const fractionType &other) const{
int num = (numerator*other.denominator + denominator*other.numerator );
int den = (denominator * other.denominator);
fractionType temp(num,den);
return temp;
}
const fractionType operator-(const fractionType &other) const{
int num = (numerator*other.denominator - denominator*other.numerator );
int den = (denominator * other.denominator);
fractionType temp(num,den);
return temp;
}
const fractionType operator*(const fractionType &other) const{
int num = (numerator*other.numerator );
int den = (denominator * other.denominator);
fractionType temp(num,den);
return temp;
}
const fractionType operator/(const fractionType &other) const{
int num = (numerator*other.denominator);
int den = (denominator * other.numerator);
fractionType temp(num,den);
return temp;
}
friend ostream& operator<<(ostream &out,const fractionType &oth){
out<<"("<<oth.numerator<<"/"<<oth.denominator<<")";
return out;
}
string toString(){
return numerator+"/"+denominator;
}
double toDouble(){
return (double)(numerator/denominator);
}
void simplify(){
int m = gcd(numerator,denominator);
numerator = numerator/m;
denominator = denominator/m;
}
bool operator!=(fractionType &other){
int ad = numerator*other.denominator;
int bc = denominator*other.denominator;
return ad != bc;
}
bool operator==(fractionType &other){
int ad = numerator*other.denominator;
int bc = denominator*other.denominator;
return ad == bc;
}
bool operator>=(fractionType &other){
int ad = numerator*other.denominator;
int bc = denominator*other.denominator;
return ad >= bc;
}
bool operator<=(fractionType &other){
int ad = numerator*other.denominator;
int bc = denominator*other.denominator;
return ad <= bc;
}
bool operator>(fractionType &other){
int ad = numerator*other.denominator;
int bc = denominator*other.denominator;
return ad > bc;
}
bool operator<(fractionType &other){
int ad = numerator*other.denominator;
int bc = denominator*other.denominator;
return ad < bc;
}
friend istream& operator>>(istream &in,fractionType &oth){
string s1;
getline(in,s1);
stringstream ss;
for(int i = 0;i<s1.length();i++){
if(s1[i] == '/'){
ss<<" ";
}
else{
ss<<s1[i];
}
}
ss>>oth.numerator>>oth.denominator;
return in;
}
private:
int gcd(int a, int b){
int m,n;
m=a;
n=b;
while(m!=n)
{
if(m>n)
m=m-n;
else
n=n-m;
}
return m;
}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.