C++ 函数与库
一、cmath 库中一些基本函数
函数 | 功能 |
---|---|
abs(x) | 返回x的绝对值 |
sqrt(x) | 返回x的平方根 |
floor(x) | 返回小于或等于浮点数x的最大整数值 |
ceil(x) | 返回大于或等于浮点数x的最小整数值 |
exp(x) | 返回x的指数函数值 e^x |
log(x) | 返回x的自然对数值 |
log10(x) | 返回x的常用对数值 |
pow(x,y) | 返回x的y次方的值 |
cos(theta) | 返回角θ的余弦值,θ为弧度,可用θ*π/180转换为度 |
sin(theta) | 返回弧度θ的正弦值 |
tan(theta) | 返回弧度θ的正切值 |
atan(x) | 返回弧度θ的反正切值 |
atan2(y,x) | 返回x轴与原点和点(x,y)所形成的直线的夹角的弧度值 |
二、程序错误报告
1. 定义error函数
代码如下:
#include <cstdlib>
#include <string>
void error(string msg){
cerr << msg << endl; // cerr() 用于报告错误
exit(EXIT_FAILURE); // exit() 函数可以立即终止主程序执行, EXIT_FAILURE 在 <cstdlib> 库中
}
三、接口与实现
1. 定义error库
定义c++库,首先定义接口,它让库用户在不了解库实现细节情况下使用库中函数;其次定义库具体实现,说明库的底层实现细节。定义接口文件后缀名 .h,定义实现文件后缀名 .cpp 。
error库接口:
#ifndef _error_h // 指令检查 _error_h 是否已被定义
#define _error_h
void error(std::string msg);
#endif
}
error库实现:
#include <iostream>
#include <cstdlib>
#include <string>
#include "error.h"
using namespace std;
void error(string msg){
cerr << msg << endl;
exit(EXIT_FAILURE);
}
2. 导出数据类型
以Direction类型编码四个方向为例 enum Direction { NORTH, EAST, SOUTH, WEST }
direction库的接口:
#ifndef _direction_h
#define _direction_h
#include <string>
enum Direction { NORTH, EAST, SOUTH, WEST };
Direction leftFrom(Direction dir);
Direction rightFrom(Direction dir);
std::string directionToString(Direction dir);
#endif
direction库的实现:
#include <string>
#include "direction.h"
using namespace std;
Direction leftFrom(Direction dir){
return Direction((dir + 3) % 4 );
}
Direction leftFrom(Direction dir){
return Direction((dir + 1) % 4 );
}
string directionToString(Direction dir){
switch(dir){
case NORTH: return "NORTH";
case EAST: return "EAST";
case SOUTH: return "SOUTH";
case WEST: return "WSET";
default : return "error";
}
}
3. 导出常量定义
如果为了能从接口中导出常量 PI ,需要在其接口的常量定义声明和原型中都都加上关键字 extern 。
gmath库简化的接口:
#ifndef _gmath_h
#define _gmath_h
extern const double PI;
double sinDegrees(double angle);
double cosDegrees(double angle);
double toDegrees(double radians);
double toRadians(double degrees);
#endif
gmath库的实现:
#include <cmath>
#include "gmath.h"
extern const double PI = 3.1415926;
double sinDegrees(double angle){
return sin(toRadians(angle));
}
double cosDegrees(double angle){
return cos(toRadians(angle));
}
double toDegrees(double radians){
return radians * 180 / PI;
}
double toRadians(double degrees){
return degrees * PI / 180;
}
四、随机数库的设计
1. 标准库中的伪随机数
<cstdlib> 类库提供了函数 rand,其原型为: int rand() ;
返回结果为 0 ~ RAND_MAX 之间任意整数,RAND_MAX 通常定义为整形最大值。
2. 选择正确的函数集
random.h 用户期望功能:
- 从一个特定的区域内选取随机整数
- 从一个特定区域内选取随机实数
- 以某个特定概率模拟一个随机事件
据此随机数库接口如下:
#ifndef _random_h
#define _random_h
int randomInteger(int low, int high);
double randomReal(double low, double high); // [low, high)
bool randomChance(double p);
void setRandomSeed(int seed);
#endif
3.随机数库的实现
利用 rand() 函数,对值进行规范化、量化、翻译、转换为整数的处理。
考虑随机数种子,及一系列复杂情况,随机数库实现如下:
#include <cstdlib>
#include <cmath>
#include <ctime>
#include "random.h"
using namespace std;
void initRandomSeed();
int randomInteger(int low, int high){
initRandomSeed();
double d = rand() / (double(RAND_MAX) + 1);
double s = d * (double(high) - low + 1); //此处两句转换为double型为防止整形数据溢出!
return int(floor(low + s));
}
double randomReal(double low, double high){
initRandomSeed();
double d = rand() / (double(RAND_MAX) + 1);
double s = d * (high - low);
return low + s;
}
bool randomChance(double p){
initRandomSeed();
return randomReal(0, 1) < p;
}
void setRandomSeed(int seed){
initRandomSeed();
srand(seed);
}
void initRandomSeed(){
static bool initialized = false;
if(!initialized){
srand(int(time(NULL)));
initialized = true;
}
}
学习记录,便于以后查阅
参考
《C++程序设计 基础变成抽象与算法策略》 Eric S.Roberts 机械工业出版社