2011-04-30 17 views
0

皆さん、こんにちは、私はちょっと頭がおかしくなりました。 throwMudAt()関数では、距離を見つける必要がありますが、ノードの個々の変数を呼び出す方法はわかりません。 (各ノードは、XとYを持っており、私はそれに渡された任意のノードとの点(x、y)との間の距離を見つける必要があります。 ありがとう! 正しい方向に私を指すものが欠落していてください。リンクされたリスト関数のヘルプ....オーバーロードされたストリームの操作と取得と設定の関数C++

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; 
} 
*/ 
+6

多くのコードを読まなくても、あなたの質問に答えられる可能性が高くなります。あなたの問題を説明するコンパイル可能な例を作ろうとしますが、できるだけシンプルです。それは私たちがあなたの解決を手助けするのを簡単にします? –

+0

私はすべてのコードが役立つと思った。 paticularで... 私はスロー泥の助けを求めています、私は最初の投稿でその目的を説明しました...しかし、どこに問題があるかは、ノードのいずれかから変数を使用する方法です。ここで私はノードから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(合計)。 – adsderek

答えて

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

その機能は不完全でした。しかし、私はそれを理解しました... <<演算子のオーバーロードの助けがあればいいです... 私はその関数からgetX()関数などを呼び出す方法を理解する必要があります。 – adsderek

+1

「getX()を呼び出す方法」に関する助けを言うとどういう意味ですか?私が知っている限り他の機能と同じですが、私が何かを逃していない限り、説明してください。 – atoMerz

関連する問題