程序设计艺术与方法实验报告
//the STL function sort();
bool sortByPolorAngle(const POINT &p1,const POINT &p2)
{
double d=direction(startPoint,p1,p2);
if(d<0)return true;
if(d>0)return false;
lsit顺序容器提供在容器中任意位置进行插入与删除操作的有效实现方法。如果打多说插入和删除发生在容器首尾,在双头队列更适合。List是双链表,每个结点包含数据和指向上一个结点、下一个结点的指针,支持双向迭代器。List与一般的容器相比增加的成员函数有splice、push_front、remove、unique、merge、reverse、sort等,使用的时候要包含<list>头文件.
Please enter the element you want to find
2
The element 2 is in the array
10 6 5 5 4 4 3 2 1 1 1
10 6 5 5 4 4 3 2 1 1
请按任意键继续. . .
5 3 5 3 7 1 3 8 1 7
9 5 3 5 3 7 1 3 8 1 7
实验三计算几何算法的实现
一、实验目的和要求∶
(1)理解线段的性质、叉积和有向面积。
(2)掌握寻找凸包的算法。
(3)综合运用计算几何和搜索中的知识求解有关问题。
二、实验环境和仪器设备∶
硬件环境:PC计算机
软件环境:操作系统:Windows 2000 / Windows XP / Linux
语言环境:Dev cpp / gnu c++
//p1p turns to p2p with respect to point p
//if return value is positive,means clockwise;
//if return value is negative,means counter-clockwise;
//naught means on the same line;
二、实验环境和仪器设备∶
硬件环境:PC计算机
软件环境:
操作系统:Windows 2000 / Windows XP / Linux
语言环境:Dev cpp / gnu c++
三、相关资料和参考文献(教材和实验指导书以外)∶
《算法与程序设计》
四、实验任务∶
编写程序实现实验内容要求的功能,并通过测试数据。
double min_x=p1.first<p2.first?p1.first:p2.first;
double max_x=p1.first>p2.first?p1.first:p2.first;
double min_y=p1.second<p2.second?p1.second:p2.second;
void find_convex_hull(vector<POINT>&point)
{
POINT p0=point[0];
(2)练习泛型算法的使用。定义一个vector,元素类型为int,插入10个随机数,使用sort按升序排序,输出每个元素的值,再按降叙排序,输出每个元素的值。练习用find查找元素。用min和max找出容器中的最小元素和最大元素,并输出。
六、源程序∶
#include<iostream>
#include<vector>
计算机与信息学院
程序设计艺术与方法
实验报告
专业班级
计算机科学与技术08-3班
学生姓名及学号
孔露20082582
课程教学班号
任课教师
徐本柱
实验指导教师
徐本柱
实验地点
2009 ~2010学年第二学期
实验一STL的熟悉和使用
一、实验目的和要求∶
(1)掌握C++中STL的容器类的使用。
(2)掌握C++中STL的算法类的使用。
int r=rand();
a1.push_back(r);
}
cout<<"向量中元素为:"<<endl;
Array1::iterator p;
for(p=a1.begin();p!=a1.end();p++){
cout<<*p<<endl;
}
int r=rand();
a1.insert(a1.begin(),r);
else{
a1.push_back(123);
cout<<"元素未找到"<<*location<<endl;
for(p=a1.begin();p!=a1.end();p++){
cout<<*p<<endl;
}
}
cout<<"将vector排序后的各元素为:"<<endl;
sort(a1.begin(),a1.end());
五、实验内容(步骤)∶
(1)练习vector和list的使用。定义一个空的vector,元素类型为int,生成10个随机数插入到vector中,用迭代器遍历vector并输出其中的元素值。在vector头部插入一个随机数,用迭代器遍历vector并输出其中的元素值。用泛型算法find查找某个随机数,如果找到便输出,否则将此数插入vector尾部。用泛型算法sort将vector排序,用迭代器遍历vector并输出其中的元素值。删除vector尾部的元素,用迭代器遍历vector并输出其中的元素值。将vector清空。定义一个list,并重复上述实验,并注意观察结果。
if(d==0&&on_segment(startPoint,p1,p2))return true;
if(d==0&&on_segment(p2,startPoint,p1))return true;
return false;
}
//here realizes the process of finding convex hull
for(p=a1.begin();p!=a1.end();p++){
cout<<*p<<endl;
}
a1.clear();
if(a1.empty())cout<<"vector已空!\n\n\n"<<endl;
lint l;
for(int j=0;j<10;j++){
int r=rand();
l.push_back(r);
1 1 3 3 3 5 5 7 7 8 9
1 1 3 3 3 5 5 7 7 8
请按任意键继续. . .
七、实验总结:
C++标准模板库提供三种顺序容器:vector、list和deque。Vector类和deque类都是基于数组的,list类实现链表数据结构。
vector类是一个线性数组类,内部用线性空间来存放元素,内部有两个不同的变量来标识当前元素个数_size和当前共分配的空间数_capacity,通常情况下_size要小于_capacity,这样当在数组中添加元素时就不用每次都重新分配空间,从而提高了时间效率,直到分配的空间使用完时才重新分配。Vector类重载了下标运算符,可以像数组一样访问容器中的元素,而且vector可以相互赋值,这是C语言的数组所不能实现的。
double direction(POINT p,POINT p1,POINT p2){
POINT v1,v2;
v1.first=p2.first-p1.first;
v1.second=p2.second-p1.first;
v2.first=p1.first-p.first;
v2.second=p1.second-p.second;
for(p2=l.begin();p2!=l.end();p2++){
cout<<*p2<<endl;
}
lint::iterator p3=find(l.begin(),l.end(),123);
if(p3!=l.end() )cout<<"元素为"<<*p3<<endl;
else{
l.push_back(123);
(4)房间最短路问题:给顶一个内含阻碍墙的房间,求解出一条从起点到终点的最最短路径。房间的边界固定在x=0,x=10,y=0和y=10。起点和重点固定在(0,5)和(10,5)。房间里还有0到18个墙,每个墙有两个门。输入给定的墙的个数,每个墙的x位置和两个门的y坐标区间,输出最短路的长度。下图是个例子:
}
cout<<"链表中元素为:"<<endl;
lint::iterator p2;
for(p2=l.begin();p2!=l.end();p2++){
cout<<*p2<<endl;
}
int r2=rand();
l.insert(l.begin(),r2);
cout<<"头部插入元素后链表中的元素为:"<<endl;
cout<<"头部插入元素后向量中的元素为:"<<endl;