1002.The Triangle

本文介绍如何设计一个名为Triangle的类,该类继承自GeometricObject类,并实现三角形面积和周长的计算。文章提供了错误示例与正确解答,强调了数据类型的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Time Limit: 1sec    Memory Limit:256MB
Description

Design a class named Triangle that extends GeometricObject class.
The class contains:
Three double data fields named side1side2, and side3 with default values 1.0 to denote three sides of the triangle.
A no-arg constructor that creates a default triangle with color = "blue", filled = true.
A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true.
The accessor functions for all three data fields, named getSide1()getSide2()getSide3().
A function named getArea() that returns the area of this triangle.
A function named getPerimeter() that returns the perimeter of the triangle.

class GeometricObject
{
public:
  GeometricObject(string color, bool filled)
  {
    this->color = color;
    this->filled = filled;
  }
  
  string getColor()
  { return color; }
  void setColor(string color)
  { this->color = color; }
  bool isFilled()
  { return filled; }
  void setFilled(bool filled)
  { this->filled = filled;}
  string toString()
  {
    return "Geometric object color " + color +
    " filled " + ((filled) ? "true" : "false");
  }
private:
  string color;
  bool filled;
};
 

Hint

Please submit the GeometricObject class and Triangle class.

Don't submit the main() function.


一发WA:

#include<iostream>
#include<cmath>
using namespace std; 
class GeometricObject
{
public:
  GeometricObject(){
  color="blue";
  filled=true;
}
  GeometricObject(string color, bool filled)
  {
    this->color = color;
    this->filled = filled;
  }
  
  string getColor()
  { return color; }
  void setColor(string color)
  { this->color = color; }
  bool isFilled()
  { return filled; }
  void setFilled(bool filled)
  { this->filled = filled;}
  string toString()
  {
    return "Geometric object color " + color +
    " filled " + ((filled) ? "true" : "false");
  }
private:
  string color;
  bool filled;
};
class Triangle: public GeometricObject{
private:
        double side1,side2,side3;
        //string color;
       // bool filled;
public:
        Triangle(){
        //color = "blue";
        //filled = true;
        side1=1.0;
        side2=1.0;
        side3=1.0;
        }
        Triangle(int s1,int s2,int s3/*,const string &color="blue",bool filled="true"*/){
        side1=s1;
        side2=s2;
        side3=s3;
        //this->color=color;
        //this->filled=filled;
        }
        Triangle(Triangle &T){
        side1=T.side1;
        side2=T.side2;
        side3=T.side3;
        //color=T.color;
        //filled=T.filled;
        }
        double getSide1(){
        return side1;
        }
        double getSide2(){
        return side2;
        }
        double getSide3(){
        return side3;
        }
        double getArea(){
        double p=(side1+side2+side3)/2.0;
        return pow(p*(p-side1)*(p-side2)*(p-side3),0.5);
        }
        double getPerimeter(){
        return side1+side2+side3;
        }
       /* string toString()
  {
    return "Triangle object color " + color +
    " filled " + ((this->isFilled()) ? "true" : "false");
  }*/

};                                 


正解:

#include<iostream>
#include<cmath>
using namespace std; 
class GeometricObject
{
public:

  GeometricObject(string color, bool filled)
  {
    this->color = color;
    this->filled = filled;
  }
  
  string getColor()
  { return color; }
  void setColor(string color)
  { this->color = color; }
  bool isFilled()
  { return filled; }
  void setFilled(bool filled)
  { this->filled = filled;}
  string toString()
  {
    return "Geometric object color " + color +
    " filled " + ((filled) ? "true" : "false");
  }
private:
  string color;
  bool filled;
};
class Triangle: public GeometricObject{
private:
        double side1,side2,side3;
public:
        Triangle():GeometricObject("blue",true){
        
        side1=1.0;
        side2=1.0;
        side3=1.0;
        }
        Triangle(double s1,double s2,double s3):GeometricObject("blue",true){
        side1=s1;
        side2=s2;
        side3=s3;
      
        }
        double getSide1(){
        return side1;
        }
        double getSide2(){
        return side2;
        }
        double getSide3(){
        return side3;
        }
        double getArea(){
        double p=(side1+side2+side3)/2.0;
        return sqrt(p*(p-side1)*(p-side2)*(p-side3));
        }
        double getPerimeter(){
        return side1+side2+side3;
        }
};



conclusion:醉了。。。一个int double 纠结了一下午,WA还以为是算法错了,结果是数据类型弄错。以后还是多注意一点吧

Time Limit: 1sec    Memory Limit:256MB
Description

Design a class named Triangle that extends GeometricObject class.
The class contains:
Three double data fields named side1side2, and side3 with default values 1.0 to denote three sides of the triangle.
A no-arg constructor that creates a default triangle with color = "blue", filled = true.
A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true.
The accessor functions for all three data fields, named getSide1()getSide2()getSide3().
A function named getArea() that returns the area of this triangle.
A function named getPerimeter() that returns the perimeter of the triangle.

class GeometricObject
{
public:
  GeometricObject(string color, bool filled)
  {
    this->color = color;
    this->filled = filled;
  }
  
  string getColor()
  { return color; }
  void setColor(string color)
  { this->color = color; }
  bool isFilled()
  { return filled; }
  void setFilled(bool filled)
  { this->filled = filled;}
  string toString()
  {
    return "Geometric object color " + color +
    " filled " + ((filled) ? "true" : "false");
  }
private:
  string color;
  bool filled;
};
 

Hint

Please submit the GeometricObject class and Triangle class.

Don't submit the main() function.

o3d.visualization.draw_geometries([pcd] + combined_arrows + [point]) TypeError: draw_geometries(): incompatible function arguments. The following argument types are supported: 1. (geometry_list: List[open3d.cpu.pybind.geometry.Geometry], window_name: str = 'Open3D', width: int = 1920, height: int = 1080, left: int = 50, top: int = 50, point_show_normal: bool = False, mesh_show_wireframe: bool = False, mesh_show_back_face: bool = False) -> None 2. (geometry_list: List[open3d.cpu.pybind.geometry.Geometry], window_name: str = 'Open3D', width: int = 1920, height: int = 1080, left: int = 50, top: int = 50, point_show_normal: bool = False, mesh_show_wireframe: bool = False, mesh_show_back_face: bool = False, lookat: numpy.ndarray[numpy.float64[3, 1]], up: numpy.ndarray[numpy.float64[3, 1]], front: numpy.ndarray[numpy.float64[3, 1]], zoom: float) -> None Invoked with: [PointCloud with 5 points., TriangleMesh with 124 points and 240 triangles., TriangleMesh with 124 points and 240 triangles., TriangleMesh with 124 points and 240 triangles., TriangleMesh with 124 points and 240 triangles., TriangleMesh with 124 points and 240 triangles., TriangleMesh with 124 points and 240 triangles., TriangleMesh with 124 points and 240 triangles., TriangleMesh with 124 points and 240 triangles., TriangleMesh with 124 points and 240 triangles., TriangleMesh with 124 points and 240 triangles., TriangleMesh with 124 points and 240 triangles., TriangleMesh with 124 points and 240 triangles., TriangleMesh with 124 points and 240 triangles., TriangleMesh with 124 points and 240 triangles., TriangleMesh with 124 points and 240 triangles., array([4., 0., 0.], dtype=float32)]
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值