Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //box.h
- #ifndef BOX_H
- #define BOX_H
- template <typename DataType>
- struct box
- {
- DataType data;
- box<DataType> *next;
- box(DataType, box<DataType>*);
- };
- #endif
- //box.cpp
- #include "box.h"
- template <typename DataType>
- box<DataType>::box(DataType d, box<DataType> *n)
- {
- data = d;
- next = n;
- }
- //LList.h
- #ifndef LLIST_H
- #define LLIST_H
- #include "box.h"
- template <typename DataType>
- class LList
- {
- public:
- box<DataType> *first;
- LList ();
- LList (const LList&);
- ~LList();
- void push (DataType);
- void pop ();
- void removeAll();
- void copy (const LList<DataType>&);
- void insertAt (DataType, int);
- void removeAt (int);
- box<DataType> *findElementAt (int);
- LList<DataType>& operator = (const LList<DataType>&);
- DataType& operator[] (int);
- bool operator == (LList<DataType>);
- };
- #endif
- //LList.cpp
- #include <assert.h>
- #include <cstring>
- #include "box.h"
- #include "LList.h"
- template <typename DataType>
- void LList<DataType>::removeAll ()
- {
- while (first != NULL)
- pop();
- }
- template <typename DataType>
- LList<DataType>::~LList()
- {
- removeAll ();
- }
- template <typename DataType>
- LList<DataType>& LList<DataType>::operator = (const LList<DataType> &l)
- {
- if (&l != this)
- {
- this->removeAll();
- this->copy (l);
- }
- return *this;
- }
- template <typename DataType>
- void LList<DataType>::copy (const LList<DataType> &l)
- {
- box<DataType> *otherCrr = l.first;
- int i =0;
- while (otherCrr != NULL)
- {
- this->insertAt (otherCrr->data,i);
- otherCrr = otherCrr->next;
- i++;
- }
- }
- template <typename DataType>
- LList<DataType>::LList (const LList &l)
- {
- this->first = NULL;
- copy (l);
- }
- template <typename DataType>
- LList<DataType>::LList ():first(NULL) {}//{first = NULL;}
- template <typename DataType>
- void LList<DataType>::push (DataType newData)
- {
- box<DataType> *newBox = new box<DataType> (newData,NULL);
- newBox->next = first;
- first = newBox;
- }
- template <typename DataType>
- void LList<DataType>::insertAt (DataType newData, int ix)
- {
- if (ix == 0)
- {
- push (newData);
- return;
- }
- box<DataType> *crr = findElementAt (ix-1);
- assert (crr != NULL);
- box<DataType> *newBox = new box<DataType> (newData,crr->next);
- crr->next = newBox;
- }
- template <typename DataType>
- box<DataType>* LList<DataType>::findElementAt (int ix)
- {
- box<DataType> *crr = first;
- for (int i = 0; i < ix && crr != NULL; i++)
- crr = crr->next;
- return crr;
- }
- template <typename DataType>
- void LList<DataType>::pop ()
- {
- assert (first != NULL);
- box<DataType> *save = first;
- first = first->next;
- delete save;
- }
- template <typename DataType>
- void LList<DataType>::removeAt ( int ix)
- {
- if (ix == 0)
- {
- pop();
- return;
- }
- box<DataType> *crr = findElementAt (ix-1);
- assert (crr != NULL);
- assert (crr->next != NULL);
- box<DataType> *save = crr->next;
- crr->next = crr->next->next;
- delete save;
- }
- template <typename DataType>
- DataType& LList<DataType>::operator[] (int index)
- {
- box<DataType> *crr = first;
- int count = 0;
- while (crr != NULL && count != index)
- {
- crr = crr->next;
- count++;
- }
- assert (crr != NULL);
- return crr->data;
- }
- template <typename DataType>
- bool LList<DataType>::operator == (LList<DataType> other)
- {
- box<DataType> *crr1 = first;
- box<DataType> *crr2 = other.first;
- while (crr1 != NULL && crr2 != NULL)
- {
- if (crr1->data != crr2->data)
- return false;
- crr1 = crr1->next;
- crr2 = crr2->next;
- }
- return crr1 == NULL && crr2 == NULL;
- }
- //Song.h
- #ifndef SONG_H
- #define SONG_H
- class Song
- {
- private:
- char* name;
- char* artist;
- int yearPublished;
- int length;
- Song* original;
- public:
- Song();
- Song(const Song&);
- Song(char*, char*, int, int, Song*);
- ~Song();
- Song& operator=(const Song&);
- void setName(const char*);
- char* getName() const;
- void setArtist(const char*);
- char* getArtist() const;
- void setYearPublished(const int);
- int getYearPublished() const;
- void setLength(const int);
- int getLength() const;
- void setOriginal(Song*);
- Song* getOriginal() const;
- void print() const;
- };
- #endif
- //Song.cpp
- #include <iostream>
- #include <cstring>
- #include "Song.h"
- Song::Song()
- {
- name = new char[100];
- artist = new char[100];
- original = NULL;
- }
- Song::Song(const Song& other)
- {
- name = new char[100];
- artist = new char[100];
- *this = other;
- }
- Song::Song(char* _name, char* _artist, int _yearPublished, int _length, Song* _original)
- {
- name = new char[100];
- artist = new char[100];
- setName(_name);
- setArtist(_artist);
- setYearPublished(_yearPublished);
- setLength(_length);
- setOriginal(_original);
- }
- Song::~Song()
- {
- delete[] name;
- delete[] artist;
- delete original;
- }
- Song& Song::operator=(const Song &other)
- {
- setName(other.getName());
- setArtist(other.getArtist());
- setYearPublished(other.getYearPublished());
- setLength(other.getLength());
- setOriginal(other.getOriginal());
- return *this;
- }
- void Song::setName(const char* _name)
- {
- delete[] name;
- name = new char[strlen(_name)+1];
- strcpy(name, _name);
- }
- char* Song::getName() const
- {
- return name;
- }
- void Song::setArtist(const char* _artist)
- {
- delete[] artist;
- artist = new char[strlen(_artist)+1];
- strcpy(artist, _artist);
- }
- char* Song::getArtist() const
- {
- return artist;
- }
- void Song::setYearPublished(const int _yearPublished)
- {
- yearPublished = _yearPublished;
- }
- int Song::getYearPublished() const
- {
- return yearPublished;
- }
- void Song::setLength(const int _length)
- {
- length = _length;
- }
- int Song::getLength() const
- {
- return length;
- }
- void Song::setOriginal(Song* _original)
- {
- original = _original;
- }
- Song* Song::getOriginal() const
- {
- return original;
- }
- void Song::print() const
- {
- std::cout<<"Name: "<<name<<"\nArtist: "<<artist<<"\nYear Published: "<<yearPublished<<"\nLength: "<<length<<"\n\n";
- }
- //Album.h
- #ifndef ALBUM_H
- #define ALBUM_H
- #include "LList.h"
- class Album
- {
- private:
- LList<Song> songs;
- char* name;
- public:
- Album();
- Album(const Album&);
- ~Album();
- Album& operator=(const Album&);
- void setSongs(LList<Song>);
- LList<Song> getSongs() const;
- void setName(char*);
- char* getName() const;
- void addSong(Song);
- void print();
- int getLength();
- char* getArtist();
- Song* findSong(char*);
- //void deleteDuplicates();
- //void detectCovers();
- };
- #endif
- //Album.cpp
- #include <iostream>
- #include <cstring>
- #include "Song.h"
- #include "Album.h"
- #include "LList.h"
- Album::Album()
- {
- name = new char[100];
- }
- Album::Album(const Album& other)
- {
- songs = other.getSongs();
- name = new char[strlen(other.getName())+1];
- strcpy(name, other.getName());
- }
- Album::~Album()
- {
- delete[] name;
- }
- Album& Album::operator=(const Album& other)
- {
- setName(other.getName());
- songs = other.getSongs();
- return *this;
- }
- void Album::setName(char* _name)
- {
- delete[] name;
- name = new char[strlen(_name)+1];
- strcpy(name, _name);
- }
- char* Album::getName() const
- {
- return name;
- }
- void Album::setSongs(LList<Song> _songs)
- {
- songs = _songs;
- }
- LList<Song> Album::getSongs() const
- {
- return songs;
- }
- void Album::addSong(Song song)
- {
- songs.push(song);
- }
- void Album::print()
- {
- std::cout<<"Name: "<<name<<"\nArtist: "<<getArtist()<<"\nSongs:\n\n";
- for(box<Song>* i = songs.first; i!=NULL; i=i->next)
- {
- i->data.print();
- }
- std::cout<<"Album length: "<<getLength()<<'\n';
- }
- int Album::getLength()
- {
- int length = 0;
- for(box<Song>* i = songs.first; i!=NULL; i=i->next)
- {
- length+=i->data.getLength();
- }
- return length;
- }
- char* Album::getArtist()
- {
- char* artist = songs.first->data.getArtist();
- for(box<Song>* i = songs.first; i!=NULL; i=i->next)
- {
- if(strcmp(artist, i->data.getArtist()) != 0) return "Various artists";
- }
- return artist;
- }
- Song* Album::findSong(char* songName)
- {
- for(box<Song>* i = songs.first; i!=NULL; i=i->next)
- {
- if(strcmp(songName, i->data.getName()) == 0) return &i->data;
- }
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement