int main()
{
dlist<Swatch> swatches;
dlist<Swatch>::iterator it;
ifstream fin;
fin.open("swatches.txt");
if (fin.fail())
{
cout << "Could not open input file." << endl;
return 1;
}
Swatch tmp;
while (fin >> tmp)
{
int red = tmp.get_red();
int green = tmp.get_green();
int blue = tmp.get_blue();
if ( (green >= red) && (green >= blue) ) // green is dominant
{
swatches.rear_insert(tmp);
}
else if ( (red >= green) && (red>=blue) )
// red is dominant
{
swatches.front_insert(tmp);
}
else // blue is dominant
{
it = swatches.begin();
for(int i = 0;i < swatches.size()/2;i++)
++it; // loop moves iterator to the middle
if(swatches.size()%2 == 1){
swatches.insert_before(it,tmp);
}
else{
swatches.insert_after(it,tmp);
}
}
}
fin.close();
dlist<Swatch> copy(swatches); // make a copy
// remove the front, back, and centermost swatch from the copy
copy.front_remove();
copy.rear_remove();
it = copy.begin();
for(int i =0; i < copy.size()/2; ++i)
++it;
//if(copy.size()%2 ==1) ++it; // if list has a true middle
// step up into it
copy.remove(it);
// output the original list frontwards
for (dlist<Swatch>::iterator i=swatches.begin(); i != swatches.end(); ++i)
{
cout << *i << endl;
}
cout << endl << endl; // some space
// output the copy frontwards
for (dlist<Swatch>::iterator i=copy.begin(); i != copy.end(); ++i)
{
cout << *i << endl;
}
cout << endl << endl; // some space
// output the original backwards
for (dlist<Swatch>::iterator i=swatches.r_begin(); i != swatches.r_end(); --i)
{
cout << *i << endl;
}
cout << endl << endl; // some space
// destroy the original list by alternating between removal of first and
// last items. Print each item as it is removed
int counter=0;
while (swatches.size() > 0)
{
cout<<*swatches.begin()<<endl;
swatches.front_remove();
if(swatches.size() > 0){
cout<<*swatches.r_begin()<<endl;
swatches.rear_remove();
}
}
cout << endl << endl; // some space
// output the copy backwards
for (dlist<Swatch>::iterator i=copy.r_begin(); i != copy.r_end(); --i)
{
cout << *i << endl;
}
// CONSTRUCTORS
Swatch::Swatch(){
color=0;
red=green=blue=0;
width=0;
length=0;
}
Swatch::Swatch(unsigned long n_color, int n_width, int n_length){
color=n_color;
red = n_color/TWO_COLOR;
blue = n_color%ONE_COLOR;
green = (n_color/ONE_COLOR)%ONE_COLOR;
width=n_width;
length=n_length;
}
// ACCESSOR FUNCTIONS
//This function returns the color as a number which is how it is
//actually stored
unsigned long Swatch::get_color(){
return color;
}
//This function returns a string version of the color number in standard
// RGB format
std::string Swatch::color_string(){
char a_c_string[10];
sprintf(a_c_string,"%x",color);
std::string tmp(a_c_string);
int leading = 6 - tmp.length();
if(leading <= 0) return tmp;
else{
std::string padding;
for(int i=0; i<leading; ++i){
padding += '0';
}
padding += tmp;
return padding;
}
}
//Return amount of red in swatch
unsigned long Swatch::get_red(){
return red;
}
//Return amount of green in a swatch
unsigned long Swatch::get_green(){
return green;
}
//Return amount of blue in a swatch
unsigned long Swatch::get_blue(){
return blue;
}
// Return width of the swatch
int Swatch::get_width(){
return width;
}
// Returns length of the swatch
int Swatch::get_length(){
return length;
}
// MODIFICATION FUNCTIONS
// Sets color to value of the argument
void Swatch::set_color(unsigned long n_color){
color = n_color;
red = n_color/TWO_COLOR;
blue = n_color%ONE_COLOR;
green = (n_color/ONE_COLOR)%ONE_COLOR;
}
// Sets color to value created when string is converted to a hex number
void Swatch::set_color(std::string n_color){
//The strol function will work with any base from 2 to 36.
//The 16 here denotes that we are converting from a
//hexadecimal representation.
color = strtol(n_color.c_str(), (char **)NULL, 16);
red = color/TWO_COLOR;
blue = color%ONE_COLOR;
green = (color/ONE_COLOR)%ONE_COLOR;
}
// Sets width to value of the argument
void Swatch::set_width(int n_width){
width=n_width;
}
// Sets length to value of argument
void Swatch::set_length(int n_length){
length=n_length;
}
return ins;
}
swatches.h
/**************************************************************************
The header file for a class called Swatches, which stores
the color and dimensions of a color swatch. The color is stored
as a single hexadecimal number which is actually a composite of
three hex numbers representing the intensity of Red, Green and
Blue respecitively. The dimensions are intergers for width and
height which can be interpreted into whatever unit is relevant
for the application.
A set of accessor and modification functions are provided for
for the private variables, and the color is available as either
a number a string.
************************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
#ifndef SWATCH_H
#define SWATCH_H
class Swatch{
public:
static const unsigned long ONE_COLOR = 256;
static const unsigned long TWO_COLOR = 256*256;
// CONSTRUCTORS
Swatch();
Swatch(unsigned long n_color, int n_width, int n_height);
// ACCESSOR FUNCTIONS
unsigned long get_color();
std::string color_string();
unsigned long get_red();
unsigned long get_green();
unsigned long get_blue();
int get_width();
int get_length();
// MODIFICATION FUNCTIONS
void set_color(unsigned long n_color);
void set_color(std::string n_color);
void set_width(int n_width);
void set_length(int n_length);
// FRIENDS
friend std::ostream& operator <<(std::ostream& outs, const Swatch& sw);
friend std::istream& operator >>(std::istream& ins, Swatch& sw);
private:
unsigned long color;
unsigned long red, green, blue;
int width;
int length;
};
#endif
front_remove() and rear_remove() don't do the right thing when the list has one item. Then need to set head and tail to nullptr. Also, you need to delete the removed node to avoid leaking memory.