C++ 三角形面积计算
来源:互联网 发布:mac已安装软件桌面 编辑:程序博客网 时间:2024/05/17 22:36
作业要求
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();//纵坐标提取函数
纵坐标提取函数
friendclasstri;
友元类
成员变量
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(intx1=0,inty1=0,intx2=0,inty2=0,intx3=0,inty3=0)
{}
tri(tri&p);//拷贝构造函数
~tri();//析构函数 析构函数,显示调用顺序
voidtsetvalue(point&zp1,point&zp2,point&zp3);//设置函数 设置函数,将三个点类输入
doublearea();//面积计算函数 利用tarea=0.5*(x1*y2+x2*y3+x3*y1-x1*y3-x2*y1-x3*y2);公式将三角形的面积计算出来
源程序
由声明头文件point.h和tri.h、实现源程序文件point.cpp、tri.cpp和运算例程main.cpp组成。
声明头文件point.h代码清单
#ifndef POINT
#definePOINT
classtri;
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();//纵坐标提取函数
friendclasstri;
};
#endif// !POINT
声明头文件tri.h代码清单
#ifndef TRI
#define TRI
#include"point.h"
classtri
{
point marray[3];
public:
tri( constpoint &zp1,constpoint &zp2,constpoint &zp3);//构造函数
tri(intx1=0,inty1=0,intx2=0,inty2=0,intx3=0,inty3=0)
{}
tri(tri &p);//拷贝构造函数
~tri ();//析构函数
void tsetvalue (constpoint &zp1,constpoint &zp2,constpoint &zp3);//设置函数
double area ();//面积计算函数
};
#endif
实现源程序文件point.cpp代码清单
#include<iostream>
#include<cmath>
#include"point.h"
#include"tri.h"
usingnamespace std;
point::point(intzx,intzy)//构造函数
{
mx=zx;
my=zy;
}
point::point(point &p)//拷贝构造函数
{
mx=p.mx;
my=p.my;
}
point::~point()//析构函数
{
cout<<mx<<","<<my<<endl;
}
voidpoint::psetvalue(intzx,intzy)//point赋值函数
{
mx=zx;
my=zy;
}
intpoint::getx()//横坐标提取函数 //在此次试验中没有作用
{
return mx;
}
intpoint ::gety()//纵坐标提取函数 //在此次试验中没有作用
{
return my;
}
实现源程序文件tri.cpp代码清单
#include<iostream>
#include<cmath>
#include"point.h"
#include"tri.h"
usingnamespace std;
tri::tri(constpoint &zp1,constpoint &zp2,constpoint &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;
}
voidtri::tsetvalue(constpoint &zp1,constpoint &zp2,constpoint &zp3)//设置赋值函数
{
marray[0]=zp1;
marray[1]=zp2;
marray[2]=zp3;
}
doubletri ::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>
usingnamespace 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;
}
程序测试报告
测试用例设计
输入三个点的坐标为(0,0)(0,1)(1,0),输出面积为0.5
输入三个点的坐标为(0,0)(0,2)(2,0),输出面积为2
输入三个点的坐标为(0,0)(0,3)(3,0),输出面积为4.5
全部计算正确
自己程序运行结果截屏及说明
检查他人程序发现问题和学习心得
同学程序靓点:思路清晰,简单易懂,在main函数中加入判断循环while,通过用户的输入,判断是否计算。
你所发现的同学程序问题:
可能有重复编译的情况,应该加上#ifndef……#define……#endif
作业完成及检查学习心得
知道了友元类的使用方法,明白了友元类的“好处”,是我们能直接进入数据中,但是正如老师所说的,这是与c++的思想相悖的,所以要尽量的少使用。
- c语言 计算三角形的面积
- c++:计算三角形的惆怅与面积
- 【OpenJudge】c语言_计算三角形面积
- 计算三角形面积
- JavaScript计算三角形面积
- 计算三角形的面积
- 计算三角形面积
- 三角形面积计算
- C++ 三角形面积计算
- 矢量计算三角形面积
- 库函数计算三角形面积
- 计算三角形面积
- 计算三角形的面积
- 计算三角形的面积
- 三角形面积计算
- 三角形面积计算
- 17:计算三角形面积
- 计算三角形的面积
- linux下的四个简单函数介绍:getpid, getppid, getuid, getgid
- UVA11624 Fire! 两次BFS 读懂题意很重要
- C语言中的数组名与指针
- C/C++编写一个定时往串口(虚拟串口)发送数据的程序
- hdu1087 super jumping
- C++ 三角形面积计算
- Java虚拟机体系结构 - 访问策略1
- Dubbo之旅--问题汇总
- hdu1176 免费馅饼
- Windows 7 x64系统安装CRFsuite给Python使用
- Antlr v4入门教程和实例
- 闲聊DTD语法(二)
- android 接口回调
- 设计模式思考----适配器模式