1002.The Triangle



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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值