C++ 三角形面积计算


面向对象程序设计与 C++ 实验作业报告 #8

作业要求

8[D&SJX_B] 设计、编写和调试面向对象C++程序,支持和实现基于三个平面坐 标点的三角形面积计算功能。基本要求同作业题[D&SJX_A],但要求三角形类作 为点类的友元类,故需改写三角形面积计算成员函数使其可直接访问其三个顶点 的横纵坐标值

程序设计说明

点类设计

类名

point

备注

成员函数

point (int zx=0,int zy=0);//构造函数

构造函数

point (point &p);//拷贝构造函数

拷贝构造函数

~point();//析构函数

析构函数

void psetvalue (int zx,int zy);//设置函数

设置横纵坐标函数

int getx();//横坐标提取函数

横坐标提取函数

int gety();//纵坐标提取函数

纵坐标提取函数

friend class tri;

友元类

成员变量

int mx,my;

横纵坐标

 

类名

tri

备注

成员函数

tri( point &zp1, point &zp2, point &zp3);//构造函数

构造函数

tri(int x1=0,int y1=0,int x2=0,int y2=0,int x3=0,int y3=0) {}

构造函数

tri(tri &p);//拷贝构造函数

拷贝构造函数

~tri ();//析构函数

析构函数

void tsetvalue ( point &zp1, point &zp2, point &zp3);//设置函数

赋值函数

double area ();//面积计算函数

面积计算函数

成员变量

point marray[3];

点类的对象数组

 

关键函数设计说明

point (int zx=0,int zy=0);//构造函数 构造函数,支持缺省赋值

point (point &p);//拷贝构造函数  用于拷贝构造函数

~point();//析构函数 输出赋值的结构,便于观察调用顺序

void psetvalue (int zx,int zy);//设置函数  设置函数中的横纵坐标

int getx();//横坐标提取函数  将函数的横坐标返回

int gety();//纵坐标提取函数  将函数的纵坐标返回

tri( point &zp1, point &zp2, point &zp3);//构造函数 

tri(int x1=0,int y1=0,int x2=0,int y2=0,int x3=0,int y3=0)

{}

tri(tri &p);//拷贝构造函数

~tri ();//析构函数  析构函数,显示调用顺序

void tsetvalue ( point &zp1, point &zp2, point &zp3);//设置函数  设置函数,将三个点类输入

double area ();//面积计算函数  利用tarea=0.5*(x1*y2+x2*y3+x3*y1-x1*y3-x2*y1-x3*y2);公式将三角形的面积计算出来

源程序

由声明头文件point.htri.h、实现源程序文件point.cpptri.cpp和运算例程main.cpp组成。

声明头文件point.h代码清单

#ifndef POINT

#define POINT

class tri;

 

class

   point

{

   int mx,my;

public:

   point (int zx=0,int zy=0);//构造函数

   point (point &p);//拷贝构造函数

   ~point();//析构函数

   void psetvalue (int zx,int zy);//设置函数

   int getx();//横坐标提取函数

   int gety();//纵坐标提取函数

   friend class tri;

 

};

 

 

#endif // !POINT

 

声明头文件tri.h代码清单

#ifndef  TRI

#define  TRI

#include"point.h"

class tri

{

   point marray[3];

public:

   tri( const point &zp1, const point &zp2,const point &zp3);//构造函数

   tri(int x1=0,int y1=0,int x2=0,int y2=0,int x3=0,int y3=0)

   {}

   tri(tri &p);//拷贝构造函数

   ~tri ();//析构函数

   void tsetvalue (const point &zp1,const point &zp2,const point &zp3);//设置函数

   double area ();//面积计算函数

};

#endif

实现源程序文件point.cpp代码清单

#include<iostream>

#include<cmath>

#include"point.h"

#include"tri.h"

using namespace std;

point::point(int zx,int zy)//构造函数

{

   mx=zx;

   my=zy;

}

 

point::point(point &p)//拷贝构造函数

{

   mx=p.mx;

   my=p.my;

}

 

point::~point()//析构函数

{

   cout<<mx<<","<<my<<endl;

}

void point::psetvalue(int zx,int zy)//point赋值函数

{

   mx=zx;

   my=zy;

}

 

int point::getx()//横坐标提取函数  //在此次试验中没有作用

{

   return mx;

}

 

int point ::gety()//纵坐标提取函数  //在此次试验中没有作用

{

   return my;

}

实现源程序文件tri.cpp代码清单

#include<iostream>

#include<cmath>

#include"point.h"

#include"tri.h"

using namespace std;

 

tri::tri( const point &zp1,const point &zp2,const point &zp3)//构造函数

{

   marray[0]=zp1;

   marray[1]=zp2;

   marray[2]=zp3;

}

tri::tri(tri &p)//拷贝构造函数

{

   marray[0]=p.marray[0];

 

   marray[1]=p.marray[1];

   marray[2]=p.marray[2];

  

}

tri::~tri()//析构函数

{

   cout <<"析构函数 tri"<< endl;

}

 

void tri::tsetvalue(const point &zp1,const point &zp2,const point &zp3)//设置赋值函数

{

   marray[0]=zp1;

   marray[1]=zp2;

   marray[2]=zp3;

}

 

double tri ::area()//面积计算函数

{

   double tarea ;

   double x1,x2,x3,y1,y2,y3;

   x1=marray[0].mx;

   x2=marray[1].mx;

   x3=marray[2].mx;

   y1=marray[0].my;

   y2=marray[1].my;

   y3=marray[2].my;

 

   tarea=0.5*(x1*y2+x2*y3+x3*y1-x1*y3-x2*y1-x3*y2);//此处可以将上述的marray[i].mx/my直接带入,但是为了简洁,不带入

  

   return fabs(tarea);//取绝对值

}

运算例程main.cpp代码清单

#include"point.h"

#include "tri.h"

#include <cmath>

#include<iostream>

#include<stdio.h>

 

using namespace std;

int main()

{

   int a,b;

   for(;1;)

   {

   cout<<"请输入第一个点的坐标"<<endl;

   cin>>a>>b;

   point zp1(a,b);

 

   cout<<"请输入第二个点的坐标"<<endl;

   cin>>a>>b;

   point zp2(a,b);

 

   cout<<"请输入第三个点的坐标"<<endl;

   cin>>a>>b;

   point zp3(a,b);

 

   tri mtri;

   mtri.tsetvalue(zp1,zp2,zp3);

   cout<<"the area is "<<mtri.area()<<endl;

   }

   system("pause");

   return 0;

}

程序测试报告

测试用例设计

  1. 输入三个点的坐标为(00)(01)(10),输出面积为0.5

  2. 输入三个点的坐标为(00)(02)(20),输出面积为2

  3. 输入三个点的坐标为(00)(03)(30),输出面积为4.5

    全部计算正确

自己程序运行结果截屏及说明

检查他人程序发现问题和学习心得

同学程序靓点:

思路清晰,简单易懂,在main函数中加入判断循环while,通过用户的输入,判断是否计算。

你所发现的同学程序问题:

可能有重复编译的情况,应该加上#ifndef……#define……#endif

作业完成及检查学习心得

知道了友元类的使用方法,明白了友元类的“好处”,是我们能直接进入数据中,但是正如老师所说的,这是与c++的思想相悖的,所以要尽量的少使用。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值