ハノイ
あなたのコードから、問題をどのように解決したいかわかりません。あなたにとってのステップ 1 は、問題にどのように取り組みたいかを定式化し、プログラミング言語なしで (疑似コードで、または Word の箇条書きリストとして) 説明することだと思います。そして、そこからプログラミング言語でプログラミングを始めるかもしれません。
C++
あなたのコードは、ランダムなコードの断片に少し似すぎています。この基本的な 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
}
g++ -o hanoi.x hanoi.cpp
たとえば、Unixでコンパイルします。