/*
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 张鲁
* 完成日期: 2012 年 4月 1日
* 版 本 号:
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 问题分析:……
* 算法设计:……
*/
Triangle.h
- using namespace std;
- class CPoint
- {
- private:
- double x; // 横坐标
- double y; // 纵坐标
- public:
- CPoint(double xx=1,double yy=2):x(xx),y(yy){}
- double Distance(CPoint p) const; // 两点之间的距离
- void input(); //以x,y 形式输入坐标点
- void output(); //以(x,y) 形式输出坐标点
- };
- 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; //三顶点
- double a,b,c; //三条边
- };
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; //三顶点
double a,b,c; //三条边
};main.cpp
- #include<iostream>
- #include"Triangle.h"
- using namespace std;
- void main()
- {
- CPoint a1;
- CPoint a2;
- CPoint a3;
- a1.input();
- a2.input();
- a3.input();
- cout<<"三角形的三个顶点为:"<<endl;
- a1.output();
- a2.output();
- a3.output();
- CTriangle A(a1,a2,a3);
- a1.Distance(a2);
- a2.Distance(a3);
- a1.Distance(a3);
- A.perimeter();
- A.area();
- cout<<"该三角形"<<(A.isRightTriangle()?"是":"不是")<<"直角三角形。"<<endl;
- cout<<"该三角形"<<(A.isIsoscelesTriangle()?"是":"不是")<<"等腰三角形。"<<endl;
- system("pause");
- }
CPoint.cpp
- #include<iostream>
- #include<cmath>
- #include"Triangle.h"
- using namespace std;
- // 求边长
- double CPoint::Distance(CPoint p) const
- {
- double l;
- l = sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
- return l;
- }
- //以x,y 形式输入坐标点
- void CPoint::input()
- {
- char a;
- cout<<"请以“x,y ”形式输入坐标点:"<<endl;
- cin>>x>>a>>y;
- if(a!=',')
- {
- exit(0);
- }
- }
- //以(x,y) 形式输出坐标点
- void CPoint::output()
- {
- cout<<"("<<x<<","<<y<<")"<<endl;
- }
// 求边长
double CPoint::Distance(CPoint p) const
{
double l;
l = sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
return l;
}
//以x,y 形式输入坐标点
void CPoint::input()
{
char a;
cout<<"请以“x,y ”形式输入坐标点:"<<endl;
cin>>x>>a>>y;
if(a!=',')
{
exit(0);
}
}
//以(x,y) 形式输出坐标点
void CPoint::output()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
CTriangle.cpp
- #include<iostream>
- #include<cmath>
- #include"Triangle.h"
- using namespace std;
- void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z)
- {
- A = X;
- B = Y;
- C = Z;
- }
- //计算三角形的周长
- float CTriangle::perimeter(void)
- {
- a = A.Distance(B);
- b = B.Distance(C);
- c = A.Distance(C);
- cout<<"三角形的周长为:"<<endl;
- cout<<a+b+c<<endl;
- return 0;
- }
- //计算并返回三角形的面积
- float CTriangle::area(void)
- {
- double p;
- p = (a +b +c)/2;
- a = A.Distance(B);
- b = B.Distance(C);
- c = A.Distance(C);
- cout<<"三角形的面积为:"<<endl;
- cout<<sqrt(p*(p-a)*(p-b)*(p-c))<<endl;
- return 0;
- }
- //是否为直角三角形
- bool CTriangle::isRightTriangle()
- {
- if(a*a+b*b == c*c || a*a+c*c == b*b || b*b+c*c == a*a)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //是否为等腰三角形
- bool CTriangle::isIsoscelesTriangle()
- {
- if(a==b||a==c||b==c)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z)
{
A = X;
B = Y;
C = Z;
}
//计算三角形的周长
float CTriangle::perimeter(void)
{
a = A.Distance(B);
b = B.Distance(C);
c = A.Distance(C);
cout<<"三角形的周长为:"<<endl;
cout<<a+b+c<<endl;
return 0;
}
//计算并返回三角形的面积
float CTriangle::area(void)
{
double p;
p = (a +b +c)/2;
a = A.Distance(B);
b = B.Distance(C);
c = A.Distance(C);
cout<<"三角形的面积为:"<<endl;
cout<<sqrt(p*(p-a)*(p-b)*(p-c))<<endl;
return 0;
}
//是否为直角三角形
bool CTriangle::isRightTriangle()
{
if(a*a+b*b == c*c || a*a+c*c == b*b || b*b+c*c == a*a)
{
return true;
}
else
{
return false;
}
}
//是否为等腰三角形
bool CTriangle::isIsoscelesTriangle()
{
if(a==b||a==c||b==c)
{
return true;
}
else
{
return false;
}
}
运行结果:
上机感言:一定要赶上,不能拉下 。 加油
/*
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:
* 作 者: 张鲁
* 完成日期: 2012 年 4月 1日
* 版 本 号:
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 问题分析:……
* 算法设计:……
*/
Triangle.h
- using namespace std;
- class CPoint
- {
- private:
- double x; // 横坐标
- double y; // 纵坐标
- public:
- CPoint(double xx=1,double yy=2):x(xx),y(yy){}
- double Distance(CPoint p) const; // 两点之间的距离
- void input(); //以x,y 形式输入坐标点
- void output(); //以(x,y) 形式输出坐标点
- };
- 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; //三顶点
- double a,b,c; //三条边
- };
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; //三顶点
double a,b,c; //三条边
};main.cpp
- #include<iostream>
- #include"Triangle.h"
- using namespace std;
- void main()
- {
- CPoint a1;
- CPoint a2;
- CPoint a3;
- a1.input();
- a2.input();
- a3.input();
- cout<<"三角形的三个顶点为:"<<endl;
- a1.output();
- a2.output();
- a3.output();
- CTriangle A(a1,a2,a3);
- a1.Distance(a2);
- a2.Distance(a3);
- a1.Distance(a3);
- A.perimeter();
- A.area();
- cout<<"该三角形"<<(A.isRightTriangle()?"是":"不是")<<"直角三角形。"<<endl;
- cout<<"该三角形"<<(A.isIsoscelesTriangle()?"是":"不是")<<"等腰三角形。"<<endl;
- system("pause");
- }
CPoint.cpp
- #include<iostream>
- #include<cmath>
- #include"Triangle.h"
- using namespace std;
- // 求边长
- double CPoint::Distance(CPoint p) const
- {
- double l;
- l = sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
- return l;
- }
- //以x,y 形式输入坐标点
- void CPoint::input()
- {
- char a;
- cout<<"请以“x,y ”形式输入坐标点:"<<endl;
- cin>>x>>a>>y;
- if(a!=',')
- {
- exit(0);
- }
- }
- //以(x,y) 形式输出坐标点
- void CPoint::output()
- {
- cout<<"("<<x<<","<<y<<")"<<endl;
- }
// 求边长
double CPoint::Distance(CPoint p) const
{
double l;
l = sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
return l;
}
//以x,y 形式输入坐标点
void CPoint::input()
{
char a;
cout<<"请以“x,y ”形式输入坐标点:"<<endl;
cin>>x>>a>>y;
if(a!=',')
{
exit(0);
}
}
//以(x,y) 形式输出坐标点
void CPoint::output()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
CTriangle.cpp
- #include<iostream>
- #include<cmath>
- #include"Triangle.h"
- using namespace std;
- void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z)
- {
- A = X;
- B = Y;
- C = Z;
- }
- //计算三角形的周长
- float CTriangle::perimeter(void)
- {
- a = A.Distance(B);
- b = B.Distance(C);
- c = A.Distance(C);
- cout<<"三角形的周长为:"<<endl;
- cout<<a+b+c<<endl;
- return 0;
- }
- //计算并返回三角形的面积
- float CTriangle::area(void)
- {
- double p;
- p = (a +b +c)/2;
- a = A.Distance(B);
- b = B.Distance(C);
- c = A.Distance(C);
- cout<<"三角形的面积为:"<<endl;
- cout<<sqrt(p*(p-a)*(p-b)*(p-c))<<endl;
- return 0;
- }
- //是否为直角三角形
- bool CTriangle::isRightTriangle()
- {
- if(a*a+b*b == c*c || a*a+c*c == b*b || b*b+c*c == a*a)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- //是否为等腰三角形
- bool CTriangle::isIsoscelesTriangle()
- {
- if(a==b||a==c||b==c)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z)
{
A = X;
B = Y;
C = Z;
}
//计算三角形的周长
float CTriangle::perimeter(void)
{
a = A.Distance(B);
b = B.Distance(C);
c = A.Distance(C);
cout<<"三角形的周长为:"<<endl;
cout<<a+b+c<<endl;
return 0;
}
//计算并返回三角形的面积
float CTriangle::area(void)
{
double p;
p = (a +b +c)/2;
a = A.Distance(B);
b = B.Distance(C);
c = A.Distance(C);
cout<<"三角形的面积为:"<<endl;
cout<<sqrt(p*(p-a)*(p-b)*(p-c))<<endl;
return 0;
}
//是否为直角三角形
bool CTriangle::isRightTriangle()
{
if(a*a+b*b == c*c || a*a+c*c == b*b || b*b+c*c == a*a)
{
return true;
}
else
{
return false;
}
}
//是否为等腰三角形
bool CTriangle::isIsoscelesTriangle()
{
if(a==b||a==c||b==c)
{
return true;
}
else
{
return false;
}
}
运行结果:
上机感言:一定要赶上,不能拉下 。 加油