2016-09-24 43 views
-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_topmove_ring函数的主体,而不具有不同的阵列,ringUsed用于每个挂钩。我已经研究了很长一段时间,我不需要整个解决方案,但有些方向将不胜感激。如果我应该使用这些typedef,请帮助我了解这些特定程序如何使用这些程序,但我没有经验。

+0

为什么你不应该为每个peg有不同的数组和'ringUsed'? – molbdnilo

+0

我不知道,我只是认为应该这样做,因为我给出的所有起始代码都只有一个。 – Rhuen

回答

0

改变towersrings和制作remove_top_ring功能后...

这是我想出了add_ring_to_top()

/* adds a new ring to the peg. 
* only works if peg isn't full 
* and the new diameter is smaller 
* than the existing top ring diameter. */ 

void Pegs::add_ring_to_top(const value_type ring_diameter, int peg_number) { 

cout << "Adding ring of diameter: " << ring_diameter << " to the top" << endl; 

if (peg_number == 1) { // if the choice is peg 1 
    if (ringsUsed1 < (ringCapacity + 1)) { // Is the peg at maximum capacity? 
     if (ringsUsed1 == 0) { // Is the peg empty? 
      *arr1 = ring_diameter; 
      ringsUsed1++; 
      legal = true; 
     } 
     else { 
      if (ring_diameter < *arr1) { // Is the new diameter lower than the topmost ring? 
       value_type *xarr1 = new value_type[ringCapacity]; // New array 
       *xarr1 = ring_diameter; 
       for (unsigned int i = 1; i <= ringsUsed1 + 1; i++) { 
        xarr1[i] = arr1[i - 1]; 
       } 
       delete[] arr1; 
       arr1 = xarr1; 
       ringsUsed1++; 
       legal = true; 
      } 
      else { 
       cout << "Error!" << endl; 
       legal = false; 
      } 
     } 
    } 
    else { 
     cout << "Error!" << endl; 
     legal = false; 
    } 
} 
else if (peg_number == 2) { 
    if (ringsUsed2 < (ringCapacity + 1)) { // Is the peg at maximum capacity? 
     if (ringsUsed2 == 0) { // Is the peg empty? 
      *arr2 = ring_diameter; 
      ringsUsed2++; 
      legal = true; 
     } 
     else { 
      if (ring_diameter < *arr2) { // Is the new diameter lower than the topmost ring? 
       value_type *xarr2 = new value_type[ringCapacity]; // New array 
       *xarr2 = ring_diameter; 
       for (unsigned int i = 1; i <= ringsUsed2 + 1; i++) { 
        xarr2[i] = arr2[i - 1]; 
       } 
       delete[] arr2; 
       arr2 = xarr2; 
       ringsUsed2++; 
       legal = true; 
      } 
      else { 
       cout << "Error!" << endl; 
       legal = false; 
      } 
     } 
    } 
    else { 
     cout << "Error!" << endl; 
     legal = false; 
    } 
} 
else { 
    if (ringsUsed3 < (ringCapacity + 1)) { // Is the peg at maximum capacity? 
     if (ringsUsed3 == 0) { // Is the peg empty? 
      *arr3 = ring_diameter; 
      ringsUsed3++; 
      legal = true; 
     } 
     else { 
      if (ring_diameter < *arr3) { // Is the new diameter lower than the topmost ring? 
       value_type *xarr3 = new value_type[ringCapacity]; // New array 
       *xarr3 = ring_diameter; 
       for (unsigned int i = 1; i <= ringsUsed3 + 1; i++) { 
        xarr3[i] = arr3[i - 1]; 
       } 
       delete[] arr3; 
       arr3 = xarr3; 
       ringsUsed3++; 
       legal = true; 
      } 
      else { 
       cout << "Error!" << endl; 
       legal = false; 
      } 
     } 
    } 
    else { 
     cout << "Error!" << endl; 
     legal = false; 
    } 
} 
} 

这是我想出了move_ring()

/* top ring is moved from start peg to end peg. 
* uses add function from Peg class. 
* function also checks if move is legal or not 
* and returns 1 if it is legal else returns 0. */ 

int Rings::move_ring(int start_peg, int end_peg) { 

// attempt to move 
if (start_peg == 1) { 
    Pegs::add_ring_to_top(*arr1, end_peg); 
    Pegs::remove_top_ring(1); 
    if (Pegs::legal == true) { 
     cout << "Success." << endl; 
     return 1; 
    } 
    else { 
     cout << "Fail." << endl; 
     return 0; 
    } 
} 
else if (start_peg == 2) { 
    Pegs::add_ring_to_top(*arr2, end_peg); 
    Pegs::remove_top_ring(2); 
    if (Pegs::legal == true) { 
     cout << "Success." << endl; 
     return 1; 
    } 
    else { 
     cout << "Fail." << endl; 
     return 0; 
    } 
} 
else if (start_peg == 3) { 
    Pegs::add_ring_to_top(*arr3, end_peg); 
    Pegs::remove_top_ring(3); 
    if (Pegs::legal == true) { 
     cout << "Success." << endl; 
     return 1; 
    } 
    else { 
     cout << "Fail." << endl; 
     return 0; 
    } 
} 
else 
    cout << "Error." << endl; 
return 0; 
}