当前位置:网站首页>E. Zoom in and zoom out of X (operator overloading)

E. Zoom in and zoom out of X (operator overloading)

2022-06-11 08:55:00 ZZZWWWFFF_



 Title Description 

X Letters can be enlarged and reduced , Turn into n That's ok X(n=1,3,5,7,9,...,21). for example ,3 That's ok x The pattern is as follows :

 Now let's assume that n That's ok (n>0, Odd number )X pattern , The remote control can control X The enlargement and reduction of the pattern 
. The remote control has 5 A button ,1)show, Show the current X pattern ;2)show++,  Show when 
 front X pattern , Zoom in on the pattern ,n+2;3)++show, Enlarge the pattern first ,n+2, Then show the figure 
 case ;4)show--, Show the current X pattern , Zoom out again ,n-2;5)--show, Shrink first 
 Small pattern ,n-2, Then display the pattern . hypothesis X The enlargement and reduction of the pattern are 1-21 Between .n=1 when 
, Shrinking doesn't work ,n=21 when , Zoom in doesn't work .

 Utility class CXGraph Express X Pattern and its magnification 、 narrow 、 Show . The main function simulates the remote controller , Code 
 as follows , Do not modify the . Please add CXGraph Class definition and Implementation .
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;
}
 Input 

  first line n, Greater than 0 The odd number ,X The initial size of the pattern .

 The second line , Operating frequency 

 One line for each operation , by show、show++、show--、--show、++show One of , See the title for the specific operation meaning .

 Output 

  For each operation , Output corresponding X pattern .


 sample input 1 
3
5
show
show++
show++
++show
--show

 sample output 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

This question mainly investigates the application of operator overloading , The main thing to pay attention to problem Is for the output operator << When overloading, we must add... To the class variable passed in const, because xGraph-- or ++ The return of is a temporary object , The system is worried about your modifying temporary objects , Therefore, it is required to add const

#define _CRT_SECURE_NO_WARNINGS
#include<algorithm>
#include<string>
#include<vector>
#include <iomanip>
#include<cmath>
#include<cstring>
#include<cctype>
#include<set>
#include<queue>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;

class CXGraph
{
private:
	int n;
public:
	CXGraph(int nn) { n = nn; }// Pay attention to the addition of const
	friend ostream &operator<<(ostream& output, const CXGraph &c)// Output 
	{
		for (int i = 1; i <= c.n/2+1; i++)// The first half of the line 
		{
			for (int j = 1; j < i; j++)// Output space 
			{
				output << ' ';
			}
			
			for (int j = c.n; j >=2*i-1 ; j--)
			{
				output << 'X';
			}
			output << endl;
		}

		for (int i = c.n/2; i >=1; i--)// Second half row 
		{
			for (int j = 1; j < i; j++)// Output space 
			{
				output << ' ';
			}
			for (int j =c.n ; j >=2*i-1; j--)
			{
				output << "X";
			}
			output << endl;
		}
		return output;
	}
	friend CXGraph operator++(CXGraph&c)// In front of ++
	{
		if(c.n!=21)
		c.n+=2;
		return c;
	}
	CXGraph operator++(int)// After ++
	{
		CXGraph cc(n);
		if(cc.n!=21)
		n+=2;
		return cc;
	}
	friend CXGraph operator--(CXGraph& c)// In front of --
	{
		if(c.n!=1)
		c.n-=2;
		return c;
	}
	CXGraph operator--(int)// After --
	{
		CXGraph cc(n);
		if(cc.n!=1)
		n-=2;
		return cc;
	}

};

int main()
{
	int t, n;
	string command;
	cin >> n;// Initial size 
	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;
		}
		if (command == "show")
		{
			cout << xGraph << endl;
		}
	}
	return 0;
}

原网站

版权声明
本文为[ZZZWWWFFF_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110836519887.html