C++ 轴对齐矩形操作向量和字符串

Posted

技术标签:

【中文标题】C++ 轴对齐矩形操作向量和字符串【英文标题】:C++ axis-aligned rectangle manipulation vectors and strings 【发布时间】:2015-04-24 03:24:14 【问题描述】:

嘿,我的 C++ 家庭作业有问题,我花了很多时间来解决我的错误。作业要求您编写一个读取用户输入矩形的程序。程序将询问以 rec "name" 开头的矩形的名称(即矩形 "John" 将输入为 rec John)、左下角的坐标、矩形的长度和矩形的高度.程序将把它放在一个列表中并不断要求更多的矩形,直到用户输入矩形的名称作为“停止”。程序将返回每个矩形的输入信息,然后将矩形缩放 3。它将返回缩放后矩形的新左下角坐标、中点、矩形面积、周长、高度和长度。

这是我到目前为止所得到的。

    #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: 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 8, Midpoint is located at (2, 2)
     After scale by 3: Location is (-1, -1), Length is 6, Height is 6; Area is 36, Perimeter is 24, Midpoint is located at (2, 2)

但是正确的输出是

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)

如果有任何帮助,我将不胜感激。我真的不想再拉一个通宵了,我已经完成了大部分!非常感谢!

【问题讨论】:

这是太多代码,无法找出问题所在。请考虑发布MCVE。 还要注意,当你缩放一个矩形时,你必须决定矩形的哪个点保持不变。是左下角,是右上角,还是中点?没有它,有无限可能的方法来缩放一个矩形。 对不起,我今天刚刚创建了一个帐户。我相信问题出在 int main() 中的嵌套循环中。 - 如果有帮助的话,矩形将围绕其中点缩放 3。刚读完我的作业。 【参考方案1】:

如果您打算将Rectangle 缩放到中点,那么您的函数中的逻辑是不正确的。试试这个:

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);

更新

您用来读取名称的逻辑有问题。看看How to allow user to do input more than twice?

【讨论】:

是的,我意识到我的逻辑有点混乱。谢谢,但我的布尔函数仍然有问题。如果您注意到我在原始问题中所说的话,我当前的程序将输出“无效输入”和“再试一次!”当用户将有有效的输入。我不确定是什么在我的 main 循环中导致了这种情况,或者它是否是 bool 函数本身。

以上是关于C++ 轴对齐矩形操作向量和字符串的主要内容,如果未能解决你的问题,请参考以下文章

如何找到将 X、Y、Z 向量与另一个坐标系对齐的角度

在javascript中获取最近的笛卡尔轴对齐向量

c_cpp 两个轴对齐矩形是否重叠,以及计算两个轴对齐矩形的重叠比例。具体算法说明,请参见http://blog.csdn.net/qianchenglenger/article/details/50

2022-10-09:我们给出了一个(轴对齐的)二维矩形列表 rectangles 。 对于 rectangle[i] = [x1, y1, x2, y2],其中(x1,y1)是矩形 i 左下角的坐

字符串向量和数组C++

华为机试真题 C++ 实现矩形相交的面积