#ifndef __UTILITY_H__ // 如果没有定义__UTILITY_H__
#define __UTILITY_H__ // 那么定义__UTILITY_H__
// 实用程序工具包
#ifdef _MSC_VER // 表示是Visual C++
#if _MSC_VER == 1200 // 表示Visual C++6.0
// 标准库头文件
#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> // 支持断言
#else // 其它版本的Visual C++
// ANSI C++标准库头文件
#include <string> // 标准串和操作
#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中
#endif // _MSC_VER == 1200
#else // 非Visual C++
// ANSI C++标准库头文件
#include <string> // 标准串操作
#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中
#endif // _MSC_VER
// 实用函数
char GetChar(istream &inStream = cin); // 从输入流inStream中跳过空格及制表符获取一字符
bool UserSaysYes(); // 当用户肯定回答(yes)时, 返回true, 用户否定回答(no)时,返回false
// 函数模板
template <class ElemType >
void Swap(ElemType &e1, ElemType &e2); // 交换e1, e2之值
template<class ElemType>
void Display(ElemType elem[], int n); // 显示数组elem的各数据元素值
// 实用类
class Timer; // 计时器类Timer
class Error; // 通用异常类
class Rand; // 随机数类Rand
char GetChar(istream &in) // 从输入流in中跳过空格及制表符获取一字符
{
char ch; // 临时变量
while ((ch = in.peek()) != EOF // 文件结束符(peek()函数从输入流中接受1
// 字符,流的当前位置不变)
&& ((ch = in.get()) == ' ' // 空格(get()函数从输入流中接受1字符,流
// 的当前位置向后移1个位置)
|| 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回答:"; // 非初始回答
while ((ch = GetChar()) == '\n'); // 跳过空格,制表符及换行符获取一字符
initialResponse = false; // 非初始回答
} while (ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N');
while (GetChar() != '\n'); // 跳过当前行后面的字符
if (ch == 'y' || ch == 'Y') return true; // 肯定回答返回true
else return false; // 否定回答返回false
}
template <class ElemType >
void Swap(ElemType &e1, ElemType &e2) // 交换e1, e2之值
{
ElemType temp; // 临时变量
temp = e1; e1 = e2; e2 = temp; // 循环赋值实现交换e1, e2
}
template<class ElemType>
void Show(ElemType elem[], int n) // 显示数组elem的各数据元素值
{
for (int i = 0; i < n; i++)
{ // 显示数组elem
cout << elem[i] << " "; // 显示elem[i]
}
cout << endl; // 换行
}
// 计时器类Timer
class Timer
{
private:
// 数据成员
clock_t startTime;
public:
// 方法声明
Timer(){ startTime = clock(); } // 构造函数, 由当前时间作为开始时间构造对象
double ElapsedTime() const // 返回已过的时间
{
clock_t endTime = clock(); // 结束时间
return (double)(endTime - startTime) / (double)CLK_TCK; // 计算已过时间
}
void Reset(){ startTime = clock(); } // 重置开始时间
};
// 通用异常类Error
#define MAX_ERROR_MESSAGE_LEN 100
class Error
{
private:
// 数据成员
char message[MAX_ERROR_MESSAGE_LEN]; // 异常信息
public:
// 方法声明
Error(char mes[] = "一般性异常!"){ strcpy(message, mes); } // 构造函数
void Show() const{ cout << message << endl; } // 显示异常信息
};
// 随机数类Rand
class Rand
{
public:
// 方法声明
static void SetRandSeed(){ srand((unsigned)time(NULL)); } // 设置当前时间为随机数种子
static int GetRand(int n){ return rand() % n; } // 生成0 ~ n-1之间的随机数
static int GetRand(){ return rand(); } // 生成0 ~ n-1之间的随机数
};
#endif
离散数学的代价关系判断的c++代码
3星 · 超过75%的资源 需积分: 9 88 浏览量
2009-12-08
15:23:39
上传
评论
收藏 8KB RAR 举报
sanmao3
- 粉丝: 27
- 资源: 7