实用程序软件包(关于使用静态函数报错问题“Static function declared but not defined in C+++ "eh.h is only for C++!"报错问题)

#ifndef _UTILITY_H_//加static时,表示静态函数只在这个文件里用,那么编译器就在这个文件里找,没找到定义,就可以确定是未定义了;
//不加static,表示静态函数可能在其他文件里,但是编译时没找到,又不能确定是哪个文件应该有,所以报没有链接错误。
#define _UTILITY_H_
//***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>
//****ANSI C++end
//旧版C++标准库头文件
/*#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>*/
//旧版C++标准库头文件end
using namespace std;
#define MAX_ERROR_MESSAGE_LEN 20
static char GetChar(istream &inStream);
static bool UserSays_Yes();
static void SetRand_Seed();
static int GetRand(int n);
static int GetRand();
static int GetPoissionRand(double expectValue);
template<class ElemType>
void Swap(ElemType &e1,ElemType &e2)//交换e1,e2的值
{
	ElemType temp;
	temp=e1;
	e1=e2;
	e2=temp;
}
template<class ElemType>
void Display(ElemType elem[],int n)//显示数组各数据元素值
{
	for(int i=0;i<n;i++)
		cout<<elem[i]<<" ";
	cout<<endl;
}
template<class ElemType>
void Write(ElemType &e)//显示数据元素
{
	cout<<e<<" ";
}
class Timer
{
private:
	clock_t startTime;
public:
	Timer()
	{
		startTime=clock();
	}
	~Timer(){}
	double ElapsedTime()//返回已过的时间
	{
		clock_t endTime=clock();
		return (double)(endTime-startTime)/(double)CLK_TCK;
	}
	void  Reset()//重置开始的时间
	{
		startTime=clock();
	}
};
//通用的异常处理类
class Error
{
private:
	char message[MAX_ERROR_MESSAGE_LEN];//异常处理信息
public:
	Error(char mes[]="一般性异常!")
	{
		strcpy(message,mes);
	}
	~Error(){}
	void Show()const
	{
		cout<<message<<endl;
	}
};
#define DEFAULT_SIZE 100//默认元素个数
#define DEFAULT_INFINITY 1000000//默认无穷大

//自定义类型
enum StatusCode{SUCCESS,FAIL,UNDER_FLOW,OVER_FLOW,RANGE_ERROR,DUPLICATE_ERROR,NOT_PRESENT,ENTRY_INSERTED,ENTRY_FOUND,VISITED,UNVISITED};
//将函数说明为静态函数,使函数的作用域只局限于被包含的源程序文件,这样可以避免在一个程序中有多个源程序文件都包含改函数的定义时出现的程序连接错误
//getchar()函数
static char GetChar(istream &inStream)
{
	char ch;
	while((ch=(inStream).peek())!=EOF&&(ch=(inStream).get())==' '||ch=='\t')//peek函数从输入流中接受一个字符,流的当前位置不变,get函数从输入流中接受一个字符,流的当前位置向后移动一个位置
		;
	return ch;
}
//当用户肯定回答(yes)时,返回true;当用户否定回答(no)时,返回false
static bool UserSays_Yes()
{
	char ch;
	bool initialResponse=true;
	do
	{
		if(initialResponse)
			cout<<"(y,n)?";
		else
			cout<<"please answer y or n:";
		ch=GetChar(cin);
		initialResponse=false;
	}while(ch!='y'&&ch!='Y'&&ch!='n'&&ch!='N');
	if(ch=='y'||ch=='Y')
		return true;
	else
		return false;
}
//定时器类

//有关随机数的函数
static void SetRand_Seed()//设置当前时间为随机数种子
{
	srand((unsigned)time(NULL));
}
static int GetRand(int n)//生成0~n-1之间的随机数
{
	return rand()%(n);
}
static int GetRand()//生成随机数
{
	return rand();
}
static int GetPoissionRand(double expectValue)//生成期望值为expectValue的泊松随机数,泊松分布适用于事件驱动中模拟离散产生的事件,该函数可得到一个非负整数序列满足给定期希值的泊松分布
{
	double x=rand()/(double)(RAND_MAX+1);//X均匀分布于[0,1)
	int k=0;
	double p=exp(-expectValue);//p为泊松分布值
	double s=0;
	while(s<=x)
	{
		s+=p;
		k++;
		p=p*expectValue/k;
	}
	return k-1;
}
//其他实用函数
#endif
#include "utility.h"//调试时#error : "eh.h is only for C++!"如果文件名是.c改为.cpp,测试

#define NUM 280
int main()
{
	try//用try分装可能出现异常的代码
	{
		if(NUM>280)
			throw Error("NUM值太大了!");
		int a[NUM+1][NUM+1],b[NUM+1][NUM+1],c[NUM+1][NUM+1];
		bool isContinue=true;
		Timer objTimer;
		while(isContinue)
		{
			int i,j,k;
			SetRand_Seed();
			objTimer.Reset();
			for(i=1;i<=NUM;i++)
				for(j=1;j<=NUM;j++)
				{
					a[i][j]=GetRand();
					b[i][j]=GetRand();
				}
			//求c=ab
				for(i=1;i<=NUM;i++)
					for(j=1;j<=NUM;j++)
					{
						c[i][j]=0;
						for(k=1;k<=NUM;k++)
							c[i][j]=c[i][j]+a[i][k]*b[k][j];
					}
				cout<<"using time:"<<objTimer.ElapsedTime()<<"second."<<endl;
				cout<<"if you want to continue";
				isContinue=UserSays_Yes();
		}
	}
	catch(Error my_error)
	{
		my_error.Show();
	}
	system("PAUSE");
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值