X的放大与缩小(运算符重载)

目录

题目描述

思路分析

AC代码


题目描述

X字母可以放大和缩小,变为n行X(n=1,3,5,7,9,...,21)。例如,3行x图案如下:

​现假设一个n行(n>0,奇数)X图案,遥控器可以控制X图案的放大与缩小。遥控器有5个按键,1)show,显示当前X图案;2)show++, 显示当前X图案,再放大图案,n+2;3)++show,先放大图案,n+2,再显示图案;4)show--,显示当前X图案,再缩小图案,n-2;5)--show,先缩小图案,n-2,再显示图案。假设X图案的放大和缩小在1-21之间。n=1时,缩小不起作用,n=21时,放大不起作用。

用类CXGraph表示X图案及其放大、缩小、显示。

输入

第一行n,大于0的奇数,X图案的初始大小。

第二行,操作次数

每个操作一行,为show、show++、show--、--show、++show之一,具体操作含义见题目。

输出

对每个操作,输出对应的X图案。

输入样例1

3
5
show
show++
show++
++show
--show

输出样例1

XXX
X
XXX

XXX
X
XXX

XXXXX
XXX
X
XXX
XXXXX

XXXXXXXXX
XXXXXXX
XXXXX
XXX
X
XXX
XXXXX
XXXXXXX
XXXXXXXXX

XXXXXXX
XXXXX
XXX
X
XXX
XXXXX
XXXXXXX

思路分析

先说一下注意的问题,加上int的是后增量,还需要看到题目说n=1时,缩小不起作用,n=21时,放大不起作用。

关于打出这个图形的问题,我之前打过三角形和棱形,差不多的思路,都是先打上面一半,然后循环倒回来打出下面一半。

格式不对的时候,小心的是打完X之后是没有空格的,需要直接回车。

跑不起来的时候,尝试加上或者去掉一些const和&。

AC代码

#include <iostream>
#include <string>
using namespace std;
class CXGraph
{
	int num;
public:
	CXGraph(int num):num(num) {  }
	friend ostream& operator<<(ostream& out,CXGraph x);
	CXGraph& operator++()
	{
		if (num <= 19)
		{
			num++;
			num++;
		}
		return *this;
	}
	CXGraph operator++(int)
	{
		CXGraph temp=*this;
		if (num <= 19)
		{
			num++;
			num++;
		}
		return temp;
	}
	CXGraph& operator--()
	{
		if (num >= 3)
		{
			num--;
			num--;
		}
		return *this;
	}
	CXGraph operator--(int)
	{
		CXGraph temp=*this;
		if (num >= 3)
		{
			num--;
			num--;
		}
		return temp;
	}
	void show()
	{
		int i, j;
		for (i = 0; i <num/2; i++)
		{
			for (j = 0; j < num; j++)
				if (j<num - i && j>=i)
					cout << 'X';
				else if(j<num-i)
					cout << ' ';
			cout << endl;
		}
		for (i = num / 2; i >=0 ; i--)
		{
			for (j = 0; j < num; j++)
				if (j < num - i && j >= i)
					cout << 'X';
				else if(j<num-i)
					cout << ' ';
			cout << endl;
		}
	}
};
ostream& operator<<(ostream& out,CXGraph x)
{
	x.show();
	return out;
}
int main()

{

	int t, n;
	string command;
	cin >> n;
	CXGraph xGraph(n);
	cin >> t;
	while (t--)
	{
		cin >> command;
		if (command == "show++")
			cout << xGraph++ << endl;
		else if (command == "++show")
			cout << ++xGraph << endl;
		else if (command == "show--")
			cout << xGraph-- << endl;
		else if (command == "--show")
			cout << --xGraph << endl;
		else if (command == "show")
			cout << xGraph << endl;
	}
	return 0;

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MaolinYe(叶茂林)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值