c++中重载流op、get和set函数的帮助



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

你忘了往下看了

  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;
    }

相关内容

  • 没有找到相关文章

最新更新