使用软件vs2022
包含两个头文件
#ifndef __ASSISTANCE_H__ // 如果没有定义__ASSISTANCE_H__
#define __ASSISTANCE_H__ // 那么定义__ASSISTANCE_H__
// 辅助软件包
// ANSI C++标准库头文件
#include <cstring> // 标准串操作
#include <iostream> // 标准流操作
#include <limits> // 极限
#include <cmath> // 数据函数
#include <fstream> // 文件输入输出
#include <cctype> // 字符处理
#include <ctime> // 日期和时间函数
#include <cstdlib> // 标准库
#include <cstdio> // 标准输入输出
#include <iomanip> // 输入输出流格式设置
#include <cstdarg> // 支持变长函数参数
#include <cassert> // 支持断言
using namespace std; // 标准库包含在命名空间std中
// 自定义类型
enum Status {
SUCCESS, FAIL, UNDER_FLOW, OVER_FLOW, RANGE_ERROR, DUPLICATE_ERROR,
NOT_PRESENT, ENTRY_INSERTED, ENTRY_FOUND, VISITED, UNVISITED
};
// 宏定义
constexpr auto DEFAULT_SIZE = 1000; // 缺省元素个数;
constexpr auto DEFAULT_INFINITY = 1000000; // 缺省无穷大;
// 辅助函数声明
char GetChar(istream& inStream = cin); // 从输入流inStream中跳过空格及制表符获取一字符
template <class ElemType >
void Swap(ElemType& e1, ElemType& e2); // 交换e1, e2之值
template<class ElemType>
void Display(ElemType elem[], int n); // 显示数组elem的各数据元素值
template <class ElemType>
void Write(const ElemType& e); // 显示数据元素
// 辅助类
class Error; // 通用异常类
char GetChar(istream& inStream)
// 操作结果:从输入流inStream中跳过空格及制表符获取一字符
{
char ch; // 临时变量
while ((ch = (inStream).peek()) != EOF // 文件结束符(peek()函数从输入流中接受1
// 字符,流的当前位置不变)
&& ((ch = (inStream).get()) == ' ' // 空格(get()函数从输入流中接受1字符,流
// 的当前位置向后移1个位置)
|| ch == '\t')); // 制表符
return ch; // 返回字符
}
// 通用异常类
constexpr auto MAX_ERROR_MESSAGE_LEN = 100;
class Error
{
private:
// 数据成员
char message[MAX_ERROR_MESSAGE_LEN];// 异常信息
public:
// 方法声明
Error(const char* mes = "一般性异常!"); // 构造函数
~Error(void) {}; // 析构函数
void Show() const; // 显示异常信息
};
// 通用异常类的实现部分
Error::Error(const char* mes)
// 操作结果:由mes构构通用异常对象
{
strcpy_s(message, mes); // 复制异常信息
}
void Error::Show()const
// 操作结果:显示异常信息
{
cout << message << endl; // 显示异常信息
}
template <class ElemType >
void Swap(ElemType& e1, ElemType& e2)
// 操作结果: 交换e1, e2之值
{
ElemType temp; // 临时变量
// 循环赋值实现交换e1, e2
temp = e1; e1 = e2; e2 = temp;
}
template<class ElemType>
void Display(ElemType elem[], int n)
// 操作结果: 显示数组elem的各数据元素值
{
for (int i = 0; i < n; i++)
{ // 显示数组elem
cout << elem[i] << " ";
}
cout << endl;
}
template <class ElemType>
void Write(const ElemType& e)
// 操作结果: 显示数据元素
{
cout << e << " ";
}
#endif
#pragma once
#include"Assistance.h"//辅助软件包
//有序顺序表
template<class ElemType>
class OrdSeqList
{
protected://有序顺序表数据成员
int length;//顺序表当前长度
int maxLength;//顺序表最大容量
ElemType* elems;//元素存储空间首地址
public://顺序表函数成员
OrdSeqList(int size = DEFAULT_SIZE);//构造函数,构造一个空表
OrdSeqList(const OrdSeqList<ElemType>& ord, const OrdSeqList<ElemType>& ord2);//由两个有序表构造一个新的有序表
//OrdSeqList(const OrdSeqList<ElemType>& ord);//拷贝构造函数
virtual ~OrdSeqList();//析构函数
Status InsertElem(const ElemType& e);//插入元素e
int GetLength() const;//取顺序表长度
bool IsEmpty() const;//判断是否为空
void Clear();//清空
void Traverse(void(*Visit)(const ElemType&)) const;//遍历顺序表
int LocateElem(const ElemType& e) const;//求指定元素在顺序表中的位置
Status GetElem(int i, ElemType& e) const;//取顺序表中第i个元素的值
//Status SetElem(int i, const ElemType& e);//修改顺序表中第i个元素的值
void DeleteElem(ElemType a, ElemType b);//删除第i个元素,并返回它的值
void DeleteElem(const ElemType e);//删除值为e的所有元素
//OrdSeqList<ElemType>& operator=(const OrdSeqList<ElemType>& ord);//重载赋值语句
};
//函数成员的实现
template<class ElemType>
OrdSeqList<ElemType>::OrdSeqList(int size)//构造一个容量为size的空有序顺序表
{
elems = new ElemType[size];//申请存储空间
assert(elems);//申请存储空间失败,程序终止
maxLength = size;
length = 0;//当前长度为0
}
template<class ElemType>
OrdSeqList<ElemType>::~OrdSeqList()
{
delete[]elems;
}
template<class ElemType>
int OrdSeqList<ElemType>::GetLength()const
{
return length;
}
template<class ElemType>
bool OrdSeqList<ElemType>::IsEmpty()const
{
return length == 0;
}
template<class ElemType>
void OrdSeqList<ElemType>::Clear()
{
length = 0;
}
template<class ElemType>
void OrdSeqList<ElemType>::Traverse(void(*visit)(const ElemType&))const
{
for (int i = 0; i < length; i++)
(*visit)(elems[i]);
}
template<class ElemType>
int OrdSeqList<ElemType>::LocateElem(const ElemType& e)const
//求元素e在顺序表中的序号,如果顺序表中不存在元素e则返回0
{
int i = 0;
while (i < length && elems[i]!=e) i++;
return i < length ? i + 1 : 0;
}
template<class ElemType>
Status OrdSeqList<ElemType>::GetElem(int i, ElemType& e)const
//当顺序表存在第i个元素时,用e返回其值,函数返回ENTRY_FOUND,否则函数返回NOT_PRESENT
{
if (i<1 || i>length)
return NOT_PRESENT;//返回元素不存在
else {
e = elems[i - 1];
return ENTRY_FOUND;//元素存在
}
}
//插入元素e
template<class ElemType>
Status OrdSeqList<ElemType>::InsertElem(const ElemType& e)
{
int i = length;
if (length == maxLength)
return OVER_FLOW;
else {
while (i > 0 && elems[i - 1] > e) {
elems[i] = elems[i - 1];
i--;
}
elems[i] = e;
length++;
return SUCCESS;
}
}
//删除值为e的所有元素
template<class ElemType>
void OrdSeqList<ElemType>::DeleteElem(const ElemType e)
{
int i = 0, j;
while (i <= length - 1 && elems[i] < e) i++;
if (i < length - 1 && elems[i] == e) {
j = i + 1;
while (j <= length - 1 && elems[j] == e) j++;
while (j <= length) {
elems[i++] = elems[j++];
}
length = length + i - j;
}
}
//合并两个有序表
template<class ElemType>
OrdSeqList<ElemType>::OrdSeqList(const OrdSeqList<ElemType>& oa, const OrdSeqList<ElemType>& ob)
{
int oaLength = oa.GetLength();
int obLength = ob.GetLength();
ElemType e1, e2;
if (oa.maxLength > ob.maxLength)
this->maxLength = 2 * oa.maxLength;
else
this->maxLength = 2 * ob.maxLength;
elems = new ElemType[this->maxLength];
assert(elems);
length = 0;
int i = 1, j = 1;
oa.GetElem(1, e1);
ob.GetElem(1, e2);
while(i <= oaLength && j <= obLength&&length<maxLength) {
if (e1 < e2) {
elems[length++] = e1;
oa.GetElem(++i, e1);
}
else {
elems[length++] = e2;
ob.GetElem(++j,e2);
}
}
while (i <= oaLength&&length<maxLength) {
elems[length++] = e1;
oa.GetElem(++i, e1);
}
while (j <= obLength&&length<maxLength) {
elems[length++] = e2;
ob.GetElem(++j, e2);
}
}
//删除给定区间值
template<class ElemType>
void OrdSeqList<ElemType>::DeleteElem(ElemType s, ElemType t)
{
int i = 0, j;
if (length <= 0 || s > t) {
cerr << "List is empty or parameters are illegal!" << endl;
return;
}
else {
while (i <= length - 1 && elems[i] < s)i++;
if (elems[i] <= t) {
j = i + 1;
while (j <= length - 1 && elems[j] <= t)j++;
while (j <= length) {
elems[i++] = elems[j++];
}
length = length + i - j;
}
}
}