早教吧作业答案频道 -->其他-->
二维点容器设计C++首先设计一个二维点Point类,包括x,y2个浮点数成员;然后设计点容器PointContainer类,为该类设计成员函数:(1)成员函数voidAdd(Point&pt),实现向容器中添加点;(
题目详情
二维点容器设计C++
首先设计一个二维点Point类,包括x,y 2个浮点数成员;然后设计点容器PointContainer类,为该类设计成员函数:(1) 成员函数void Add(Point &pt),实现向容器中添加点;(2) 成员函数 void Remove(int index),删除容器中指定序号的点;(3) 成员函数 void Clear(),清空容器,删除所有点;(4) 成员函数 Point &Get(int index),返回指定序号点的引用;(5) 重载运算符“[ ]”,实现核(4)同样的功能;(6) 成员函数 int Count(),得到容器中点的数量;(7) 成员函数 int CloseToWhich(Point &pt,float fdist),返回容器中与参数pt距离小于fdist的第一个点的序号。(8) 编写主函数测试上述功能
首先设计一个二维点Point类,包括x,y 2个浮点数成员;然后设计点容器PointContainer类,为该类设计成员函数:(1) 成员函数void Add(Point &pt),实现向容器中添加点;(2) 成员函数 void Remove(int index),删除容器中指定序号的点;(3) 成员函数 void Clear(),清空容器,删除所有点;(4) 成员函数 Point &Get(int index),返回指定序号点的引用;(5) 重载运算符“[ ]”,实现核(4)同样的功能;(6) 成员函数 int Count(),得到容器中点的数量;(7) 成员函数 int CloseToWhich(Point &pt,float fdist),返回容器中与参数pt距离小于fdist的第一个点的序号。(8) 编写主函数测试上述功能
▼优质解答
答案和解析
//////////////////////////pointContainer.h#ifndef CONTAINER_H
#define CONTAINER_H
#include
using std::cout;
using std::endl;
class PointContainer;
class Point
{
friend class PointContainer;
public:
Point():x(0),y(0),next(NULL){}
Point(float x,float y):x(x),y(y),next(NULL){}
private:
float x;
float y;
Point* next;
};
class PointContainer
{
public:
PointContainer()
{
head=NULL;
}
void Add(Point& pt);
void Remove(int index);
void Clear();
Point& Get(int index);
Point& operator[](int index);
int Count();
int CloseToWhich(Point& pt,float fdist);
void display();
void displayItem(Point& pt);
private:
Point* head;
};
#endif************************************************************************//////////////////pointContainer.cpp#include"pointContainer.h"
//#include
#include
//using std::cout;
//using std::endl;
void PointContainer::Add(Point& pt)
{
if(NULL==head)
{
head=&pt;
}
else
{
Point* temp=head;
while(temp->next)
{
temp=temp->next;
}
temp->next=&pt;
}
}
int PointContainer::Count()
{
int count=0;
Point* temp=head;
while(temp)
{
count++;
temp=temp->next;
}
return count;
}
void PointContainer::Remove(int index)
{
if(index>Count()|| head==NULL)
{
cout< return;
}
else
{
int i=1;
Point* temp=head;
while(temp)
{
if((index-1)==i)
{
break;
}
else
{
i++;
temp=temp->next;
}
}
temp->next=temp->next->next;
}
}
void PointContainer::Clear()
{
Point* tempHead=head;
Point* temp=NULL;
while(tempHead)
{
temp=tempHead;
tempHead=tempHead->next;
delete temp;
temp=NULL;
}
head=NULL;
}
Point& PointContainer::Get(int index)
{
int i=1;
Point* temp=head;
if(index>Count()|| index<1)
{
cout< return *(Point*)NULL;
}
while(temp)
{
if(index==i)
{
break;
}
temp=temp->next;
i++;
}
return *temp;
}
Point& PointContainer::operator[](int index)
{
int i=1;
Point* temp=head;
if(index>Count()|| index<1)
{
cout< return *(Point*)NULL;
}
while(temp)
{
if(index==i)
{
break;
}
temp=temp->next;
i++;
}
return *temp;
}
int PointContainer::CloseToWhich(Point& pt,float fdist)
{
int i=1;
float tempValue;
if(head==NULL)
{
cout< return 0;
}
Point* temp=head;
while(temp)
{
tempValue=(temp->x-pt.x)*(temp->x-pt.x)+(temp->y-pt.y)*(temp->y-pt.y);
if(fdist>sqrt(tempValue))
{
break;
}
i++;
temp=temp->next;
}
return i;}
void PointContainer::display()
{
if(head==NULL)
{
cout< return ;
}
Point* temp=head;
while(temp)
{
cout<x<y< temp=temp->next;
}
return;
}
void PointContainer::displayItem(Point& pt)
{
cout< return;
}*******************************************main.cpp#include"pointContainer.h"
#includeusing std::cout;
using std::endl;int main()
{
PointContainer PointCon;
Point* pt1=new Point(2,3);
Point* pt2=new Point(6,3);
Point* pt3=new Point(4,1);
PointCon.Add(*pt1);
PointCon.Add(*pt2);
PointCon.Add(*pt3);
PointCon.display();
// PointCon.Remove(2);
PointCon.display();
// PointCon.Clear();
PointCon.displayItem(PointCon[2]);
Point* pt4=new Point(7,2);
cout< return 1;
}
#define CONTAINER_H
#include
using std::cout;
using std::endl;
class PointContainer;
class Point
{
friend class PointContainer;
public:
Point():x(0),y(0),next(NULL){}
Point(float x,float y):x(x),y(y),next(NULL){}
private:
float x;
float y;
Point* next;
};
class PointContainer
{
public:
PointContainer()
{
head=NULL;
}
void Add(Point& pt);
void Remove(int index);
void Clear();
Point& Get(int index);
Point& operator[](int index);
int Count();
int CloseToWhich(Point& pt,float fdist);
void display();
void displayItem(Point& pt);
private:
Point* head;
};
#endif************************************************************************//////////////////pointContainer.cpp#include"pointContainer.h"
//#include
#include
//using std::cout;
//using std::endl;
void PointContainer::Add(Point& pt)
{
if(NULL==head)
{
head=&pt;
}
else
{
Point* temp=head;
while(temp->next)
{
temp=temp->next;
}
temp->next=&pt;
}
}
int PointContainer::Count()
{
int count=0;
Point* temp=head;
while(temp)
{
count++;
temp=temp->next;
}
return count;
}
void PointContainer::Remove(int index)
{
if(index>Count()|| head==NULL)
{
cout< return;
}
else
{
int i=1;
Point* temp=head;
while(temp)
{
if((index-1)==i)
{
break;
}
else
{
i++;
temp=temp->next;
}
}
temp->next=temp->next->next;
}
}
void PointContainer::Clear()
{
Point* tempHead=head;
Point* temp=NULL;
while(tempHead)
{
temp=tempHead;
tempHead=tempHead->next;
delete temp;
temp=NULL;
}
head=NULL;
}
Point& PointContainer::Get(int index)
{
int i=1;
Point* temp=head;
if(index>Count()|| index<1)
{
cout< return *(Point*)NULL;
}
while(temp)
{
if(index==i)
{
break;
}
temp=temp->next;
i++;
}
return *temp;
}
Point& PointContainer::operator[](int index)
{
int i=1;
Point* temp=head;
if(index>Count()|| index<1)
{
cout< return *(Point*)NULL;
}
while(temp)
{
if(index==i)
{
break;
}
temp=temp->next;
i++;
}
return *temp;
}
int PointContainer::CloseToWhich(Point& pt,float fdist)
{
int i=1;
float tempValue;
if(head==NULL)
{
cout< return 0;
}
Point* temp=head;
while(temp)
{
tempValue=(temp->x-pt.x)*(temp->x-pt.x)+(temp->y-pt.y)*(temp->y-pt.y);
if(fdist>sqrt(tempValue))
{
break;
}
i++;
temp=temp->next;
}
return i;}
void PointContainer::display()
{
if(head==NULL)
{
cout< return ;
}
Point* temp=head;
while(temp)
{
cout<
}
return;
}
void PointContainer::displayItem(Point& pt)
{
cout<
}*******************************************main.cpp#include"pointContainer.h"
#include
using std::endl;int main()
{
PointContainer PointCon;
Point* pt1=new Point(2,3);
Point* pt2=new Point(6,3);
Point* pt3=new Point(4,1);
PointCon.Add(*pt1);
PointCon.Add(*pt2);
PointCon.Add(*pt3);
PointCon.display();
// PointCon.Remove(2);
PointCon.display();
// PointCon.Clear();
PointCon.displayItem(PointCon[2]);
Point* pt4=new Point(7,2);
cout<
}
看了二维点容器设计C++首先设计一...的网友还看了以下:
用海涅法则证明极极限..Y=sin1/x在.X0(这是一个很小的0)=0处为第二类间断点证明:Y= 2020-05-13 …
凡是被定为一二类设备的电气设备,均称为()。 (A)完好设备 (B)良好设备 (C)优良 2020-05-31 …
龙这种动物到底是怎么产生的?十二生肖11个都是真有的,如果龙是人们幻想出来的不是真正存在的又怎么会 2020-06-07 …
关于杠杆有下列杠杆:镊子、独轮车、理发剪刀、拧螺丝的扳手、用餐的筷子、剪纸刀和大刀钳,它们可以分成 2020-07-08 …
“K”粉,在医学上称氯胺酮,是一种白色粉末,属于静脉全麻药品,具有一定的精神依赖性.目前,该药物已 2020-07-12 …
“K”粉在医学上称氯胺酮,属于静脉全麻药品,具有一定的精神信赖性,因其物理形状通常为白色粉末,故称“ 2020-11-24 …
“K”粉在医学上称氯胺酮,属于静脉全麻药品,具有一定的精神信赖性,因其物理形状通常为白色粉末,故称“ 2020-12-14 …
“K”粉医学上称氯胺酮,属于静脉全麻药品,具有一定的精神依赖性.因其物理形状通常为白色粉末,故称“K 2020-12-14 …
“工程施工”账户、“劳务成本”账户、“在建工程”账户和“生产成本”账户,按其经济内容分,均为成本类账 2020-12-26 …
每颗类地行星都有相同比例的水和氧气?比如金星主要成分是硫酸,为什么?因为高温,氧气将硫成二氧化硫,二 2021-01-14 …