The code below is what I have done up so far.
I'm currently trying to complete one of the 4 classes I have, so the codes are incomplete and made to work at it's current stage.
Q1.
I need to have 3 different sorting criteria:
1.Sort by X (asc/desc)
2.Sort by Y (asc/desc)
3.Sort by DistFrOrigin (asc/desc)
How can I sort them?
I tried using sort( v.begin(), v.end() ) and resulted in a long list of error.
Q2.
On a side note, is the way I'm doing actually Logical?
Or there's a better way to do it?
Please advise.
Thanks in advance!
Point2D.h
class Point2D
{
private:
protected:
//Declarations
int x;
int y;
double distFrOrigin;
//Function
void setDistFrOrigin();
public:
//Constructor
Point2D();
Point2D(int,int);
//Set Methods
void setX(int);
void setY(int);
//Get Methods
int getX();
int getY();
double getScalarValue();
void printPoint2D();
};
Point2D.cpp
//Constructor
Point2D::Point2D()
{
x=0;
y=0;
}
Point2D::Point2D(int x, int y)
{
this->x=x;
this->y=y;
setDistFrOrigin();
distFrOrigin = getScalarValue();
}
//Set Method
void Point2D::setX(int x)
{
this->x=x;
setDistFrOrigin();
}
void Point2D::setY(int y)
{
this->y=y;
setDistFrOrigin();
}
//Get Method
int Point2D::getX()
{
return x;
}
int Point2D::getY()
{
return y;
}
double Point2D::getScalarValue()
{
return distFrOrigin;
}
//Function
void Point2D::setDistFrOrigin()
{
distFrOrigin = sqrt( pow(x,2) + pow(y,2) );
}
void Point2D::printPoint2D()
{
cout << x << y << distFrOrigin << endl;
}
Main file.
string lines;
vector<Point2D> v;
ifstream infile("myfile.txt");
while(!getline(infile,lines,',').eof())
{
if(lines=="Point2D")
{
int x, y, z;
getline(infile,lines,' ');
getline(infile,lines,'[');
getline(infile,lines,',');
stringstream s1(lines);
s1 >> x;
getline(infile,lines,' ');
getline(infile,lines,']');
stringstream s2(lines);
s2 >> y;
Point2D p(x,y);
v.push_back(p);
getline(infile,lines);
}
else if(lines=="Point3D")
{
getline(infile,lines);
}
else if(lines=="Line2D")
{
getline(infile,lines);
}
else if(lines=="Line3D")
{
getline(infile,lines);
}
}
infile.close();
vector<Point2D>::iterator it = v.begin();
while(it != v.end())
{
it->printPoint2D();
it++;
}
mytext.txt
Point2D, [-9, -9]
Line3D, [7, 12, 3], [-9, 13, 68]
Point3D, [1, 3, 8]
Line2D, [5, 7], [3, 8]
Point2D, [3, 3]
Line3D, [7, -12, 3], [9, 13, 68]
Point3D, [6, 9, 5]
Point2D, [23, 23]
Line3D, [70, -120, -3], [-29, 1, 268]
Line3D, [25, -69, -33], [-2, -41, 58]
Point3D, [6, 9, -50]