开发者

Trying to make a program using a data structure with an array

I just started trying to learn programming so i am taking a class at my local community college and we have to make a program of the tower of hanoi i have been reading books going to the library and went to tutoring so far this is what i came up with. can someone point me in the right direction or give me some help please.

// Tower of Hanoi

#include<iostrea开发者_运维百科m.h>
#include<stdlib.h>
#include<conio.h>

{
// need to use this function with an array of up to 6 disk to display and output
// showing the disk moving from tower to tower
// something like
//   *
//   **
//   ***
//   and as they move it will show them moving in steps
//

 void HanoiTowers::HanoiTowers(int n, char srcpole, char sparepole, char dstpole)
   {
    if (n==1)
      {
         // sparepole and dstpole are swapped
       cout<<"Move top disk from pole"<<tower
         cout<<"Move pole"<<dstpole<<endl;
      else
      {
       rHanoiTowers(cout-1,srcpole, sparepole, dstpole);
         rHanoiTowers(1, srcpole, dstpole, sparepole);
         rHanoiTowers(count-1, sparepole, dstpole, srcpole);
      }
   }

   void main()
   {



   class array
   {
    private:
      int t1[6],t2[6],t3[6];
      int srcpole, sparepole, dstpole;
   }

cout<<"Enter The amount of disk from 1-6"<<endl;

return 0;             
}


Hanoi

From your code I can not see how you want to solve your problem. Step 1 for you would be, I think, to formulate how you want to approach your problem, and describe without a programming language -- maybe in pseudo code, or as a bullet-list in Word. Then, you might start programming in a programming language from that.

C++

Your code looks a bit too much like some random code fragment. Lets see if you can get a bit more started with this basic C++ code fragment. It is not really problem specific, but it might give you a start for a real C++ program.

//file: hanoi.cpp
// Tower of Hanoi
#include <iostream>  //! std is without '.h', add a space after 'include'
//! '<stdlib.h>' only needed for C-ish; maybe later, and then use <cstdlib>
//! first try it without '#include <conio.h>', try using 'cout << "\r"' with numbers.
#include <assert.h> //! assert()

using std::cout; //! do this, or always writw `std::cout`
using std::cin;
using std::endl;


//! DECLARATION
class HanoiTowers
{
  const size_t disks_;   //! some people mark class fields with a ending '_'
public:
  explicit HanoiTowers(size_t disks);
  //! use 'size_t' for indexes into array
  void move(int n, size_t srcpole, size_t sparepole, size_t dstpole);
};


//! IMPLEMENTATION
HanoiTowers::HanoiTowers(size_t disks)
  : disks_(disks)
{
  // ... more init code ...
}

void HanoiTowers::move(int n, size_t srcpole, size_t sparepole, size_t dstpole)
{
  //! ... is your algorithm going to work? I dont know ...
  if(n==1) {
    // sparepole and dstpole are swapped
    cout << "Move top disk from pole" << srcpole;  //! ending ';' was missing
    cout << "Move pole" << dstpole << endl;
  } //! closing '}' was missing
  else {
    // ... any code what you need. I can not get your idea here ...
    move(n-1,srcpole, sparepole, dstpole);
    move(1, srcpole, dstpole, sparepole);
    move(n-1, sparepole, dstpole, srcpole);
  }
}

static const size_t HEIGHT = 6; //! use consts

//! I can not see the use of this data strucure for you problem. whats your idea?
class array
{
  //! ok, its good to make stuff private, but then you need public accessors
private:
  int t1[HEIGHT],t2[HEIGHT],t3[HEIGHT];    //! what do you want to store in these?
                  //! is it not enough to store that stack-heights for each stack?
  int srcpole, sparepole, dstpole;

//! accessors:
public:
  int getT1WithIndex(size_t idx) const {
    assert(idx < HEIGHT); //! checks in code
    return t1[idx];
  }
  //! ... more of this, if needed ...
};


//! MAIN

int main(int argc, const char* args[]) //! one 'main' to make a program runnable
{
  // ... any program code ...

  cout << "Enter The amount of disk from 1-6..." << endl;
  int disks = 6;  //! default
  cin >> disks; //! let user enter a number

  //! create a data structure
  HanoiTowers hanoi(disks);

  //! execute your algorithm
  hanoi.move(0,0,0,0); //! ???
  //! ...more...???

  return 0; //! '0': program succeeded
}

and compile it with g++ -o hanoi.x hanoi.cpp on Unix for example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜