使用类和函数来提供有关矩形的信息
Use of class and functions to provide info about rectangle
我在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"开头,所以您的程序会提示一个错误。
相关文章:
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- C++有什么方法可以在既不调用函数模板也不提供其模板参数的情况下引用函数模板?
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- 提供运算符+或运算符到双向迭代器有什么缺点吗?
- VisualC++ 2010 有没有办法找出有关未处理异常错误的更多详细信息
- std::atomic_flag不提供加载或存储操作有什么缺点吗?(自旋锁定示例)
- 对于存储另一个类所需信息的类,例如其构造,是否有设计模式?
- gdb:在选定的帧上提供本地信息
- 如果您有Visual Studio 2017 Preview 2更新,您可以为我提供include/iosfwd文件吗?
- 当遇到不是来自 STL 的未捕获异常时,有没有办法打印其信息?
- 我可以为 C++ 类提供不完整的标头以隐藏实现详细信息吗?
- 是否有提供可调整 dpi、可选文本和 CSS 支持的 HTML 到 PDF 渲染器?
- 从带有 getline() 的文件读入一行上有多个信息得到无效参数错误
- 如果有一个几何设计着色器,则如何将信息从顶点着色器传递到碎片着色器
- 有没有办法根据用户的输入访问类对象的信息?
- 是否有一种简单的方法可以从普通的 win32 C++代码向应用程序见解提供数据?
- 有没有办法从字符串中提取某些信息?C++
- C++代码不打印信息,有什么问题?
- 使用基对象修改/访问派生类的信息的有问题的设计
- 当我向只接受无符号整数的函数提供有符号整数时,是否有一种方法可以使GCC警告我?