汉诺塔程序加了一个log记录功能

#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;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值