嘿大家,我现在有点困惑,需要帮助在这些课程中重载流操作。还需要帮助我正在创建的链表中访问数据,在throwMudAt()函数中我需要找到距离但不知道如何调用节点的各个变量。 (每个节点都有一个x和一个Y,我需要找到任何给定节点和传递给它的点(x,y)之间的距离。 谢谢! 缺少任何东西请指出我正确的方向。
class Linknode {
friend class SchmooList;
public:
Linknode(){next=0; data =0;}
~Linknode(){if (data){delete data; data =0;}}
private:
Linknode *next; //"SRO"
Schmoo *data;
};
#endif
class SchmooList {
public:
SchmooList(){first=0;}
// ~SchmooList();
bool isEmpty(){return first==NULL;}
void insertFront(Schmoo*);
void throwMudAt(double, double);//throws mud at the given (x,y) and adds one
//to the mud value of any schmoo within 5.0 feet of given
//within 5.0 means distance <= to 5.0
void removeAt(double, double); //removes any Schmoo that is within 1.0 feet
int getPopulation();
void printAll();//send each Schmoo to STDOUT one per line in list order
private:
Linknode *first;
};
#endif
using namespace std;
void SchmooList::insertFront(Schmoo *nt){
Linknode *temp= new Linknode();
temp -> data=nt;
temp->next=first;
first = temp;
}
void SchmooList::throwMudAt(double xx, double yy){
Linknode *temp=first;
while(temp){
double sum = (pow(xx - temp->data.getX(), 2))+(pow(yy - temp->data.getX(), 2));
double distance = sgrt(sum);
}
/*void SchmooList::removeAt(double x, double y){
Linknode *temp=first
while(temp){
double xd=
double yd= temp - y;
if(xd <= 1 || yd <= 1){
temp == 0;
*/
int SchmooList::getPopulation(){
int pop=0;
Linknode *temp=first;
while(temp){
pop++;
temp=temp->next;
}
return pop;
}
void SchmooList::printAll(){
Linknode *temp=first;
while(temp){
cout << '*' << endl; //print the object
temp = temp->next;
}
cout << getPopulation();//for testing
}
class Schmoo{
public:
Schmoo(double, double);
void setX(double);
double getX() const;
void setY(double);
double getY() const;
void setMud(int);
int getMud() const;
private:
double x;
double y;
int mud;
};
#endif
Schmoo::Schmoo(double xx, double yy){
x = xx;
y = yy;
setMud(0);
}
void Schmoo::setX(double x1){
x = ( x1 >= -1000 && x1 <= 1000) ? x1 : 0;
}
double Schmoo::getX() const{
return x;
}
void Schmoo::setMud(int m){
mud = ( m >= -1000 && m <= 1000) ? m : 0;
}
int Schmoo::getMud() const{
return mud;
}
/*ostream &operator<<(ostream &os, Schmoo &s){
if(s->getMud() == 1){
os << "Schmoo at (" << s.x << ", " << s.y << ") was hit mud " << mud << "time.";
}
os << "Schmoo at (" << s.x << ", " << s.y << ") was hit with mud" << mud << "times.";
return os;
}
*/
答案 0 :(得分:1)
你忘了在列表中前进。
void SchmooList::throwMudAt(double xx, double yy){
Linknode *temp=first;
while(temp){
double sum = (pow(xx - temp->data.getX(), 2))+(pow(yy - temp->data.getX(), 2));
double distance = sqrt(sum);
temp = temp->next;
}