⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utility.h

📁 采用c++实现的文本编辑器 采用了数据结构中的双链表实现
💻 H
字号:
#ifndef __UTILITY_H__		// 如果没有定义__UTILITY_H__
#define __UTILITY_H__		// 那么定义__UTILITY_H__

// 实用程序软件包

// 标准库头文件
#include <string.h>			// 标准串和操作
#include <iostream.h>		// 标准流操作
#include <limits.h>			// 极限
#include <math.h>			// 数据函数
#include <fstream.h>		// 文件输入输出
#include <ctype.h>       	// 字符处理
#include <time.h>        	// 日期和时间函数
#include <stdlib.h>      	// 标准库
#include <stdio.h>       	// 标准输入输出
#include <iomanip.h>		// 输入输出流格式设置	
#include <stdarg.h> 		// 支持变长函数参数	
#include <assert.h>			// 支持断言

// 自定义类型
enum StatusCode {SUCCESS, FAIL, UNDER_FLOW, OVER_FLOW,RANGE_ERROR, DUPLICATE_ERROR,
	NOT_PRESENT, ENTRY_INSERTED, ENTRY_FOUND, VISITED, UNVISITED};

// 缺省长度
const int DefaultListSize = 10;

// 实用函数声明
char GetChar(istream &inStream = cin); // 从输入流inStream中跳过空格、换行符及制表符获取一字符
bool UserSaysYes();		// 当用户肯定回答(yes)时, 返回true, 用户否定回答(no)时,返回false
void SetRandSeed();		// 设置当前时间为随机数种子
int GetRand(int n);		// 生成0 ~ n-1之间的随机数
int GetRand();			// 生成随机数
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(ElemType &e);				// 显示数据元素

// 实用类声明
class Timer;			// 定时器类Timer
class Error;			// 通用异常类

char GetChar(istream &inStream)
// 操作结果:从输入流inStream中跳过空格、换行符及制表符获取一字符
{
	char ch;
	do
	{	// 跳过空格, 换行符及制表符
		inStream >> ch;
	} while (ch == ' ' || ch == '\n' || ch == '\t');

	return ch;
}

bool UserSaysYes()
// 操作结果: 当用户肯定回答(yes)时, 返回true, 用户否定回答(no)时,返回false
{
	char ch;							// 用户回答字符
	bool initialResponse = true;		// 初始回答

	do
	{	// 循环直到用户输入恰当的回答为止
		if (initialResponse)
		{	// 初始回答
			cout << "(y, n)?"; 
		}
		else
		{	// 非初始回答
			cout << "用y或n回答:";
		}
		
		ch = GetChar();				// 从输入流跳过空格, 换行符及制表符获取一字符
		initialResponse = false;
	} while (ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N');

	if (ch == 'y' || ch == 'Y') return true;
	else return false;
}

// 定时器类Timer
class Timer
{
private:
// 数据成员
	clock_t startTime;

public:
//  方法声明
	Timer();				// 构造函数
	double ElapsedTime();	// 返回已过的时间
	void Reset();			// 重置开始时间
};

// 定时器类Timer的实现部分
Timer::Timer()							
// 操作结果:由当前时间作为开始时间构造对象
{	 
	startTime = clock();
}

double Timer::ElapsedTime()
// 操作结果:返回从Timer对象启动或最后一次调用reset()后所使用的CPU时间
{
	clock_t endTime = clock();
	return (double)(endTime - startTime) / (double)CLK_TCK;
}

void Timer::Reset()
// 操作结果:重置开始时间
{
	startTime = clock();
}

// 通用异常类                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
#define MAX_ERROR_MESSAGE_LEN 100
class Error
{
private:
// 数据成员
	char message[MAX_ERROR_MESSAGE_LEN];// 异常信息

public:
//  方法声明
	Error(char mes[] = "一般性异常!");	// 构造函数 
	void Show() const;					// 显示异常信息
};

// 通用异常类的实现部分
Error::Error(char *mes)
// 操作结果:由mes构构通用异常对象
{
	strcpy(message, mes);				// 复制异常信息
}

void Error::Show()const
// 操作结果:显示异常信息
{
	cout << message << endl;			// 显示异常信息	
}

void SetRandSeed()
// 操作结果:设置当前时间为随机数种子
{ 
	srand((unsigned)time(NULL)); 
}

int GetRand(int n)
// 操作结果:生成0 ~ n-1之间的随机数
{ 
	return rand() % (n); 
}

int GetRand()
// 操作结果:生成随机数
{ 
	return rand(); 
}

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(ElemType &e)
// 操作结果: 显示数据元素
{
    cout << e << "  ";
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -