「Lattice.txt」という名前のファイルに格子の座標のセットがあります。以下の私のコードは、各格子の隣人の数をチェックします-
void CheckLattice(int b)
{
vector<Lattice> lattices;
/* reading from file logic */
ifstream theFile("Lattice.txt");
double x1,y1,x2,y2;
while (theFile >> x1 >> y1 >> x2 >> y2) {
lattices.push_back(Lattice(x1,y1, x2,y2));
}
/* counting neighbors logic */
int lattice_count = 0;
int GroupLattice=0;
for (int x = 0; x < lattices.size(); ++x) {
if (lattices[b].NearestLattice(lattices[x])) {
if (x==b) {
continue; //this is our lattice , skip it.
}
lattice_count++;
}
GroupLattice++;
}
cout<<"Lattice "<<(b+1)<<" has = "<<lattice_count<<" neighbours "<<endl;
cout << " The number of lattice with "<< lattice_count << " neighbours are " << GroupLattice << endl;
cout << endl;
}
int main()
{
int neighbour=0;
for(int i=0; i<10; i++){
CheckLattice(i);
}
return 0;
}
次のような値を返します-
Lattice 1 has = 7 neighbours The number of lattice with 7 neighbours are 14 Lattice 2 has = 3 neighbours The number of lattice with 3 neighbours are 15 Lattice 3 has = 8 neighbours The number of lattice with 8 neighbours are 14 Lattice 4 has = 6 neighbours The number of lattice with 6 neighbours are 15 Lattice 5 has = 8 neighbours The number of lattice with 8 neighbours are 14 Lattice 6 has = 8 neighbours The number of lattice with 8 neighbours are 15 Lattice 7 has = 8 neighbours The number of lattice with 8 neighbours are 14 Lattice 8 has = 1 neighbours The number of lattice with 1 neighbours are 15 Lattice 9 has = 7 neighbours The number of lattice with 7 neighbours are 14 Lattice 10 has = 5 neighbours The number of lattice with 5 neighbours are 15
このコードは、各ラティスの隣接数を正しく返しますが、同じ隣接数を持つラティスの総数の値を返しません。代わりに、14 または 15 を返すだけです。どうすれば修正できますか?
このような出力が必要です-
Lattice 1 has = 7 neighbours Lattice 2 has = 3 neighbours Lattice 3 has = 8 neighbours Lattice 4 has = 6 neighbours Lattice 5 has = 8 neighbours Lattice 6 has = 8 neighbours Lattice 7 has = 8 neighbours Lattice 8 has = 1 neighbours Lattice 9 has = 7 neighbours Lattice 10 has = 5 neighbours The number of lattice with 1 neighbours are 1 The number of lattice with 2 neighbours are 1 The number of lattice with 7 neighbours are 2 The number of lattice with 8 neighbours are 4 ...etc
私は何かを逃したかもしれません。誰でもそれで私を助けることができますか?