以下是由点组成的正方形25 x 25网格的代码。我需要从用户输入x1,x2,y1,y2和两个字符(如@和/),并创建一个矩形,其中一个字符填充内部,一个字符勾勒出矩形。我不确定如何将这些输入工作到循环中并创建所述矩形。 这是头文件....
// Rectangle.h
#ifndef Rectangle_h
#define Rectangle_h
#include <iostream>
using namespace std;
class Rectangle
{
private:
float length, width, perimeter, area;
int a, i, j, h, x1, x2, y1, y2;
char inner;
public:
Rectangle(); // default constructor
void printGrid();
void setValues();
void setIntChar();
};
#endif
这是源代码....
void Rectangle::setValues()
{
do
{
cout << "Enter x1, x2, y1, and y2 such that x1 < x2 and y1 < y2:\n";
cin >> x1;
cin >> x2;
cin >> y1;
cin >> y2;
}
while(x1 >= x2 || y1 >= y2);
cout << endl << "Rectangle accepted.\n\n";
}
void Rectangle::setIntChar()
{
cout << "Enter a character to fill the interior of the rectangle: \n" << endl;
cin >> inner;
cout << endl << endl;
}
void Rectangle::printGrid()
{
for(i = 0; i < 25; i++) // prints 25 rows of 25 dots and a space after each dot
{
if(i < 16)
cout << 25 - i << " ";// prints 25 - 10 in descending order
if(i > 15)
cout << 25 - i << " "; // prints 9 - 1 in descending order
for(j = 0; j < 25; j++) // prints a line of 25 dots and 2 spaces after each dot
cout << ". ";
for(h = 0; h < 25; h++) // prints a line of 25 double-spaces
cout << " ";
cout << endl; // ends the line after each iteration
}
cout << " ";
for (i = 1; i <= 25; i++)
{
if(i < 10)
cout << i << " ";
if(i > 9)
cout << i << " ";
}
cout << endl << endl;
}
这是主要功能......
#include "Rectangle.h"
///// main /////
int main()
{
Rectangle grid; // creates an object of the Rectangle class
grid.setValues();
grid.setIntChar();
grid.printGrid();
}
答案 0 :(得分:0)
好吧,我不知道你学到的C ++有多远,但看起来你正在使用类?什么都是Rectangle类的成员变量?据推测它就像
class Rectangle {
public:
Rectangle(); // constructor sets member variables to default values
void getInputFromUser(); // using stdout and stdin, prompts the user for x1, y1, x2, y2, line char, fill char, and sets the member variables to these values
void printGrid(); // uses member variables to determine how to print the grid
...
private:
// private methods
...
// private variables
int m_x1;
int m_x2;
int m_y1;
int m_y2;
char m_lineChar;
char m_fillChar;
// static constants
static const int kMinXValue; // set to 0 in the cpp file
static const int kMinYValue; // set to 0 in the cpp file
static const int kMaxXValue; // set to 25 in the cpp file
static const int kMaxYValue; // set to 25 in the cpp file
};
因此,在您的课程中,您的getInputFromUser()实现将包含一些代码行,如cout << "Please enter the top left corner's x value << endl;
和cin >> m_x1;
。
printGrid()的实现将包含一些代码行,如for(int y = kMinYValue; y < kMaxYValue; y++) { // iterate through the rows
和if( y == m_y1 || y == m_y2) { // we need to print the m_lineChar value if the x value is in range
希望能够回答你的问题,足以让你解决问题。如果您需要针对特定问题的特定帮助,则需要更加具体地了解您的工作,代码是什么以及您的代码存在问题。