帮助链接列表函数...重载流操作以及获取和设置函数 C++
大家好,我现在有点卡住了,需要帮助来重载这些类中的流操作。还需要帮助访问我正在创建的链表中的数据,在 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;
}
*/
Hey everyone, I'm kind of stuck right now, need help with overloading the stream op in these classes. Also need help with access data in the linked list I am creating, In the throwMudAt() function I need to find distance but do not know how to call the individual variables of the node. (each node has an x and a Y and i need to find the distance between any given node and a point (x,y) passed to it.
Thanks!
Missing anything point me in the right direction please.
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;
}
*/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您忘记在列表中前进。
You forgot to go forward in your list.