第六周实验报告4

/* (程序头部注释开始)

* 程序的版权和版本声明部分

* Copyright (c) 2011, 烟台大学计算机学院学生

* All rights reserved.

* 文件名称:

* 作 者: 刘文英

* 完成日期:2012 年 3月28 日

* 版 本 号:

* 对任务及求解方法的描述部分

* 输入描述: 将任务4的解决用一个项目多个文件的方式实现,其中两个类的声明放在一个.h文件中,每个类的成员函数分别放一个文件,main()函数用一个文件。体会这样安排的优点。

* 问题描述:

* 程序输出:

* 程序头部的注释结束*/
[cpp] view plaincopyprint?

    //CT.h  
    class CPoint  
    {  
    private:  
     mutable double x;  // 横坐标  
     mutable double y;  // 纵坐标  
    public:  
     CPoint(double xx=0,double yy=0);  
     double Distance(CPoint p) const;   // 两点之间的距离(一点是当前点,另一点为参数p)  
     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);//  
     double perimeter(void);//计算三角形的周长  
     double area(void);//计算并返回三角形的面积  
     bool isRightTriangle(); //是否为直角三角形  
     bool isIsoscelesTriangle(); //是否为等腰三角形  
    private:  
     CPoint A,B,C; //三顶点  
    };  


[cpp] view plaincopyprint?

    //main.cpp  
    #include<iostream>  
    #include<cmath>  
    #include"CT.h"  
    using namespace std;  
    void main(void)  
    {   
       
     CTriangle tr1(CPoint (1,4),CPoint (0,0),CPoint (6,0));  
     cout<<"该三角形周长:"<<tr1.perimeter();  
     cout<<"面积:"<<tr1.area();  
     cout<<endl;  
     cout<<"该三角形"<<(tr1.isRightTriangle()?"是":"不是")<<"直角三角形"<<endl;  
     cout<<"该三角形"<<(tr1.isIsoscelesTriangle()?"是":"不是")<<"等腰三角形"<<endl;  
       
     system ("pause");  
    }  


[cpp] view plaincopyprint?

    //Cpoint.cpp  
    #include<iostream>  
    #include<cmath>  
    #include"CT.h"  
    using namespace std;  
    CPoint::CPoint(double xx,double yy)  
    {  
     x=xx;  
     y=yy;  
    }  
    //以x,y 形式输入坐标点  
    void CPoint::input()  
    {  
     char c;  
     while(1)  
     {  
      cout<<"请按照x,y格式输入"<<endl;  
      cin>>x>>c>>y;  
      if(c!=',')  
       cout<<"格式不对,请重新输入"<<endl;  
      else  
       break;  
     }  
    }  
      
    // 两点之间的距离(一点是当前点,另一点为参数p)  
    double CPoint::Distance(CPoint p) const  
    {  
     double s;  
     s=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));  
     return s;  
    }   
      
    //以(x,y) 形式输出坐标点  
    void CPoint::output()  
    {  
     cout<<'('<<x<<','<<y<<')';  
    }  


[cpp] view plaincopyprint?

    //CTriangle.cpp  
    #include<iostream>  
    #include<cmath>  
    #include"CT.h"  
    using namespace std;  
    void CTriangle::setTriangle(CPoint &X,CPoint &Y,CPoint &Z)  
    {  
     A = X;  
     B = Y;  
     C = Z;  
    }  
      
    //计算三角形的周长   
    double CTriangle::perimeter(void)  
    {  
     double a,b,c;  
     a = B.Distance(C);  
     b = A.Distance(C);  
     c = A.Distance(B);  
     return (a+b+c);  
    }  
      
    //计算并返回三角形的面积  
    double CTriangle::area(void)  
    {  
     double a,b,c,s;  
     a = B.Distance(C);  
     b = A.Distance(C);  
     c = A.Distance(B);  
     s = (a+b+c)/2;  
     return sqrt(s*(s-a)*(s-b)*(s-c));  
    }  
      
    //是否为直角三角形  
    bool CTriangle::isRightTriangle()  
    {  
     double a,b,c;  
     a = B.Distance(C);  
     b = A.Distance(C);  
     c = A.Distance(B);  
     if((a*a+b*b-c*c<1e-6) || (a*a+c*c-b*b<1e-6) || (b*b+c*c-a*a<1e-6))  
      return true;  
     else  
      return false;  
    }  
      
    //是否为等腰三角形  
    bool CTriangle::isIsoscelesTriangle()  
    {  
     double a,b,c;  
     a = B.Distance(C);  
     b = A.Distance(C);  
     c = A.Distance(B);  
     if((a-b<1e-6) || (a-c<1e-6) || (b-c<1e-6))  
      return true;  
     else  
      return false;  
    }  




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值