1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
main
|
#include <iostream>
#include <fstream>
#include "dlist.h"
#include "swatches.h"
using namespace std;
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 ( (red>=green) && (red>=blue) ) // red is dominant
{
swatches.front_insert(tmp);
}
else if ( (green >= red) && (green >= blue) )
// green is dominant
{
swatches.rear_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){
//if(swatches.size()>2){
//it++;
//}
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;
}
return 0;
}
swatches.cc----------------------------------------------
#include "swatches.h"
#include <iomanip>
#include <cstdlib>
#include <cstdio>
using namespace std;
// 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;
}
std::ostream& operator <<(std::ostream& outs,const Swatch& sw){
char a_c_string[10];
sprintf(a_c_string,"%x",sw.color);
std::string tmp(a_c_string);
int leading = 6 - tmp.length();
if(leading <= 0) outs<<tmp;
else{
std::string padding;
for(int i=0; i<leading; ++i){
padding += '0';
}
padding += tmp;
outs<<padding;
}
outs<<" "<<sw.width<<" "<<sw.length;
return outs;
}
std::istream& operator >>(std::istream& ins, Swatch& sw){
ins>>hex>>sw.color>>dec>>sw.width>>sw.length;
sw.red = sw.color/sw.TWO_COLOR;
sw.blue = sw.color%sw.ONE_COLOR;
sw.green = (sw.color/sw.ONE_COLOR)%sw.ONE_COLOR;
return ins;
}
swatches.h------------------------------
#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
|