Hi I'm new to coding. Can someone please help with a linked list problem. I'm getting numbers for output, I think they are addresses. The output I'm getting is:
HEAD CODE
-12086434400
1080131584
TAIL CODE
The output I need is:
HEAD CODE
<rect x="0" y="0" width ="200" height="200" stroke-width="2" stroke="red" fill="none" />
<rect x="14" y="140" width ="140" height="14" stroke-width="2" stroke="red" fill="none" />
TAIL CODE
I know it has something to do with the switch. I know that code is right but I'm not utilising it.
Thanks
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
class Path
{
public:
//stub
}; // Path
class Circle
{
public:
//stub
}; // Circle
class Rectangle
{
public:
Rectangle();
Rectangle(double x, double y, double w, double h, string c = "red", string f = "none");
void Print(ostream & out) const;
private:
double rx, ry, width, height;
string colour, fill;
};
ostream & operator<<(ostream & out, const Rectangle & rectangle);
class DataType
{
public:
enum { EMPTY, PATH, CIRCLE, RECTANGLE } SVGObj;
union
{
Path *path;
Circle *circle;
Rectangle *rectangle;
};
DataType();
DataType(const DataType &);
DataType(const Circle & obj);
DataType(const Path & obj);
DataType(const Rectangle & obj);
};
struct LNode
{
DataType data;
LNode *next;
};
typedef LNode* LNodePtr;
class Svg
{
public:
Svg(double w, double h);
~Svg(); //destructor
void Head(ostream & out) const;
void Tail(ostream & out) const;
void Print(ostream & out) const;
void Append(const Path & obj);
void Append(const Circle & obj);
void Append(const Rectangle & obj);
friend ostream & operator<<(ostream & out,const Svg & aList);
private:
LNodePtr head;
int size; //size of list
double myWidth;
double myHeight;
};
Rectangle::Rectangle(double x, double y, double w, double h, string c, string f)
{
rx = x;
ry = y;
width = w;
height = h;
colour = c;
fill = f;
} // Rectangle
void Rectangle::Print(ostream & out) const
{
out << "\n<rect x=\"" << rx << "\" y=\"" << ry << "\" width=\"" << width << "\" height=\"" << height
<< "\" stroke-width=\"" << 2 << "\" stroke=\"" << colour << "\" fill=\"" << fill << "\" />" << endl;
} // Rectangle Print
ostream & operator<<(ostream & out, const Rectangle & rectangle)
{
rectangle.Print( out );
return out;
} // Rectangle operator<<
// CONSTRUCTOR
DataType::DataType()
{
SVGObj = EMPTY;
}
DataType::DataType(const DataType & orig)
{
switch(orig.SVGObj)
{
case PATH:
path = new Path (*orig.path);
assert(path != NULL);
break;
case CIRCLE:
circle = new Circle (*orig.circle);
assert(circle != NULL);
break;
case RECTANGLE:
rectangle = new Rectangle (*orig.rectangle);
assert(rectangle != NULL);
break;
case EMPTY:
break;
}
SVGObj = orig.SVGObj;
}
DataType::DataType(const Rectangle & obj)
{
cout << obj; // this is wrong but I can't fix it
}
ostream & operator<<(ostream& out, const DataType& aDataType)
{
out << aDataType.SVGObj ;
return out ;
}
Svg::Svg(double w, double h)
{
head = NULL;
size = 0;
myWidth = w;
myHeight = h;
}
//destructor
Svg::~Svg()
{
LNodePtr temp;
while (head != NULL)
{
temp = head;
head = head-> next;
delete temp;
}
size = 0;
}
void Svg::Head(ostream & out) const
{
out << "HEAD CODE" << endl;
} // sample data
// XML CLOSING TAG
void Svg::Tail(ostream & out) const
{
out << "TAIL CODE" << endl;
} // sample data
void Svg::Append(const Rectangle & obj)
{
LNodePtr temp;
temp = new LNode;
if (temp == 0)
{
cerr <<"\n***No more memory!\n";
exit(1);
}
temp->data = obj;
// temp->next = NULL;
if (head == NULL) //empty list
head = temp;
else //at least one node
{
LNodePtr current = head;
//traverse list
while (current->next != NULL)
current = current->next;
//insert node at end of list
current->next = temp;
}
}
ostream & operator<<(ostream & out, const Svg & aList)
{
LNodePtr temp = aList.head;
aList.Head(out);
while(temp != NULL)
{
out << temp->data << endl;
temp = temp->next;
}
aList.Tail(out);
return out;
}
void Page( Svg & graph, double left, double bottom, double right, double top );
int main()
{
Svg pattern( 200, 200 ); // initialize Svg object
Page( pattern, 0, 0, 200, 200 );
cout << pattern;
return 0;
} // main
void Page( Svg & graph, double left, double bottom, double right, double top )
{
Rectangle rectangle(left, bottom, right, top ); // create the middle rectangle
graph.Append(rectangle);
Rectangle rectangle2(14, 140, 140, 14);
graph.Append(rectangle2);
}