我使用二维 for 循环为类作业制作形状生成函数,但无法制作空心正方形;只有一个填充的。我传入两个参数,即宽度和所使用的符号。
我已经试过好几次了,但最终我只是在一个未完成的情况下上交了它。它应该打印这样的东西
++++
+ +
+ +
++++
#include <iostream>
#include "shapemaker.h"
using namespace std;
void shapemaker::Initialize(int w, int h, char sym)
{
width = w;
height = h;
symbol = sym;
}
int shapemaker::getcanWidth() {return width;}
int shapemaker::getcanHeight() {return height;}
char shapemaker::getSymbol() {return symbol;}
void shapemaker::setSymbol(char s) { symbol = s; }
void shapemaker::setcanWidth(int w) { width = w; }
void shapemaker::setcanHeight(int h) { height = h; }
void shapemaker::drawmidHorline()
{
symbol = getSymbol();
int drawingheight = getcanHeight();
int drawingwidth = getcanWidth();
double midpoint = getcanHeight()/2;
for(int x = 0; x < drawingwidth; x++)
{
for(int y = 0; y < drawingwidth; y++)
{
if(x == midpoint)
cout << getSymbol();
}
cout << endl;
}
}
void shapemaker::drawmidVertline()
{
symbol = getSymbol();
int drawingheight = getcanHeight();
int drawingwidth = getcanWidth();
double midpoint = getcanWidth()/2;
for(int x = 0; x < drawingwidth;x++)
{
for(int y = 0; y < drawingheight; y++)
{
if(y == midpoint)
{
cout << getSymbol();
}
cout << " ";
}
cout << endl;
}
}
void shapemaker::drawWidthsizedFullSquare()
{
symbol = getSymbol();
int drawingwidth = getcanWidth();
for(int x = 0; x < drawingwidth;x++)
{
cout << symbol;
for(int y = 0; y < drawingwidth; y++)
{
cout << symbol;
}
cout << endl;
}
}
void shapemaker::drawWidthsizedOpenSquare()
{
symbol = getSymbol();
int drawingwidth = getcanWidth();
for(int x = 0; x < drawingwidth;x++)
{
cout << getSymbol();
for(int y = 0; y < drawingwidth; y++)
{
if(x == 0 || x == drawingwidth-1)
cout << getSymbol();
}
cout << getSymbol();
cout << endl;
}
}
如果你想让drawWidthsizedOpenSquare
打印这个
++++
+ +
+ +
++++
你应该这样写这个方法:
void shapemaker::drawWidthsizedOpenSquare()
{
symbol = getSymbol();
int drawingwidth = getcanWidth();
for(int x = 0; x < drawingwidth;x++)
{
for(int y = 0; y < drawingwidth; y++)
{
if(x == 0 || x == drawingwidth-1 || y == 0 || y == drawingwidth-1) // border cell case
{
cout << getSymbol();
}
else
{
cout << " ";
}
}
cout << endl;
}
}
我是一名优秀的程序员,十分优秀!