当前位置:网站首页>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;
}边栏推荐
- 端口占用问题,10000端口
- PVC plastic sheets BS 476-6 determination of flame propagation properties
- 【node】npm部分
- SAP ABAP data types and data objects
- 【C语言-数据存储】数据在内存中是怎样存储的?
- 2022 Niuke winter vacation 3
- 20. 有效的括号
- What is the process of en 1101 flammability test for curtains?
- 马志强:语音识别技术研究进展和应用落地分享丨RTC Dev Meetup
- Textview text size auto fit and textview margin removal
猜你喜欢

leetcode - 739. 每日温度

vagrant 安装踩坑

ActiveMQ simple tutorial, suitable for beginners, learning notes yyds

leetcode - 230. The k-th smallest element in a binary search tree

Matlab learning 7- linear smoothing filtering of image processing

Can not connect to local MySQL server through socket ‘/tmp/mysql. sock (2)‘

Codetop - sort odd ascending even descending linked list

Matlab r2022a installation tutorial

Interprocess communication

Mazhiqiang: research progress and application of speech recognition technology -- RTC dev Meetup
随机推荐
E. X的放大与缩小(运算符重载)
CMVSS TSD No. 302与49 CFR 571.302测试方法是否一样
Display DIN 4102-1 Class B1 fire test requirements
leetcode - 230. 二叉搜索树中第K小的元素
Wood board ISO 5660-1 heat release rate mapping test
Mazhiqiang: research progress and application of speech recognition technology -- RTC dev Meetup
Create a nodejs based background service using express+mysql
【node】npm部分
【C语言-指针进阶】挖掘指针更深一层的知识
Matlab learning 7- linear smoothing filtering of image processing
标准化编写知识
(二)从架构设计角度分析AAC源码-我的LiveData
leetcode - 739. Daily temperature
面试官问了四个问题,总结了4个经验
20. 有效的括号
2022 Niuke winter vacation 3
MATLAB R2022a 安装教程
GCC AVR(Atmel Studio+ AVR Studio)如何将结构体数组定义在程序存储器(flash)空间并进行读操作
BS 7176软垫家具阻燃防火测试
leetcode - 230. The k-th smallest element in a binary search tree