【发布时间】:2014-04-26 12:10:14
【问题描述】:
所以我在一些对象中硬编码,这是类,
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 值 这里是计算 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
}
到目前为止,我能够使用该方法的唯一方法是一次传递对象 3,而不使用 (point1,point2,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.
{ 双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 main() { 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 ;
}
【问题讨论】:
-
你在问什么?您是要我们为您制定算法吗?
-
不,我刚刚遇到了障碍,对列表和迭代器缺乏经验。我不知道每次删除最低点时如何使用迭代器遍历列表。类似于 while list.size>userInput,计算所有点的 F,比较 F 值以找到最低点,删除该点并重复。我想我被迭代器弄糊涂了。