第四周项目3——用多文件组织多个类的程序
2015-04-06 14:27
357 查看
问题及代码:
main.cpp
CPoint.h
CPoint.cpp
CTriangle.h
CTriangle.cpp
运行结果:
main.cpp
/* *Copyright (c)2015,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:point.cpp *作 者:赵敏 *完成日期:2015年4月6日 *版 本 号:v1.0 * *问题描述:回想Engineer类的数据成员,有眼镜、背包等。某Engineer的眼镜、背包,是Glass、Bag类的对象。类中的数据成员,其类型可以是简单类型,也可以是类。通过这种方式,将某些类组合到另外的类中,当作其中的一个“部件”使用。 本项目设计一个三角形类,其数据成员不再是三角形三条边的边长,而是三角形的三个顶点。利用设计的三角形类,输入三角形的三个顶点,求出其面积、周长,并判断其是否为直角三角形和等腰三角形。 提示:(1)这个问题需要用到两个类,顶点类参照项目1中的CPoint类;(2)三角形类参考下面CTriangle类的声明;(3)充分利用CPoint类 */ #include <iostream> #include <cmath> #include "CPoint.h" #include "CTriangle.h" using namespace std; int main() { CPoint X(2,5),Y(5,5),Z(7,8); CTriangle Tri1(X,Y,Z);
cout<<"该三角形的周长为:"<<Tri1.perimeter()<<",面积为:"<<Tri1.area()<<endl<<endl; cout<<"该三角形"<<(Tri1.isRightTriangle()?"是":"不是")<<"直角三角形"<<endl; cout<<"该三角形"<<(Tri1.isIsoscelesTriangle()?"是":"不是")<<"等腰三角形"<<endl; return 0; }
CPoint.h
#ifndef CPOINT_H_INCLUDED #define CPOINT_H_INCLUDED class CPoint { private: double x; // 横坐标 double y; // 纵坐标 public: CPoint(double xx=0,double yy=0); double Distance1(CPoint p) const; //两点之间的距离(一点是当前点——想到this了吗?,另一点为p) void input(); //以x,y 形式输入坐标点 void output(); //以(x,y) 形式输出坐标点 }; #endif // CPOINT_H_INCLUDED
CPoint.cpp
#include <iostream> #include <cmath> #include "CPoint.h" using namespace std; CPoint::CPoint(double xx,double yy) { x=xx; y=yy; } double CPoint::Distance1(CPoint p) const { return sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y)); } void CPoint::input() { cin>>x>>y; } void CPoint::output() { cout<<"("<<x<<"."<<y<<")"<<endl; }
CTriangle.h
#ifndef CTRIANGLE_H_INCLUDED #define CTRIANGLE_H_INCLUDED #include "CPoint.h" 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; //三顶点 }; #endif // CTRIANGLE_H_INCLUDED
CTriangle.cpp
#include <iostream> #include <cmath> #include "CTriangle.h" using namespace std; void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z) { A=X; B=Y; C=Z; } float CTriangle::perimeter(void) { double a=A.Distance1(B),b=B.Distance1(C),c=C.Distance1(A); return a+b+c; } float CTriangle::area(void) { double a=A.Distance1(B),b=B.Distance1(C),c=C.Distance1(A); double p=(a+b+c)/2; return sqrt(p*(p-a)*(p-b)*(p-c)); } bool CTriangle::isRightTriangle() { double a=A.Distance1(B),b=B.Distance1(C),c=C.Distance1(A); double max=0; if(a>max) max=a; if(b>max) max=b; if(c>max) max=c; if((max==a&&a*a-b*b-c*c==0)||(max==b&&b*b-a*a-c*c==0)||(max==c&&c*c-a*a-b*b==0)) return true; else return false; } bool CTriangle::isIsoscelesTriangle() { double a=A.Distance1(B),b=B.Distance1(C),c=C.Distance1(A); if(a==b||b==c||a==c) return true; else return false; }
运行结果:
相关文章推荐
- 2013第四周上机任务【项目3-程序的多文件组织】
- 第四周项目 3 用多文件组织多个类的程序
- 第四周项目三----程序的多文件组织
- 第四周 项目三:用多文件组织多个类的程序
- 第四周项目3程序的多文件组织
- 第四周(项目三)——程序的多文件组织
- 第四周 项目三 程序的多文件组织
- 第四周 项目3 程序的多文件组织
- 第四周上机实践项目——程序的多文件组织
- 第四周—3.程序的多文件组织
- 第六周项目三——用多文件组织多个类的程序
- 第六周 项目3 用多文件组织多个类的程序
- 《第四周实验报告2-1》---利用多文件组织项目
- 第四周项目3 多文件组织
- 第四周任务2:多文件组织项目
- 第四周任务二(多文件组织项目)
- 2013级C++第4周(春)项目——再和对象找感觉【项目3 - 程序的多文件组织】
- C++第四周【任务2】利用多文件组织项目
- C++第4周(春)项目3 程序的多文件组织
- 第四周任务2:多文件组织项目