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:

oh my god. I have seen some pretty badly formatted posts, but this surely takes the cake for the worst. I am sure nobody will take a second glance at this post unless you use

tags and re-post properly formatted code.

If you're allowed to use the STL list class this assignment should be relatively straightforward.

#include<list>
#include<string>
#include<iostream>

std::list<std::string> listOfStrings

will get you a list of std::strings

std::list<int> listOfInts

will get you a list of ints, etc. Displaying the information stored in each node/link of the list requires that you loop through the list from front to back and then call the << operator on each nodes data member.

If you have to write your own templated list class, then you have some interesting challenges ahead of you, particularly if you've never written your own list class before. Post your restrictions and requirements as well as the code you come up with so we can assist you. We won't do it for you.

This article has been dead for over six months. Start a new discussion instead.