//对一些简单系统函数的介绍和实现!!
/*系统函数
1、数学函数:需要#include<cmath>支持,包括log、log10、 pow、 pow10、 sqrt、 acos、 asin、 atan、 cos、 sin、 tan等。
2、时间函数:需要#include<ctime>的支持。调用time(0)将返回自格林威治时间1970年1月1日凌晨至现在所经过的秒数。
3、随机数函数:需要#include<cstdlib>支持。
(1)void srand(unsigned seed)用于初始化随机数发生器,参数seed称作为“种子数”,用不同的种子数可以产生不一样的序列,故time(0)的返回值常用作种子数。
(2)int rand()产生一个0到RAND_MAX(一个系统内已经定义的很大的数)的整型随机数并返回。
*/
#include<iostream>
#include<string>
#include<ctime>
#include<cmath>
#include<cstdlib>
using namespace std;
int main()
{
int i,ran_num;
double t1,t2;
double PI=3.1415926;
double NUM=100;
t1=time(0);
//随机数函数应用-产生10个随机数。
srand(time(0)); //time(0)返回值作种子,保证每次输出的随机数的结果不同。
for(i=0; i<10; i++)
{
ran_num=rand()%6; //rand()返回值对6取余保证了随机数为0~5。
cout<<ran_num<<" ";
}
cout<<endl;
//数学函数的应用
double sin_result=sin(PI);
double cos_result=cos(PI);
double tan_result=tan(PI);
double log_result=log(NUM);
double log10_result=log10(NUM);
double pow_result=pow(NUM,NUM);
cout<<"sin_result:"<<sin(PI)<<endl;
cout<<"cos_result:"<<cos(PI)<<endl;
cout<<"tan_result:"<<tan(PI)<<endl;
cout<<"log_result:"<<log(NUM)<<endl;
cout<<"log10_result:"<<log10(NUM)<<endl;
cout<<"pow_result:"<<pow(NUM,NUM)<<endl;
//时间函数
t2=time(0);
cout<<"运行耗时"<<t2-t1<<"秒!!!"<<endl;
system("pause");
return 0;
}
输出结果: