所以我硬编码在一些obejcts,这里是类重复调用一个全局函数,导航通过列表每次路过3个对象
class Point // class point will have an x and a y value
{
public:
// should contain attributes of a single point
double x ; // x value
double y ; //y value
double f ; //importance factor
//constructor
Point (double xpoint, double ypoint)// can accept two agruments
{
x = xpoint ;
y = ypoint ;
}
//default constructor
Point()
{
x = 7;
y = 8;
}
//set methods
void setX (double pointx)
{
x = pointx;
}
void setY (double pointy)
{
y = pointy ;
}
void setF(double inF)
{
f = inF ;
}
//get methods for retyening values to main
double getX()
{
return x; // return the X form the setX
}
double getY()
{
return y ; // return the y from the setY
}
//return the importance factor
double getF()
{
return f ;
}
};
,并手动将它们添加到模板列表
Point point1 (7,5); // new point object point1 with value x=2.5, y=5.3
Point point2 (4,8); // second point obejct
Point point3 (8,9); // third point object
Point point4 (10,5);//fourth point object
Point point5 (6,8);//fifth point
Point point6 (4,7);//point 6
list<Point>pointList ; // stl list that will contain my objects.
pointList.push_back(point1);//adds the obejcts to the list from the back each time {point1}
pointList.push_back(point2);//{point1, point2} < point2 pushed_back
pointList.push_back(point3);//{point, point}
pointList.push_back(point4);
pointList.push_back(point5);
pointList.push_back(point6);
导航通过列表一边喊经过3个对象,每次calculatef全局函数,使所有对象会被分配的F值 这里是caclulate方法F
//global fucntion that takes three objects as arguments
static void calculateF (Point& p1, Point& p2, Point& p3)// point2 needs to be a reference otherwise f is just changed locally.
{
double F ; // i now have an f in the class and in the method? do i need both?
double xPoint1 = p1.getX(); // x value of object p1
double yPoint1 = p1.getY(); // y value of object p1
double xPoint2 = p2.getX(); // x value of object p2
double yPoint2 = p2.getY(); // y coordinates of object p2
double xPoint3 = p3.getX(); // x coordinates of obejct p3
double yPoint3 = p3.getY(); // y coorinates of obejct p3
//equation for working out f from these six values
//temp variables to store the length of the triangles sides.
double p2p3 = sqrt (((xPoint2 - xPoint3)*(xPoint2 - xPoint3)) + ((yPoint2 - yPoint3)*(yPoint2 - yPoint3))); //length point2 to point1 (PR in assingment notes)
cout << "p1p2 is = " << p2p3 << endl; // print the value of p2p3 (PR)
double p1p2 = sqrt (((xPoint1 - xPoint2)*(xPoint1 - xPoint2)) + ((yPoint1 - yPoint2)*(yPoint1 - yPoint2))); //length point1 to point 2 (LP in assingment notes)
cout << "p1p2 is = " << p1p2 << endl;
double p1p3 = sqrt (((xPoint1 - xPoint3)*(xPoint1 - xPoint3)) + ((yPoint1 - yPoint3)*(yPoint1 - yPoint3)));//length of point1 to point 3 (LR in assigment notes)
cout << "hypotenuse p1p3 is = " << p1p3 << endl;
F = p1p2 + p2p3 - p1p3 ; //equation for f
cout << "importance factor is " << F << endl ;
p2.setF(F); // setting F to f in class
}
我已经能够通过一次传递obejcts 3不使用列表像(点1,点2,POINT3)等使用的方法,到目前为止是唯一的办法这是没有好,因为我需要用以找到点最低f值并将其从列表中删除。然后再次重新计算f值并重复此过程,直到点数减少到用户指定的数量。所以,因为需要减少点数以及列表大小,所以我需要每次向方法传递三个对象时浏览列表。我希望这个问题很长时间,我只想对整个计划进行概述。
总结,我需要通过每次路过三个对象的全局方法列表
非常感谢
导航编辑:请家伙,我一直在这几天,没有任何的帮助迄今为止是相当有什么我需要..或者我需要他们更多的信息
代码现在读
using namespace std ;
class Point // class point will have an x and a y value
{
public:
// should contain attributes of a single point
double x ; // x value
double y ; //y value
double f ; //importance factor
//constructor
Point (double xpoint, double ypoint)// can accept two agruments
{
x = xpoint ;
y = ypoint ;
}
//default constructor
Point()
{
x = 7;
y = 8;
}
//set methods
void setX (double pointx)
{
x = pointx;
}
void setY (double pointy)
{
y = pointy ;
}
void setF(double inF)
{
f = inF ;
}
//get methods for retyening values to main
double getX()
{
return x; // return the X form the setX
}
double getY()
{
return y ; // return the y from the setY
}
//return the importance factor
double getF()
{
return f ;
}
};
//global fucntion that takes three objects as arguments
static void calculateF (Point& p1, Point& p2, Point& p3)// point2 needs to be a reference otherwise f is just changed locally.
{Fdouble F;
double xPoint1 = p1.getX(); // x value of object p1
double yPoint1 = p1.getY(); // y value of object p1
double xPoint2 = p2.getX(); // x value of object p2
double yPoint2 = p2.getY(); // y coordinates of object p2
double xPoint3 = p3.getX(); // x coordinates of obejct p3
double yPoint3 = p3.getY(); // y coorinates of obejct p3
//equation for working out f from these six values
//temp variables to store the length of the triangles sides.
double p2p3 = sqrt (((xPoint2 - xPoint3)*(xPoint2 - xPoint3)) + ((yPoint2 - yPoint3)*(yPoint2 - yPoint3))); //length point2 to point1 (PR in assingment notes)
//cout << "p1p2 is = " << p2p3 << endl; // print the value of p2p3 (PR)
double p1p2 = sqrt (((xPoint1 - xPoint2)*(xPoint1 - xPoint2)) + ((yPoint1 - yPoint2)*(yPoint1 - yPoint2))); //length point1 to point 2 (LP in assingment notes)
//cout << "p1p2 is = " << p1p2 << endl;
double p1p3 = sqrt (((xPoint1 - xPoint3)*(xPoint1 - xPoint3)) + ((yPoint1 - yPoint3)*(yPoint1 - yPoint3)));//length of point1 to point 3 (LR in assigment notes)
//cout << "hypotenuse p1p3 is = " << p1p3 << endl;
F = p1p2 + p2p3 - p1p3 ; //equation for f
p2.setF(F);
//cout << "importance factor is " << p2.getF() << endl ;
//p2.setF(F); // setting F to f in class
}
INT主() { INT userPoints;
cout << "This program will reduce the number of points in shape X to a user defined amount " << endl;
cout << "Enter the number of points you wish this shape to be reduced to: " << endl ;
cin >> userPoints ;
cout << "this shape will be reduced to: " <<userPoints << " points." << endl;
// create objects of type Point, passing different x and y values to the contructor
Point point1 (1,1); // new point object point1 with value x=2.5, y=5.3
Point point2 (2,2); // second point obejct
Point point3 (3,3); // third point object
Point point4 (4,4);//fourth point object
Point point5 (5,5);//fifth point
Point point6 (6,6);//point 6
list<Point>pointList ; // stl list that will contain my objects.
//pointList.push_back(point6);//duplicate point
pointList.push_back(point1);//adds the obejcts to the list from the back each time {point1}
pointList.push_back(point2);//{point1, point2} < point2 pushed_back
pointList.push_back(point3);//{point, point}
pointList.push_back(point4);
pointList.push_back(point5);
pointList.push_back(point6);
//pointList.push_back(point1);//duplicate point
//add objects to the list
//while lists size is > user points
list<Point>::iterator it1(pointList.begin()), it2(pointList.begin()), it3(pointList.begin()); // 3 iterators all originally pointing to beginning of list
if (it1 != pointList.end()) { ++it2; ++it3; }
if (it2 != pointList.end()) { ++it3; }
int cnt = 1;
do {
calculateF(*it1, *it2, *it3);
cout << it1->x << " " << it1->y << " -- ";
cout << it2->x << " " << it2->y << " -- ";
cout << it3->x << " " << it3->y << endl;
cout << "Calc step done " << cnt << " f is" << it1->f << endl ;
++it1;
++it2;
++it3;
if (it1 == pointList.end()) it1=pointList.begin();
if (it2 == pointList.end()) it2=pointList.begin();
if (it3 == pointList.end()) it3=pointList.begin();
cnt++;
} while (it1 != pointList.begin());
/*
while (it3 != pointList.end())
{
calculateF(*it1, *it2, *it3);
++it1;
++it2;
++it3;
}
*/
//remove smallest f and repeat
/*
list<Point>::iterator pI ;
//iterator initially points to beginning of list
for (pI = pointList.begin() ; pI!=pointList.end() ; pI++)
{
cout << *pI ;
}
*/
// calculateF fucntion on point 2
//calculateF(point1, point2, point3);
//calculateF(point2, point3, point4);
//calculateF(point3,point4, point5);
//calculateF(point4,point5,point6);
//point2.getF();
//cout << "point 2 has f value of: " << point2.getF() << endl ;
system ("PAUSE");
return 0 ;
}
你在问什么?你是在问我们为你制定一个算法吗? –
不,我只是碰到一个块,并且对列表和迭代器没有经验。我不知道如何使用迭代器遍历列表,每次删除最低点。类似于list.size> userInput,为所有点计算F,比较F值找到最低值,删除此点并重复。我想我会被迭代器弄糊涂了。 – punity