Hello, i'm trying to write in the main program a linked list of namesfractions, and integres. i don't know if anyone can help me in this. i attached the fraction class and the implementationUsing the linked list class we derived in class, and the following functionality:
- A print function (you do not need to overload the << operator) that will print out the entire list.
- A read function (not the >> operator) that will read all the data from a file (or the keyboard) into the list.
- A search function tha will search the entire list for some object.
To demonstrate the flexibility of templates, your main program should have a linked list of names, a linked list of fractions and a linked list of integers. You can use the fractions class
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#pragma once
class rationalClass
{
public:
rationalClass(int newN=0, int newD=1);
int getNumerator()const;
int getDenominator()const;
friend rationalClass add(const rationalClass &left, const rationalClass &right)
;
friend rationalClass operator +(const rationalClass &left, const rationalClass &right);
friend rationalClass operator -(const rationalClass &left, const rationalClass &right);
friend void print(const rationalClass &fraction, ostream & out=cout);
friend ostream & operator <<(ostream &out, const rationalClass &right);
friend istream & operator >>(istream &in, rationalClass &right);
friend bool operator <(const rationalClass &left, const rationalClass &right);
friend bool operator >(const rationalClass &left, const rationalClass &right);
friend bool operator <=(const rationalClass &left, const rationalClass &right);
friend bool operator >=(const rationalClass &left, const rationalClass &right);
friend bool operator ==(const rationalClass &left, const rationalClass &right);
friend bool operator !=(const rationalClass &left, const rationalClass &right);
private:
friend int gcd(int a, int b);
int numerator, denominator;
}
;
#include "fractions.h"
int rationalClass::getNumerator()const
{
return numerator;
}
int rationalClass::getDenominator()const
{
return denominator;
}
rationalClass::rationalClass(int newN, int newD)
{
if(newD==0)
{
cerr << "Invalid denominator\n";
exit(12);
}
int divisor=gcd(newN, newD);
numerator=newN/divisor;
denominator=newD/divisor;
}
int gcd(int a, int b)
{
if(a<0)
a=-a;
if(b<0)
b=-b;
int x=a,y=b;
while(y != 0)
{
int r=x%y;
x=y;
y=r;
}
return x;
}
rationalClass add(const rationalClass &left, const rationalClass &right)
{
return rationalClass(left.numerator*right.denominator + right.numerator*left.denominator, left.denominator*right.denominator);
}
rationalClass operator +(const rationalClass &left, const rationalClass &right)
{
return rationalClass(left.numerator*right.denominator + right.numerator*left.denominator, left.denominator*right.denominator);
}
rationalClass operator -(const rationalClass &left, const rationalClass &right)
{
return rationalClass(left.numerator*right.denominator - right.numerator*left.denominator, left.denominator*right.denominator);
}
void print(const rationalClass &f1, ostream & out)
{
out << f1.numerator << "/" << f1.denominator << endl;
}
ostream & operator <<(ostream & out, const rationalClass &f1)
{
out << f1.numerator << "/" << f1.denominator;
return out;
}
istream & operator >>(istream &in, rationalClass &right)
{
char tmp;
int n,d;
in >> n>> tmp >> d;
right=rationalClass(n,d);
return in;
}
bool operator <(const rationalClass &left, const rationalClass &right)
{
return (left.numerator*right.denominator < right.numerator*left.denominator);
}
bool operator <=(const rationalClass &left, const rationalClass &right)
{
return (left.numerator*right.denominator <= right.numerator*left.denominator);
}
bool operator >(const rationalClass &left, const rationalClass &right)
{
return (left.numerator*right.denominator > right.numerator*left.denominator);
}
bool operator >=(const rationalClass &left, const rationalClass &right)
{
return (left.numerator*right.denominator >= right.numerator*left.denominator);
}
bool operator ==(const rationalClass &left, const rationalClass &right)
{
return (left.numerator*right.denominator == right.numerator*left.denominator);
}
bool operator !=(const rationalClass &left, const rationalClass &right)
{
return (left.numerator*right.denominator != right.numerator*left.denominator);
}
thanks:sad: