实验报告4:群体类和群体数据
更新时间:2024-05-03 04:21:01 阅读量: 综合文库 文档下载
- 群体遗传分析实验报告推荐度:
- 相关推荐
学校代码: 10128 学 号: 201220905060
《面向对象程序设计》实验报告
(
题 目:群体类和群体数据 学生姓名:燕飞 学 院:理学院 系 别:数学系
专 业:信息与计算科学 班 级:信计12-2 任课教师:侯睿
二 〇 一 五 年 十 一 月
一、实验目的
1、了解节点类的声明和实现,学习其使用方法 2、了解链表类的声明和实现,学习其使用方法 3、了解栈类的声明和实现,学习其使用方法 4、了解队列类的声明和实现,学习其使用方法 5、掌握对数组元素排序的方法 6、掌握对数组元素查找的方法
二、实验内容
1、编写程序Node.h实现例9-5的节点类,并编写测试程序lab9_1.cpp,实现链表的基本操作。 2、编写程序link.h实现例9-6的链表类,在测试程序lab_2.cpp中声明两个整型链表A和B,分别插入5元素,然后把B中的元素加入A的尾部。 3、编写程序queue.h,用链表实现队列(或栈),在测试程序lab9_3.cpp中声明一个整型队列(或栈)对象,插入5个整数,压入队列(或栈),再依次取出并显示出来。 4、(选做)声明course(课程)类,有属性:课程名char name[21]、成绩short score;在实验七的student类中增加属性;所修课程course,为课程类对象的链表。在测试程序中测试这个类,学生类与课程类关系如图
5、将直接插入排序、直接选择排序、冒泡排序、顺序查找函数封装到第九章的数组类中,作为成员函数,实现并测试这个类。
三、实验程序
1、
#ifndef NODE_CLASS #define NODE_CLASS template
private:
Node
Node (const T& item, Node
template
2
Node
template
Node
return next; }
template
void Node
p->next = next; next = p; }
template
Node
Node
next = tempPtr->next; return tempPtr; }
#endif
#ifndef NODE_LIBRARY #define NODE_LIBRARY #include
Node
Node
newNode = new Node
cerr << \ exit(1); }
return newNode; }
enum AppendNewline {noNewline,addNewline}; template
3
void PrintList(Node
Node
if(addnl == addNewline) cout << currPtr->data << endl; else
cout << currPtr->data << \ currPtr = currPtr->NextNode(); } }
template
int Find(Node
Node
while(currPtr != NULL) {
if (currPtr->data == item) return 1;
prevPtr = currPtr;
currPtr = currPtr->NextNode(); }
return 0; }
template
void InsertFront(Node
head = GetNode(item,head); }
template
void InsertRear(Node
Node
while(currPtr->NextNode() != NULL) currPtr = currPtr->NextNode(); newNode = GetNode(item); currPtr->InsertAfter(newNode); }
4
} template
void DeleteFront(Node
Node
head = head->NextNode(); delete p; } }
template
void Delete (Node
Node
while (currPtr != NULL && currPtr->data != key) {
prevPtr = currPtr;
currPtr = currPtr->NextNode(); }
if (currPtr != NULL) {
if(prevPtr == NULL)
head = head->NextNode(); else
prevPtr->DeleteAfter(); delete currPtr; } }
template
void InsertOrder(Node
Node
while (currPtr != NULL) {
if (item < currPtr->data) break;
prevPtr = currPtr;
currPtr = currPtr->NextNode(); }
5
if (prevPtr == NULL) InsertFront(head,item); else {
newNode = GetNode(item);
prevPtr->InsertAfter(newNode); } }
template
void ClearList(Node
Node
nextPtr = currPtr->NextNode(); delete currPtr; currPtr = nextPtr; }
head = NULL; }
#endif
#include
Node
for (i=0;i < 10;i++) {
cin>>item;
InsertFront(head, item); }
cout << \
PrintList(head,noNewline); cout << endl;
cout << \请输入一个需要删除的整数: \ cin >> key;
prevPtr = head;
while (Find(head,key,prevPtr) != NULL) {
if(prevPtr == NULL)
6
head = head->NextNode(); else
delPtr=prevPtr->DeleteAfter(); delete delPtr; }
cout << \
PrintList(head,noNewline); cout << endl; ClearList(head); } 2、
#include \int main() {
LinkedList
A.InsertRear(2*i+1); B.InsertRear(2*i+2); }
A.Reset();
cout << \链表A的元素为:\ while(!A.EndOfList()) {
cout << A.Data() << \ A.Next(); }
cout << endl; B.Reset();
cout << \链表B的元素为:\ while(!B.EndOfList()) {
cout << B.Data() << \ B.Next(); }
cout << endl;
cout << \把B中的元素插入A中...\ B.Reset();
while(!B.EndOfList()) {
A.InsertRear(B.Data()); B.Next(); }
A.Reset();
7
cout << \此时,链表A的元素为:\ while(!A.EndOfList()) {
cout << A.Data() << \ A.Next(); }
cout << endl; }
#ifndef LINKEDLIST_CLASS #define LINKEDLIST_CLASS #include
const int NULL = 0; #endif // NULL #include \template
private:
Node
Node
Node
void CopyList(const LinkedList
LinkedList(void);
LinkedList(const LinkedList
LinkedList
int EndOfList(void) const;
int CurrentPosition(void) const; void InsertFront(const T& item); void InsertRear(const T& item); void InsertAt(const T& item); void InsertAfter(const T& item); T DeleteFront(void);
8
void DeleteAt(void); T& Data(void); void ClearList(void); };
template
Node
Node
p = new Node
cout << \ exit(1); }
return p; }
template
void LinkedList
delete p; }
template
void LinkedList
Node
while (p != NULL) {
InsertRear(p->data); p = p->NextNode(); }
if (position == -1) return;
prevPtr = NULL; currPtr = front;
for (pos = 0; pos != position; pos++) {
prevPtr = currPtr;
currPtr = currPtr->NextNode(); } }
template
LinkedList
9
prevPtr(NULL),currPtr(NULL), size(0), position(-1) {}
template
LinkedList
front = rear = NULL;
prevPtr = currPtr = NULL; size = 0;
position = -1; CopyList(L); }
template
void LinkedList
Node
while(currPosition != NULL) {
nextPosition = currPosition->NextNode(); FreeNode(currPosition);
currPosition = nextPosition; }
front = rear = NULL;
prevPtr = currPtr = NULL; size = 0;
position = -1; }
template
LinkedList
ClearList(); }
template
LinkedList
if (this == &L) return *this; ClearList(); CopyList(L); return *this; }
template
int LinkedList
10
return size; }
template
int LinkedList
return size == 0; }
template
void LinkedList
if (currPtr != NULL) {
prevPtr = currPtr;
currPtr = currPtr->NextNode(); position++; } }
template
int LinkedList
return currPtr == NULL; }
template
int LinkedList
return position; }
template
void LinkedList
int startPos;
if (front == NULL) return;
if (pos < 0 || pos > size-1) {
cerr << \ return; }
if(pos == 0) {
prevPtr = NULL; currPtr = front; position = 0; }
11
else {
currPtr = front->NextNode(); prevPtr = front; startPos = 1;
for(position=startPos; position != pos; position++) {
prevPtr = currPtr;
currPtr = currPtr->NextNode(); } } }
template
T& LinkedList
if (size == 0 || currPtr == NULL) {
cerr << \ exit(1); }
return currPtr->data; }
template
void LinkedList
if (front != NULL) Reset(); InsertAt(item); }
template
void LinkedList
Node
newNode = GetNode(item); if (rear == NULL)
front = rear = newNode; else {
rear->InsertAfter(newNode); rear = newNode; }
currPtr = rear; position = size;
12
size++; }
template
void LinkedList
Node
newNode = GetNode(item,front); front = newNode; } else {
newNode = GetNode(item);
prevPtr->InsertAfter(newNode); }
if (prevPtr == rear) {
rear = newNode; position = size; }
currPtr = newNode; size++; }
template
void LinkedList
Node
p = GetNode(item); if (front == NULL) {
front = currPtr = rear = p; position = 0; } else {
if (currPtr == NULL) currPtr = prevPtr;
currPtr->InsertAfter(p); if (currPtr == rear) {
rear = p;
position = size; }
13
else
position++;
prevPtr = currPtr; currPtr = p; }
size++; }
template
T LinkedList
T item; Reset();
if (front == NULL) {
cerr << \ exit(1); }
item = currPtr->data; DeleteAt(); return item; }
template
void LinkedList
Node
if (currPtr == NULL) {
cerr << \ exit(1); }
if (prevPtr == NULL) {
p = front;
front = front->NextNode(); } else
p = prevPtr->DeleteAfter(); if (p == rear) {
rear = prevPtr; position--; }
currPtr = p->NextNode(); FreeNode(p);
14
size--; }
#endif 3、
#ifndef QUEUE_CLASS #define QUEUE_CLASS #include
private:
LinkedList
Queue(void);
void QInsert(const T& elt); T QDelete(void); T QFront(void);
int QLength(void) const; int QEmpty(void) const; void QClear(void); };
template
template
int Queue
return queueList.ListSize(); }
template
int Queue
return queueList.ListEmpty(); }
template
void Queue
queueList.ClearList(); }
template
void Queue
15
{
queueList.InsertRear(elt); }
template
T Queue
if (queueList.ListEmpty()) {
cerr << \ exit(1); }
return queueList.DeleteFront(); }
template
T Queue
if (queueList.ListEmpty()) {
cerr << \ exit(1); }
queueList.Reset();
return queueList.Data(); }
#endif
#ifndef LINKEDLIST_CLASS #define LINKEDLIST_CLASS #include
const int NULL = 0; #endif
#include \template
private:
Node
Node
Node
16
void CopyList(const LinkedList
LinkedList(void);
LinkedList(const LinkedList
LinkedList
int EndOfList(void) const;
int CurrentPosition(void) const; void InsertFront(const T& item); void InsertRear(const T& item); void InsertAt(const T& item); void InsertAfter(const T& item); T DeleteFront(void); void DeleteAt(void); T& Data(void);
void ClearList(void); };
template
Node
Node
p = new Node
cout << \ exit(1); }
return p; }
template
void LinkedList
delete p; }
template
void LinkedList
Node
while (p != NULL)
17
{
InsertRear(p->data); p = p->NextNode(); }
if (position == -1) return;
prevPtr = NULL; currPtr = front;
for (pos = 0; pos != position; pos++) {
prevPtr = currPtr;
currPtr = currPtr->NextNode(); } }
template
LinkedList
template
LinkedList
front = rear = NULL;
prevPtr = currPtr = NULL; size = 0;
position = -1; CopyList(L); }
template
void LinkedList
Node
while(currPosition != NULL) {
nextPosition = currPosition->NextNode(); FreeNode(currPosition);
currPosition = nextPosition; }
front = rear = NULL;
prevPtr = currPtr = NULL; size = 0;
position = -1; }
template
18
LinkedList
ClearList(); }
template
LinkedList
if (this == &L) return *this; ClearList(); CopyList(L); return *this; }
template
int LinkedList
return size; }
template
int LinkedList
return size == 0; }
template
void LinkedList
if (currPtr != NULL) {
prevPtr = currPtr;
currPtr = currPtr->NextNode(); position++; } }
template
int LinkedList
return currPtr == NULL; }
template
int LinkedList
return position; }
19
template
void LinkedList
int startPos;
if (front == NULL) return;
if (pos < 0 || pos > size-1) {
cerr << \ return; }
if(pos == 0) {
prevPtr = NULL; currPtr = front; position = 0; } else {
currPtr = front->NextNode(); prevPtr = front; startPos = 1;
for(position=startPos; position != pos; position++) {
prevPtr = currPtr;
currPtr = currPtr->NextNode(); } } }
template
T& LinkedList
if (size == 0 || currPtr == NULL) {
cerr << \ exit(1); }
return currPtr->data; }
template
void LinkedList
if (front != NULL) Reset();
20
InsertAt(item); }
template
void LinkedList
Node
newNode = GetNode(item); if (rear == NULL)
front = rear = newNode; else {
rear->InsertAfter(newNode); rear = newNode; }
currPtr = rear; position = size; size++; }
template
void LinkedList
Node
if (prevPtr == NULL) {
newNode = GetNode(item,front); front = newNode; } else {
newNode = GetNode(item);
prevPtr->InsertAfter(newNode); }
if (prevPtr == rear) {
rear = newNode; position = size; }
currPtr = newNode; size++; }
template
void LinkedList
21
Node
p = GetNode(item); if (front == NULL) {
front = currPtr = rear = p; position = 0; } else {
if (currPtr == NULL) currPtr = prevPtr;
currPtr->InsertAfter(p); if (currPtr == rear) {
rear = p;
position = size; } else
position++;
prevPtr = currPtr; currPtr = p; }
size++; }
template
T LinkedList
T item; Reset();
if (front == NULL) {
cerr << \ exit(1); }
item = currPtr->data; DeleteAt(); return item; }
template
void LinkedList
Node
if (currPtr == NULL) {
22
cerr << \ exit(1); }
if (prevPtr == NULL) {
p = front;
front = front->NextNode(); } else
p = prevPtr->DeleteAfter(); if (p == rear) {
rear = prevPtr; position--; }
currPtr = p->NextNode(); FreeNode(p); size--; }
#endif // LINKEDLIST_CLASS #include \int main() {
Queue
for(int i=0;i<5;i++) {
A.QInsert(2*i+1); }
cout << \队列A的元素为:\ while(!A.QEmpty()) {
cout << A.QFront() << \ A.QDelete(); }
cout << endl; } 4、 5、
#include
23
Array
int SortType; int SearchNum;
cout << \请输入10个整数\ for(i=0;i<10;i++) {
cout << \输入第\个数字:\ cin >> A[i]; }
cout << \输入的数组为:\ for(i=0;i<10;i++)
cout << A[i] << \ cout << endl;
cout << \请选择排序方法:1.直接插入排序 2.直接选择排序 3.冒泡排序:\ cin >> SortType; switch(SortType) {
case 1:
A.InsertionSort(); break; case 2:
A.SelectionSort(); break; case 3:
A.BubbleSort(); break; default:
cout << \输入错误,程序退出!\ exit(0); }
cout << \排序后的数组为:\ for(i=0;i<10;i++)
cout << A[i] << \ cout << endl;
cout << \请输入待查找的数字:\ cin >> SearchNum;
k= A.SeqSearch(SearchNum); if (k<0)
cout << \没有找到数字\ else
cout << SearchNum << \是第\个数字\}
#ifndef ARRAY1_CLASS
24
#define ARRAY1_CLASS #include
const int NULL = 0; #endif
enum ErrorType
{invalidArraySize, memoryAllocationError, indexOutOfRange}; char *errorMsg[] = {
\ \};
template
private:
T* alist; int size;
void Error(ErrorType error,int badIndex=0) const; public:
Array(int sz = 50); Array(const Array
Array
template
void Array
cerr << errorMsg[error];
if (error == indexOutOfRange) cerr << badIndex; cerr << endl; exit(1); }
25
template
Array
if (sz <= 0)
Error(invalidArraySize); size = sz;
alist = new T[size]; if (alist == NULL)
Error(memoryAllocationError); }
template
delete [] alist; }
template
Array
int n = X.size; size = n;
alist = new T[n]; if (alist == NULL)
Error(memoryAllocationError); T* srcptr = X.alist; T* destptr = alist; while (n--)
*destptr++ = *srcptr++; }
template
Array
int n = rhs.size; if (size != n) {
delete [] alist; alist = new T[n]; if (alist == NULL)
Error(memoryAllocationError); size = n; }
T* destptr = alist; T* srcptr = rhs.alist; while (n--)
*destptr++ = *srcptr++;
26
return *this; }
template
T& Array
if (n < 0 || n > size-1) Error(indexOutOfRange,n); return alist[n]; }
template
Array
return alist; }
template
int Array
return size; }
template
void Array
if (sz <= 0)
Error(invalidArraySize); if (sz == size) return;
T* newlist = new T[sz]; if (newlist == NULL)
Error(memoryAllocationError); int n = (sz <= size) ? sz : size; T* srcptr = alist; T* destptr = newlist; while (n--)
*destptr++ = *srcptr++; delete[] alist; alist = newlist; size = sz; }
template
void Array
int i, j; T temp;
for (i = 1; i < size; i++)
27
{
j = i;
temp = alist[i];
while (j > 0 && temp < alist[j-1]) {
alist[j] = alist[j-1]; j--; }
alist[j] = temp; } }
template
void Array
int smallIndex; int i, j;
for (i = 0; i < size-1; i++) {
smallIndex = i;
for (j = i+1; j < size; j++)
if (alist[j] < alist[smallIndex]) smallIndex = j;
Swap(alist[i], alist[smallIndex]); } }
template
T temp; temp = x; x = y; y = temp; }
template
void Array
int i,j;
int lastExchangeIndex; i = size-1; while (i > 0) {
lastExchangeIndex = 0; for (j = 0; j < i; j++)
if (alist[j+1] < alist[j])
28
{
Swap(alist[j],alist[j+1]); lastExchangeIndex = j; }
i = lastExchangeIndex; } }
template
int Array
for(int i=0;i < size;i++) if (alist[i] == key) return i; return -1; }
#endif
四、实验结果
1、
2、
3、
4、 5、
29
30
正在阅读:
实验报告4:群体类和群体数据05-03
羊群效应的初步研究11-26
2016.9.1导游应变能力知识题11-06
《半导体物理B》教学大纲2015版04-23
我当小保姆作文500字07-09
浅谈管理绩效管理的定义04-29
施工升降机使用、维修保养、检查记录表09-05
我家的猫作文250字07-10
展示空间设计调研 - 图文12-26
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 群体
- 实验
- 报告
- 数据
- S7-300的N个常见问题解答(之一到八完整版)
- Linux基础知识 - 内部培训
- 中华诗词之美叶嘉莹之王国维之时代及其 答案
- 暑期三下乡策划书
- 消防控制设备接地设计
- 师生发展结题报告
- 04跳绳比赛主持词
- 浅谈乡土植物在园林中作用
- 2012年高考填报志愿备忘(二)
- 冬季施工方案 - secret
- 泰山版六年级下册品德与社会教案
- 进出口企业守法状况自我测评标准试行
- 20140522淘宝运营专才最新考试试卷85分
- 八年级物理12月考试卷
- 灯具灯饰买家邮箱
- 宏艺科技危险源辨识、风险评价和风险控制管理制度
- 大学物理期末试卷(盐城工学院)
- 2017年中国供应链管理(SCM)行业分析及发展趋势预测(目录)
- 人教版10册表格版教案 - 图文
- 附加推销