本文整理汇总了C++中Point2D::returnType方法的典型用法代码示例。如果您正苦于以下问题:C++ Point2D::returnType方法的具体用法?C++ Point2D::returnType怎么用?C++ Point2D::returnType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Point2D
的用法示例。
在下文中一共展示了Point2D::returnType方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
vector <Point2D*> points;
int num_select;
double x;
double y;
double wt;
string col;
//Point2D* a;
//Point2D* b;
cout << "Welcome to Point Printer! You can create three different kinds of points: " << endl;
cout << endl;
cout << "1. Point2D, e.g., (2,6.5)" << endl;
cout << "2. ColorPoint2D, e.g., blue(-4.5,3.5)" << endl;
cout << "3. WeightedPoint2D, e.g., .12(3.6,8.7)" << endl;
cout << endl;
cout << "Enter 0 when you are finished. " << endl;
cin >> num_select;
while (num_select != 0)
{
switch (num_select)
{
case 1: // Directs the user to Point2D
cout << "Selection: 1" << endl;
cout << "x = ";
cin >> x;
cout << "y = ";
cin >> y;
points.push_back(new Point2D(x,y));
break;
case 2: // Directs the user to ColorPoint2D
cout << "Selection: 2" << endl;
cout << "x = ";
cin >> x;
cout << "y = ";
cin >> y;
cout << "color = ";
cin.ignore();
getline (cin,col);
points.push_back(new ColorPoint2D(x,y,col));
break;
case 3: // Directs the user to WeightedPoint2D
cout << "Selection: 3" << endl;
cout << "x = ";
cin >> x;
cout << "y = ";
cin >> y;
cout << "weight = ";
cin >> wt;
points.push_back(new WeightedPoint2D(x,y,wt));
break;
default: // Ends the selection process
cout << "Selection: 0";
break;
}
cin >> num_select;
}
cout<<endl;
Point2D type;
for (int i = 0; i < points.size(); i++)
{
type.returnType();
sort (points.begin(),points.end(), compare);
}
// Prints out the selection
cout << "Your points in sorted order are " << endl;
cout << endl;
for (int i = 0; i < points.size(); i++)
{
cout << i+1 << ": ";
(*points[i]).print();
//(*sort_points[i]).print();
cout << endl;
}
for (int i = 0; i < points.size(); i++)
{
delete points[i];
}
system ("pause");
return 0;
}
示例2: compare
/**
Compares the classes
@ parameter Point2D* a
@ parameter Point2D* b
@ returns 0
*/
bool compare (Point2D* a, Point2D* b)
{
vector <Point2D*> sort_points;
Point2D type;
// Rules for comparison of Point2D class
if (type.returnType() == 1)
{
//Point2D* a = dynamic_cast <Point2D*> (a);
//Point2D* b = dynamic_cast <Point2D*> (b);
if (sqrt(pow(a->getx(),2) +pow(a->gety(),2)) < sqrt(pow(b->getx(),2) +pow(b->gety(),2)))
{
sort_points.push_back(a);
return true;
}
else if (sqrt(pow(a->getx(),2) +pow(a->gety(),2)) > sqrt(pow(b->getx(),2) +pow(b->gety(),2)))
{
//sort_points.push_back(b);
return false;
}
else if (sqrt(pow(a->getx(),2) +pow(a->gety(),2)) == sqrt(pow(b->getx(),2) +pow(b->gety(),2)))
{
double angleA = atan2(a->gety(),a->getx());
double angleB = atan2(b->gety(),b->getx());
if (angleA < angleB)
sort_points.push_back(a);
return true;
//else
//sort_points.push_back(b);
//return false;
}
}
// Rules for comparison of ColorPoint2D class
else if (type.returnType() == 2)
{
ColorPoint2D* a = dynamic_cast <ColorPoint2D*> (a);
ColorPoint2D* b = dynamic_cast <ColorPoint2D*> (b);
if (sqrt(pow(a->getx(),2) +pow(a->gety(),2)) < sqrt(pow(b->getx(),2) +pow(b->gety(),2)))
{
sort_points.push_back(a);
return true;
}
else if (sqrt(pow(a->getx(),2) +pow(a->gety(),2)) > sqrt(pow(b->getx(),2) +pow(b->gety(),2)))
{
//sort_points.push_back(b);
return false;
}
else if (sqrt(pow(a->getx(),2) +pow(a->gety(),2)) == sqrt(pow(b->getx(),2) +pow(b->gety(),2)))
{
string colA = a->getc();
string colB = b->getc();
if (colA < colB)
sort_points.push_back(a);
return true;
//else
//sort_points.push_back(b);
//return false;
}
}
// Rules for comparison of WeightedPoint2D class
else if (type.returnType() == 3)
{
WeightedPoint2D* a = dynamic_cast <WeightedPoint2D*> (a);
WeightedPoint2D* b = dynamic_cast <WeightedPoint2D*> (b);
if (sqrt(pow(a->getx(),2) +pow(a->gety(),2)) < sqrt(pow(b->getx(),2) +pow(b->gety(),2)))
{
sort_points.push_back(a);
return true;
}
else if (sqrt(pow(a->getx(),2) +pow(a->gety(),2)) > sqrt(pow(b->getx(),2) +pow(b->gety(),2)))
{
//sort_points.push_back(b);
return false;
}
else if (sqrt(pow(a->getx(),2) +pow(a->gety(),2)) == sqrt(pow(b->getx(),2) +pow(b->gety(),2)))
{
double colA = a->getwt();
double colB = b->getwt();
if (colA < colB)
sort_points.push_back(a);
return true;
//else
//sort_points.push_back(b);
//return false;
}
}
return true;
}