使用类和函数来提供有关矩形的信息

Use of class and functions to provide info about rectangle

本文关键字:信息 提供有 函数      更新时间:2023-10-16

我在C++课程中的最后一项任务是编写这个程序:

读取矩形的名称,只接受类似"rec"名称的名称。对于例如"rec john"rec sally"
然后从左下角的x点开始询问y以及高度和长度。然后显示一些信息,包括BL点、区域、周长,。。。

这是我写的代码。我已经做了很长时间了,所以现在我真的看不出它有什么问题。所以请帮忙。

我的代码:

#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 scaleBy2();
    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 2:";
        list[i].scaleBy2();
        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*2)+(length*2));
}
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::scaleBy2()
{
    double mx = blPoint.getX() + 0.5 * length;
    double my = blPoint.getY() + 0.5 * height;
    double newmdx = mx - length;
    double newmdy = my - height;
    length= 2* length;
    height = 2* 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;
}

这是正确输入的正确输出:

Welcome! Create your own list of rectangles.
You will be asked to provide information about each rectangle in your list by name.
Type the word 'stop' for the rectangle name when you are done.
Enter the name of the first rectangle: rec john
Enter john's bottom left x and y coords: 2
3
Enter john's length and height: 4
5
Thank you! Enter the name of the next rectangle: stop
You have 1 rectangle(s) in your list:
Rectangle 'john': Location is (2, 3), length is 4, height is 5; Area is 20, perimeter is 18, midpoint is located at (4, 5.5)
     After scale by 2: Location is (0, 0.5), length is 8, height is 10; Area is 80, perimeter is 36, midpoint is located at (4, 5.5)

这是我的程序的响应:

Welcome! Create your own list of rectangles.
You will be asked to provide information about each rectangle in your list by na
me.
Type the word 'stop' for the rectangle name when you are done.
Enter the name of the first rectangle: rec john
Enter john's bottom left x and y coords: 2
3
Enter john's length and height: 4
5
Thank you! Enter the name of the next rectangle: Invalid input. Type 'rec' follo
wing 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 'john':  Location is (2, 3), length is 4, height is 5; Area is 20; per
imeter is 18, midpoint is located at (4, 5.5)
After scale by 2: Location is (0, 0.5), length is 8, height is 10; Area is 80; p
erimeter is 36, midpoint is located at (4, 5.5)

正如你所看到的,这个程序唯一的问题是它在询问下一个矩形的名称和信息之前显示errInvalidName。怎么了?

您的问题在于cin >>getline的组合。这两件事不太兼容,因为它们使用不同的机制来获取输入。要解决此问题,在程序中的每个cin >>之后,还应该添加cin.ignore()

这两种方法的区别在于,重载的operator >>在换行符之前获取内容,而将换行符留在缓冲区中。在执行>>之后使用getline时,提取空行。空行不是"stop",也不是以"rec"开头,所以您的程序会提示一个错误。

相关文章: