【问题标题】:Help with Linked list functions....overloaded stream op and get and set functions C++链接列表函数的帮助....重载流操作以及获取和设置函数 C++
【发布时间】:2011-08-15 22:28:16
【问题描述】:

大家好,我现在有点卡住了,需要帮助来重载这些类中的流操作。还需要访问我正在创建的链表中的数据的帮助,在 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;
}
*/

【问题讨论】:

  • 如果人们不必阅读这么多代码,他们更有可能回答您的问题。尝试制作一个可编译的示例来说明您的问题,但要尽可能简单。这样我们可以更轻松地帮助您解决问题吗?
  • 我认为拥有所有代码会有所帮助。特别是...我正在寻找有关扔泥的帮助,我在最初的帖子中解释了它的目的...但是我遇到的问题是如何使用其中一个节点的变量。就像这里我试图从一个节点访问 X 和 Y 变量... 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));双倍距离 = sgrt(sum);

标签: c++ get operator-overloading linked-list set


【解决方案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;
    }

【讨论】:

  • 该功能不完整。但我想通了...对
  • 我不明白,当你说“如何调用 getX()”的帮助时,你是什么意思?据我所知,它与任何其他功能相同,除非我遗漏了什么,请解释一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-11
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 2018-03-30
相关资源
最近更新 更多