您的位置:首页 > 其它

实用程序工具包

2012-11-23 22:17 162 查看
//utility.h

#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(); } // 重置开始时间

};

#define MAX_ERROR_MESSAGE_LEN 100

class Error// 通用异常类Error

{

private:

// 数据成员

char message[MAX_ERROR_MESSAGE_LEN]; // 异常信息

public:

// 方法声明

Error(char mes[] = "一般性异常!"){ strcpy(message, mes); }

// 构造函数

void Show() const{ cout << message << endl; } // 显示异常信息

};

class Rand// 随机数类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

//main.cpp

#include"utility.h"

void main()

{

try{

bool tag=true;

while(tag)

{

int n;

cout<<"请输入方阵的阶数"<<endl;

cin>>n;

if(n>1000)

throw Error("阶数太大了");

int **a,**b,**c;

Timer objTimer;

int i,j,k;

a=new int*[n+1];//分配矩阵的行(每行是个一位数组)

b=new int*[n+1];

c=new int *[n+1];

for(i=1;i<=n;i++)

{

a[i]=new int[n+1];//位矩阵的第i行分配存储空间

b[i]=new int[n+1];

c[i]=new int[n+1];

}

Rand::SetRandSeed();

objTimer.Reset();

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

{

a[i][j]=Rand::GetRand();

b[i][j]=Rand::GetRand();

}

for(i=1;i<=n;i++)//c=ab

for(j=1;j<=n;j++)

{

c[i][j]=0;

for(k=1;k<=n;k++)

c[i][j]=c[i][j]+a[i][k]*b[k][j];

}

for(i=1;i<=n;i++)//释放矩阵第i行所占用存储空间

{

delete []a[i];

delete []b[i];

delete []c[i];

}

delete []a;//释放矩阵所占用的所有存储空间

delete []b;

delete []c;

cout<<"用时"<<objTimer.ElapsedTime()<<"秒"<<endl;

cout<<"是否继续";

tag=UserSaysYes();

}

}

catch(Error err)

{

err.Show();

}

system("pause");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: