-1
这是一个在课堂上给出的任务,我对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文件。一套用于挂钉,一套用于挂环。下面是类的在挂头体:
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
,请帮助我了解这些特定程序如何使用这些程序,但我没有经验。
为什么你不应该为每个peg有不同的数组和'ringUsed'? – molbdnilo
我不知道,我只是认为应该这样做,因为我给出的所有起始代码都只有一个。 – Rhuen