关于一个程序运行时间测试的程序

youzhengdu 2010-12-02 07:02:43
//一个头文件utility
#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;
//定时器类timer
class timer
{
private:
clock_t starttime;
public:
timer();
~timer(void){};
double elapsedtime();
void reset();
};
timer::timer()
{
starttime=clock();
}
double timer::elapsedtime()
{
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[]="一般性异常");
~error(void){};
void show()const;
};
error::error(char*mes)
{
strcpy(message,mes);
}
void error::show() const

{
cout<<message<<endl;
}
//随机函数
void setrandseed()
{
srand((unsigned)time(NULL));
}
int getrand(int n)
{
return rand()%(n);
}
int getrand()
{
return rand();
}
//交互函数
bool usersaysyes()
{
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');
while (getchar()!='\n');
if(ch=='y'||ch=='Y')return true;
else return false;
}


//主函数

#include"utility.h"
#define NUM 280
int main(void)
{

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;
setrandseed();
objtimer.reset();

for(i=1;1<=NUM;i++)
for(j=1;j<=NUM;j++)
{
a[i][j]=getrand();
b[i][j]=getrand();
}

for(i=1;1<=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<<"用时:"<<objtimer.elapsedtime()<<"秒"<<endl;
cout<<"是否继续";
iscontinue=usersaysyes();

}
}
catch (error err)
{
err.show ();
}
system("pause");
return 0;
}


运行后 什么也显示不出来 请各位 高手指教 解决后马上结贴

...全文
210 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dengqibin 2010-12-02
  • 打赏
  • 举报
回复
//主函数


int main()
{
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;
setrandseed();
objtimer.reset();

for(i=1;i<=NUM;i++)//是i
{
for(j=1;j<=NUM;j++)
{
a[i][j]=getrand();
b[i][j]=getrand();
}
}

for(i=1;i<=NUM;i++)//是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<<"用时:"<<objtimer.elapsedtime()<<"秒"<<endl;
cout<<"是否继续";
iscontinue=usersaysyes();
}

}
}
catch (error err)
{
err.show ();
}
system("pause");
return 0
}
dengqibin 2010-12-02
  • 打赏
  • 举报
回复
for(i=1;1<=NUM;i++)//红的是i吧
for(j=1;j<=NUM;j++)
{
a[i][j]=getrand();
b[i][j]=getrand();
}

for(i=1;1<=NUM;i++)//红的是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];
}

65,206

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧