これはクラスで与えられた割り当てです。私はまだC++には全く新しいです。我々は、ハノイ塔のプログラムを作成するためにダイナミックアレイを使用する必要があると言われました。メインは次のように始まります。ダイナミックアレイを使用したハノイの塔:どのようにしてペグに新しいリングを追加し、リングをペグからペグに移動できますか?
int main()
{
int ring_count, start_peg, end_peg, choice;
bool stay_in_loop = true;
cout << "Enter the number of rings on the first peg:\n" << endl;
cin >> ring_count;
Towers pegs(ring_count);
cout << "The initialized pegs and rings:\n" << pegs << endl;
/* infinite loop for user to enter options to perform
* to solve the tower of hanoi till user asks for exit */
while (stay_in_loop)
{
cout << "Enter 1 to move a ring." << endl;
cout << "Enter 2 to exit the program.\n" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter the ring on which the top ring is to be moved from." << endl;
cin >> start_peg;
cout << "Enter the peg to which the ring needs to moved to." << endl;
cin >> end_peg;
//....etc.
次に、ヘッダーと対応する.cppファイルが2つあります。 1組はペグ用、もう1組はリング用です。ここでペグヘッダー内のクラスの本体は、次のとおりです。リングヘッダーで
public:
/* constructor inits the Peg
* with n rings. The diameter peg's
* rings are from one inch(on top) to
* nth inch(on the bottom). */
Peg(size_t n);
/* returns the number of rings on the peg. */
size_t many_rings()const;
/* returns the value of the diameter of the top most ring. */
size_t top_diameter()const;
/* adds a new ring to the peg. */
void add_ring_to_top(const value_type ring_diameter);
/* remove the topmost ring of the peg */
void remove_top_ring();
/* overload output operator to print the peg object
* along with its rings in a understable format. */
friend ostream& operator << (ostream& os, const Peg& p);
protected:
/* use appropriate data structure for the pegs */
value_type top_most;
size_type ringUsed;
size_type ringCapacity;
int *arr = new int[ringCapacity];
bool legal; // for telling move_ring function if the move is legal
機能があります:int move_ring(int start_peg, int end_peg);
私の問題はこれです:どのように私は私たちに与えられたこのコードを使用することができます関数add_ring_to_top
とmove_ring
の本体を別の配列を持たずに作成し、各ペグにringUsed
を作成します。私はこれについてかなり長い間取り組んできましたが、私はソリューション全体を必要としませんでしたが、いくつかの方向性は高く評価されました。もし私がそれらを使用することになっているのであれば、typedef
これらのプログラムのためにこれらがどのように使用されるのか理解してもらえたら、私はそれらの経験は全くありません。 rings
へtowers
を変更し、remove_top_ring
機能を行った後
それぞれのペグに異なる配列と 'ringUsed'を使用してはいけないのはなぜですか? – molbdnilo
私はちょうど私が与えられたすべての初心者のコードがそれぞれの1つを持っていたので、それがどうすべきかと仮定しました。 – Rhuen