6週目-プロジェクト4-三角形クラス
2663 ワード
/*
*
* Copyright (c)2013,
* All rightsreserved.
* : object.cpp
.* :
* : 2013 4 10
* : v1.0
* :
* : 。
* : , , , 。
*/
#include <iostream>
#include<Cmath>
using namespace std;
class CPoint
{private:
double x; //
double y; //
public:
CPoint(double xx=0,double yy=0);
void input();
double Distance(CPoint p) const; // ( , p)
void output(); // (x,y)
};
CPoint::CPoint(double xx,double yy)
{
x=xx;
y=yy;
}
void CPoint::input()
{
char c;
cin>>x>>c>>y;
if(c!=',')
{
cout<<" , ";
exit(0);
}
}
double CPoint::Distance(CPoint p) const // ( , p)
{
double s;
s=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
return s;
}
class CTriangle
{
public:
CTriangle(CPoint &X,CPoint &Y,CPoint &Z):A(X),B(Y),C(Z){} //
void setTriangle(CPoint &X,CPoint &Y,CPoint &Z);//
float perimeter(void);//
float area(void);//
bool isRightTriangle(); //
bool isIsoscelesTriangle(); //
private:
CPoint A,B,C; //
};
void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z)//
{
A=X;
B=Y;
C=Z;
}
float CTriangle::perimeter(void)//
{
float a,b,c,l;
a=A.Distance(B);
b=B.Distance(C);
c=C.Distance(A);
l=a+b+c;
return l;
}
float CTriangle::area(void)//
{
float a,b,c,p,s;
a=A.Distance(B);
b=B.Distance(C);
c=C.Distance(A);
p=(a+b+c)/2;
s=sqrt(p*(p-a)*(p-b)*(p-c));
return s;
}
bool CTriangle::isRightTriangle()//
{
double a,b,c;
a=A.Distance(B);
b=B.Distance(C);
c=C.Distance(A);
bool prime=false;
if((a*a+b*b==c*c)||(a*a+c*c==b*b)||(b*b+c*c==a*a))
{
prime=true;
}
return prime;
}
bool CTriangle::isIsoscelesTriangle() //
{
double a,b,c;
a=A.Distance(B);
b=B.Distance(C);
c=C.Distance(A);
bool prime=false;
if(a==b||a==c||b==c)
{
prime=true;
}
return prime;
}
int main()
{
CPoint x,y,z;
x.input();
y.input();
z.input();
CTriangle tri(x,y,z); //
cout<<" :"<<tri.perimeter()<<", :"<<tri.area()<<endl;
cout<<" "<<(tri.isRightTriangle()?" ":" ")<<" "<<endl;
cout<<" "<<(tri.isIsoscelesTriangle()?" ":" ")<<" "<<endl;
return 0;
}
結果:
感じ:2つのクラスを定義して、構造関数をコピーします!!!