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


一、题目描述

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图案及其放大、缩小、显示。主函数模拟遥控器,代码如下,不可修改。请补充CXGraph类的定义和实现。

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.输入

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

第二行,操作次数

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

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

2.输出

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

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


三、参考代码

#include<iostream>
using namespace std;
class gra
{
	int n;
public:
	gra() { n = 0; }
	gra(int num):n(num){}
	friend gra operator ++(gra& p, int)
	{
		gra temp = p;
		p.n += 2;

		if (p.n > 21)
		{
			p.n = 21;
		}
		return temp;
	}
	friend gra operator++(gra& p)
	{
		
		p.n += 2;
	
		if (p.n > 21)
		{
			p.n = 21;
		}
		return p;
	}
	friend gra operator --(gra& p, int)
	{
		gra temp = p;
		p.n -= 2;
		if (p.n <  1)
		{
			p.n = 1;
		}
		return temp;
	}
	friend gra operator--(gra& p)
	{
		p.n -= 2;
		if (p.n < 1)
		{
			p.n = 1;
		}
		return p;
	}
	friend ostream& operator << (ostream& output, gra const& a)
	{
		int i, j, k = a.n;
		for (i = 0; i < (a.n + 1) / 2; i++)
		{
			for (j = 0; j < (a.n - k) / 2; j++)
				output << " ";
			for (j = k; j > 0; j--)
				output << "X";
			output << endl;
			k = k - 2;
		}
		k = 3;
		for (i = (a.n + 1) / 2; i < a.n; i++)
		{
			for (j = (a.n - k) / 2; j > 0; j--)
				output << " ";
			for (j = 0; j < k; j++)
				output << "X";
			output << endl;
			k = k + 2;
		}
		return output;
	}
};
int main()
{
	int t, n;
	string command;
	cin >> n;
	gra 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Z1Jxxx

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

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

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

打赏作者

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

抵扣说明:

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

余额充值