#include<iostream>
#include<fstream>
using namespace std;
ofstream hanoilog;
int cc = 0;
void supermove(char, char, char, int);
int main()
{
hanoilog.open("Hanoi Log.txt");
char anotherround;
int ju = 0;
do
{
++ju;
cc = 0;
char bas, temp, des;
int n;
cout << "请输入起始柱名称:" << endl;
cin >> bas;
cout << "请输入目标柱名称:" << endl;
cin >> des;
cout << "请输入过程柱名称:" << endl;
cin >> temp;
cout << "请输入需要挪动的圆盘数:" << endl;
cin >> n;
hanoilog << " 第" << ju << "局:" << endl;
hanoilog << " 一共" << n << "个盘子,从" << bas << "柱移动到" << des << "柱。" << endl;
hanoilog << " 方法如下:" << endl;
supermove(bas, temp, des, n);
cout << "\n完成! 一共需要" <<cc<<"步"<< endl;
hanoilog << "\n 一共需要" << cc << "步" << endl;
hanoilog << endl << endl;
cout << endl;
cout<<"还需要再来一局吗?(y/n)"<<endl;
cin >> anotherround;
} while (anotherround == 'y');
hanoilog.close();
cin.get();
return 0;
}
void supermove(char bas, char temp, char des, int n)
{
if (n > 2)
{
supermove(bas, des, temp, n - 1);
supermove(bas, temp, des, 1);
supermove(temp, bas, des, n - 1);
}
else if (n == 2)
{
cout << " " << bas << " --> " << temp << endl;
cout << " " << bas << " --> " << des << endl;
cout << " " << temp << " --> " << des << endl;
hanoilog << " " << bas << " --> " << temp << endl;
hanoilog << " " << bas << " --> " << des << endl;
hanoilog << " " << temp << " --> " << des << endl;
cc += 3;
}
else
{
cout << " " << bas << " --> " << des << endl;
hanoilog << " " << bas << " --> " << des << endl;
cc += 1;
}
}
汉诺塔程序加了一个log记录功能
最新推荐文章于 2024-07-24 20:55:30 发布