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

时间:2015-04-24 03:24:15

标签: c++ string class vector

嘿所以我在完成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)

我很感激任何帮助。我真的不想拉另一个更黑的人,我已经完成了大部分工作!非常感谢!

1 个答案:

答案 0 :(得分:0)

如果你打算在中间点缩放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?