C++轴对齐矩形操作向量和字符串 嘿,我的C++作业分配有问题,我花了大量时间研究它,试图找出我的错误。家庭作业要求您编写一个程序,读取用户输入矩形。程序将要求输入以rec name开头的矩形名称,即矩形John将作为rec John输入、左下角的坐标、矩形的长度和矩形的高度。程序将把它放在一个列表中,并不断要求更多的矩形,直到用户输入矩形的名称作为停止。程序将返回每个矩形的输入信息,然后将矩形缩放3。它将返回缩放矩形的新左下角坐标、中点、矩形面积、周长、高度和长度
这是我到目前为止得到的C++轴对齐矩形操作向量和字符串 嘿,我的C++作业分配有问题,我花了大量时间研究它,试图找出我的错误。家庭作业要求您编写一个程序,读取用户输入矩形。程序将要求输入以rec name开头的矩形名称,即矩形John将作为rec John输入、左下角的坐标、矩形的长度和矩形的高度。程序将把它放在一个列表中,并不断要求更多的矩形,直到用户输入矩形的名称作为停止。程序将返回每个矩形的输入信息,然后将矩形缩放3。它将返回缩放矩形的新左下角坐标、中点、矩形面积、周长、高度和长度,c++,string,class,vector,C++,String,Class,Vector,这是我到目前为止得到的 #include <iostream> #include <string> #include <vector> using namespace std; class Point { private: double px; double py; public: void setX(const double x); void setY(const double y); double ge
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Point
{
private:
double px;
double py;
public:
void setX(const double x);
void setY(const double y);
double getX() const;
double getY() const;
};
class Rectangle
{
private:
string name;
Point blPoint;
double length, height;
public:
void setName(const string & inName);
void setBottomLeft(const double x, const double y);
void setDimensions(const double inLength, const double inHeight);
string getName() const;
Point getBottomLeft() const;
double getLength() const;
double getHeight() const;
double area() const;
double perimeter() const;
Point midPoint() const;
void scaleBy3();
void display() const;
};
void welcome();
bool read_rect (const string promptName, const string errInvalidName, const string errUsedName, string & inName, vector<Rectangle> & list);
void readXYcoord (const string promptPointxy, double & xcord, double & ycord);
void readLH (const string promptLH, double & inLength, double & inHeight);
void addRect (const string Name, double x, double y, double inLength, double inHeight, vector<Rectangle> & list);
void dis_rec(vector<Rectangle> & list);
int main()
{
Rectangle rec;
vector<Rectangle>list;
string prompt1stName = "Enter the name of the first rectangle: ";
string promptName = "Enter the name of the next rectangle: ";
string errInvalidName = "Invalid input. Type 'rec' following by the name or 'stop' if done.";
string errUsedName = "This name is already being used!";
string inName;
string Name;
double x,y,length,height;
welcome ();
bool read = read_rect (prompt1stName, errInvalidName, errUsedName, inName, list);
while (read == false)
{
cout << "Try again! ";
read = read_rect (prompt1stName, errInvalidName, errUsedName, inName, list);
}
if (inName != "stop")
{
int a = inName.length() - 4;
Name = inName.substr(4,a);
double x, y;
string promptPointxy = "Enter " + Name + "'s bottom left x and y coords: ";
readXYcoord (promptPointxy, x, y);
double length, height;
string promptLH= "Enter " + Name + "'s length and height: ";
readLH (promptLH, length, height);
addRect(Name, x, y, length, height, list);
}
while (inName !="stop")
{
cout << "Thank you! ";
bool read = read_rect(promptName, errInvalidName, errUsedName, inName, list);
while (read == false)
{
cout << "Try again! " <<endl;
read = read_rect(promptName, errInvalidName, errUsedName, inName, list);
}
if (inName != "stop")
{
int a = inName.length() - 4;
Name = inName.substr(4, a);
double x, y;
string promptPoint = "Enter " + Name + "'s bottom left x and y coords: ";
readXYcoord(promptPoint, x, y);
double inLength, inHeight;
string promptLength = "Enter " + Name + "'s length and height: ";
readLH(promptLength, inLength, inHeight);
addRect(Name, x, y, inLength, inHeight, list);
}
}
if (list.size() != 0)
{
dis_rec(list);
}
else
{
cout << "You have no rectangles in your list." << endl;
}
return 0;
}
void welcome()
{
cout << "Welcome! Create your own list of rectangles." << endl;
cout << "You will be asked to provide information about each rectangle in your list by name." << endl;
cout << "Type the word 'stop' for the rectangle name when you are done." << endl;
cout << endl;
}
bool read_rect (const string promptName, const string errInvalidName, const string errUsedName, string & inName, vector<Rectangle> & list)
{
cout << promptName;
getline(cin, inName);
if (inName == "stop")
{
return (true);
}
else if (inName.substr(0,4) != "rec ")
{
cout<< errInvalidName <<endl;
return (false);
}
else
{
int j = 0;
for (int i = 0; i < list.size(); i++)
{
if (inName == "rec " + list[i].getName())
{
j = j+1;
}
}
if (j == 0)
{
return(true);
}
if (j != 0)
{
cout << errUsedName;
return(false);
}
}
}
void readXYcoord (const string promptPointxy, double & xcord, double & ycord)
{
cout << promptPointxy;
cin >> xcord;
cin >> ycord;
}
void readLH (const string promptLH, double & inLength, double & inHeight)
{
cout<< promptLH;
cin >> inLength;
cin >> inHeight;
cout << endl;
while (inLength <= 0 || inHeight <= 0)
{
cout << "Make length and height positive values. Try again.";
cout << promptLH;
cin >> inLength;
cin >> inHeight;
cout << endl;
}
}
void addRect (const string Name, double x, double y, double inLength, double inHeight, vector<Rectangle> & list)
{
Rectangle rec;
rec.setName(Name);
rec.setBottomLeft(x, y);
rec.setDimensions(inLength, inHeight);
list.push_back(rec);
}
void dis_rec(vector<Rectangle> & list)
{
cout<<"You have "<<list.size()<<" rectangle(s) in your list: "<<endl;
for(int i=0; i<list.size(); i++)
{
cout<<"Rectangle '"<<list[i].getName()<<"': ";
list[i].display();
cout<<"After scale by 3:";
list[i].scaleBy3();
list[i].display();
}
}
void Point::setX(const double x)
{
px = x;
}
void Point::setY(const double y)
{
py = y;
}
double Point::getX() const
{
return (px);
}
double Point::getY() const
{
return (py);
}
void Rectangle::setName(const string & inName)
{
name = inName;
}
void Rectangle::setBottomLeft(const double x, const double y)
{
blPoint.setX(x);
blPoint.setY(y);
}
void Rectangle::setDimensions(const double inLength, const double inHeight)
{
length = inLength;
height = inHeight;
}
string Rectangle::getName() const
{
return (name);
}
Point Rectangle::getBottomLeft() const
{
return (blPoint);
}
double Rectangle::getLength() const
{
return (length);
}
double Rectangle::getHeight() const
{
return (height);
}
double Rectangle::area() const
{
return(length*height);
}
double Rectangle::perimeter() const
{
return ( (height*3)+(length*3));
}
Point Rectangle::midPoint() const
{
Point midPoint;
double mpx = blPoint.getX() + 0.5 * length;
double mpy = blPoint.getY() + 0.5 * height;
midPoint.setX(mpx);
midPoint.setY(mpy);
return(midPoint);
}
void Rectangle::scaleBy3()
{
double mx = blPoint.getX() + 0.5 * length;
double my = blPoint.getY() + 0.5 * height;
double newmdx = mx - length;
double newmdy = my - height;
length= 3* length;
height = 3* height;
blPoint.setX(newmdx);
blPoint.setY(newmdy);
}
void Rectangle::display() const
{
cout << " Location is (" << blPoint.getX() << ", " << blPoint.getY() << "), length is " << length << ", height is " << height << "; Area is " << area() << "; perimeter is " << perimeter() << ", midpoint is located at (" << midPoint().getX() << ", " << midPoint().getY() << ")" << endl;
}
但结果是正确的
Enter the name of the first rectangle: rec hi
Enter hi's bottom left x and y coords: 1 1
Enter hi's length and height: 2 2
Thank you! Enter the name of the next rectangle: Invalid input. Type 'rec' following by the name or 'stop' if done.
Try again!
Enter the name of the next rectangle: stop
You have 1 rectangle(s) in your list:
Rectangle 'hi': Location is (1, 1), length is 2, height is 2; Area is 4; perimeter is 12, midpoint is located at (2, 2)
After scale by 3: Location is (0, 0), length is 6, height is 6; Area is 36; perimeter is 36, midpoint is located at (3, 3)
我非常感谢你的帮助。我真的不想再熬夜了,大部分时间我都做完了!非常感谢 如果要按中点缩放矩形,则函数中的逻辑不正确。试试这个:
void Rectangle::scaleBy3()
{
double mx = blPoint.getX() + 0.5 * length;
double my = blPoint.getY() + 0.5 * height;
length = 3 * length;
height = 3 * height;
// Mid point remains exactly at the same location.
// Move the lower left corner so that it is offset from
// the mid point by half the length and half the width.
blPoint.setX( mx - 0.5*length);
blPoint.setY( my - 0.5*height);
}
更新
您用来读取名称的逻辑有问题。看看要找出问题所在,代码太多了。请注意张贴AN。还要注意,当你缩放矩形时,你必须决定矩形的哪个点保持不变。是左下角,右上角,还是中点?如果没有这些,就有无限可能的方法来缩放矩形。对不起,我今天刚刚创建了一个帐户。我相信问题在于int main中的嵌套循环如果有帮助的话,矩形将在其中点处缩放3。我只是在读我的作业。是的,我意识到我的逻辑有点混乱。谢谢,但是我的布尔函数仍然有问题。如果你注意到我在原始问题中所说的话,我当前的程序将输出无效的输入并重试!当用户有有效的输入时。我不知道是什么原因导致我的while循环在主或如果它是bool函数本身。
void Rectangle::scaleBy3()
{
double mx = blPoint.getX() + 0.5 * length;
double my = blPoint.getY() + 0.5 * height;
length = 3 * length;
height = 3 * height;
// Mid point remains exactly at the same location.
// Move the lower left corner so that it is offset from
// the mid point by half the length and half the width.
blPoint.setX( mx - 0.5*length);
blPoint.setY( my - 0.5*height);
}