我刚刚开始尝试学习编程,所以我正在当地的社区学院上课,我们必须制作一个河内塔的程序我一直在读图书去图书馆并去辅导到目前为止这是我想出了什么。有人可以指出我正确的方向或请给我一些帮助。
// Tower of Hanoi
#include<iostream.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;
}
答案 0 :(得分:0)
从您的代码中我看不出您想如何解决问题。我认为,第一步是制定你想要解决问题的方法,并描述没有编程语言 - 可能在 pseudo code ,或者作为Word中的子弹列表。然后,您可以从那开始用编程语言编程。
您的代码看起来有点像一些随机代码片段。让我们看看你是否可以从这个基本的C ++代码片段开始。它并不是特定于问题,但它可能会为您提供真正的C ++程序。
//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
}
并在Unix上用g++ -o hanoi.x hanoi.cpp
编译它。例如。