早教吧作业答案频道 -->其他-->
二维点容器设计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++首先设计一...的网友还看了以下:
几个"e"的发音请具体说说音标中几个类似"e"的发音,嘴形应该怎么样,他们之间要注意些什么!一个像 2020-05-14 …
将括号内字母发音相同的单词归类W(e)st,tr(a)ffic,(c)lass,t(e)le)l, 2020-05-14 …
在证券交易程序中,竞价成交的核心内容是( )。A.对象优先 B.方式优先C.数量优先 D.价格优先E 2020-05-21 …
种子蕨是()。A.原始蕨类B.真蕨类C.最古老、最原始的裸子植物D.许多现代裸子植物的祖先E.C和D 2020-05-25 …
在面向对象系统中,不同类型元素的有序集合,称为______。A.行类型B.数组类型C.列表类型D.包 2020-05-26 …
保证项目可行的条件是( )。 A.FNPV≥0 B.FNPV≤0 C.FIRR≥ic D.FIRR≤ 2020-06-07 …
戈登提出四种类比方法。多项选择:a、拟人类比b、直接类比c、象征类比d、复合类比e、幻想类比 2020-07-30 …
求一道极限题求lim(t->+∞)[t+1/p]/e^(pt)=lim(t->+∞)1/[pe^( 2020-08-03 …
我国营养学家将食物分为五类:A、谷类(米、面)、薯类;B、禽、鱼、蛋、瘦肉类;C、蔬菜、水果类;D、 2020-11-14 …
side变复数sides,如何发音?以e结尾的名词变复数时,如果这个e不发音(感觉大多数这类带e的单 2021-02-05 …