根据以下题目写出编写程序重可能遇到的问题及解决方案设计自定义异常类NotTriangleException;设计Triangle(三角形)类; (1)Triangle类基本信息:三个顶点坐标; (2)带三个顶点坐标参数的构造函数;当传入实参为不合理数据时(例如:输入的三角形的顶点不能组成一个三角形),抛出自定义异常类NotTriangleException的对象; (3)有返回三角形面积的函数getArea()。 (4)各个类的声明放在与类同名的头文件中(*.h),类的实现放在与类同名的源文件中(*.cpp); (5)只能在主函数中输入输出,各个类的成员函数中不能进行输入输出。 (6)在主函数中,接收用户输入的三角形三个顶点坐标,然后,传给三角形对象的构造函数,并捕获NotTriangleException异常对象。当捕获到异常后,提示用户输入数据错误,并要求用户重新输入。
时间: 2023-11-28 11:49:22 浏览: 40
编写程序可能遇到的问题及解决方案:
1. 输入的顶点坐标不符合要求,无法构成三角形。
解决方案:在Triangle类的构造函数中判断输入的三个顶点坐标是否能构成三角形。如果不能构成三角形,则抛出自定义异常类NotTriangleException的对象。
2. 无法计算三角形的面积。
解决方案:在Triangle类中计算三角形的面积时,需要确保输入的三个顶点坐标能构成三角形。如果不能构成三角形,则无法计算面积。
3. 用户输入错误的数据,需要重新输入。
解决方案:在主函数中,捕获NotTriangleException异常对象后,提示用户输入数据错误,并要求用户重新输入。
下面是可能的代码实现:
NotTriangleException.h:
```c++
#ifndef NOT_TRIANGLE_EXCEPTION_H
#define NOT_TRIANGLE_EXCEPTION_H
#include <exception>
class NotTriangleException : public std::exception {
public:
const char* what() const noexcept override {
return "The input cannot form a triangle!";
}
};
#endif // NOT_TRIANGLE_EXCEPTION_H
```
Triangle.h:
```c++
#ifndef TRIANGLE_H
#define TRIANGLE_H
class Triangle {
public:
Triangle(double x1, double y1, double x2, double y2, double x3, double y3);
double getArea() const;
private:
double m_x1, m_y1, m_x2, m_y2, m_x3, m_y3;
bool isValid() const;
};
#endif // TRIANGLE_H
```
Triangle.cpp:
```c++
#include "Triangle.h"
#include "NotTriangleException.h"
#include <cmath>
Triangle::Triangle(double x1, double y1, double x2, double y2, double x3, double y3)
: m_x1(x1), m_y1(y1), m_x2(x2), m_y2(y2), m_x3(x3), m_y3(y3)
{
if (!isValid()) {
throw NotTriangleException();
}
}
double Triangle::getArea() const
{
double a = std::sqrt(std::pow(m_x2 - m_x1, 2) + std::pow(m_y2 - m_y1, 2));
double b = std::sqrt(std::pow(m_x3 - m_x2, 2) + std::pow(m_y3 - m_y2, 2));
double c = std::sqrt(std::pow(m_x1 - m_x3, 2) + std::pow(m_y1 - m_y3, 2));
double s = (a + b + c) / 2;
return std::sqrt(s * (s - a) * (s - b) * (s - c));
}
bool Triangle::isValid() const
{
double a = std::sqrt(std::pow(m_x2 - m_x1, 2) + std::pow(m_y2 - m_y1, 2));
double b = std::sqrt(std::pow(m_x3 - m_x2, 2) + std::pow(m_y3 - m_y2, 2));
double c = std::sqrt(std::pow(m_x1 - m_x3, 2) + std::pow(m_y1 - m_y3, 2));
return (a + b > c && a + c > b && b + c > a);
}
```
main.cpp:
```c++
#include <iostream>
#include "Triangle.h"
#include "NotTriangleException.h"
int main()
{
double x1, y1, x2, y2, x3, y3;
while (true) {
std::cout << "Please enter the coordinates of three vertices of a triangle: ";
std::cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
try {
Triangle t(x1, y1, x2, y2, x3, y3);
std::cout << "The area of the triangle is " << t.getArea() << std::endl;
break;
} catch (const NotTriangleException& e) {
std::cerr << e.what() << std::endl;
}
}
return 0;
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)