实用程序工具包(用户自制的头文件)

    /* 感觉好久没写新日志了,主要是因为最近感冒了。拉下了些课吧,为了补上必须自己看书还得完成实验报告。今天写个最近接触的小东西。本人是个菜鸟,写不了什么难的,只是想跟大家分享一下自己明白的程序。*/

     /*1.建立工程utility;2.建立实用程序工具包头文件utility.h;3.在主函数中包含头文件"utility.h"的功能验证。*/

//utility.h头文件内容如下:

#include <iostream>
#ifndef _ _UTILITY_H_ _   
#define _ _UTILITY_H_ _  
#include <time.h>
using namespace std;

/*
char GetChar(istream &inStream=cin);
//char GetChar(istream &in=cin);
bool UserSaysYes();


template <class ElemType>
void Swap(ElemType &e1,ElemType &e2);
template<class ElemType>
void Display(ElemType elem[],int n);


class Timer;
class Error;
class Rand;
*/
char GetChar(istream &in=cin)
{
 char ch;
 while ((ch=in.peek())!=EOF
  &&((ch=in.get())==' '
  ||ch=='/t'));
 return ch;
}

bool UserSaysYes()
{
 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;
 else
  return false;
}

template <class ElemType>
void Swap(ElemType &e1,ElemType &e2)
{
 ElemType temp;
 temp=e1;e1=e2;e2=temp;
}

template<class ElemType>
void Show(ElemType elem[],int n)
{
 for(int i=0;i<n;i++)
 {
  cout<<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
{
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;}
 static int GetRand(){return rand();}
};

#endif

//主函数如下:

#include <iostream>
#include "utility.h"
using namespace std;

int main(void)
{
 try
 {
  bool tag=true;
  while (tag)
  {
   int n;


   cout<<"请输入矩阵阶数:";
   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];
    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++)
     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++)
     {
      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();
 }

 return 0;
}

/*在上回的皇后问题中也可以包含这个头文件已求程序运行时间,主函数为:


#include "queen.h"    //这个上回写的笔记里有    
#include "utility.h"
int main(void)               
{
 
 Timer objTimer;

 Queen objQueen(8);                                          //八皇后问题对象;当然也可以求四皇后问题
 objQueen.Run();                                           //运行求解皇后问题
 cout<<"用时"<<objTimer.ElapsedTime()<<"秒。"<<endl;
 system("PAUSE");

 return 0;
}

*/

 

        个人觉得挺有意思的,可能是因为刚刚玩这个吧。以上是个人的一些体会,如有不好的地方,欢迎指正。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值