收藏 分享(赏)

c++的头文件和经典程序代码大全.pdf

上传人:HR专家 文档编号:6233782 上传时间:2019-04-03 格式:PDF 页数:156 大小:513.55KB
下载 相关 举报
c++的头文件和经典程序代码大全.pdf_第1页
第1页 / 共156页
c++的头文件和经典程序代码大全.pdf_第2页
第2页 / 共156页
c++的头文件和经典程序代码大全.pdf_第3页
第3页 / 共156页
c++的头文件和经典程序代码大全.pdf_第4页
第4页 / 共156页
c++的头文件和经典程序代码大全.pdf_第5页
第5页 / 共156页
点击查看更多>>
资源描述

1、 1 C/C+头文件一览 C、传统 C+ #include /设定插入点 使用断言 assert()宏是用于保证满足某个特定条件,用法是: assert(表达式 ); 如果表达式的值为假,整个程序将退出,并输出一条错误信息。如果表达式的值为真则继续执行后面的语句。 使用这个宏前需要包含头文件 assert.h 例如 #include #include void main() float a,b; scan(“%f %f“, assert(b!=0); printf(“%fn“,a/b); 以上的程序要计算 A/B 的值,因此要求 b!=0,所以在程序中使用了 assert()用于确保 b!=0

2、,如果 b=0,则程序会退出。 #include /字符处理 isalnum 判断一个字符是否是字符类的数字或者字母 isalpha 判断一个字符是否是字母 isblank 判断一个字符是空白字符(空格和水平制表符 Tab) iscntrl 判断一个控制符( ascii 码 0-31 之间的字符) isdigit 判断一个字符是否是字符类的数字 isgraph 判断一个字符是否是可打印字符( ascii 码 33-126 之间的字符) islower 判断一个字符是否是小写字母 isprint 判断一个字符是否是包含空格在内的可打印字符( ascii 码 32-126 之间的字符) ispun

3、ct 判断一个字符是否是除空格,字母,数字外的标点符号 isspace 判断一个字符是空白字符(空格,换行符 (n),走纸符 (f),回车符 (r),垂直制表符 (v),水平制表符 (t)) isupper 判断一个字符是否是大写字母 isxdigit 判断一个字符是否是一个十六进制的数字 tolower 将大些字符转换为小写 toupper 将小写字符转换为大写 isalnum()函数的作用是判断一个字符是否是字符类的数字或者字母: #include #include int main(void) if(isalnum(a) printf(“Its True“); / 显示 Its True

4、 if(isalnum(4) printf(“Its True“); / 显示 if(isalnum(4) printf(“Its True“); / 显示 Its True return 0; isalpha()函数的作用是判断一个字符是否是字母: #include #include int main(void) if(isalpha(a) printf(“Its True“); / 显示 Its True if(isalpha(4) printf(“Its True“); / 显示 if(isalpha(4) printf(“Its True“); / 显示 2 return 0; isb

5、lank()函数的作用是判断一个字符是空白字符(空格和水平制表符 Tab), isspace()函数和 isblank()函数类似,但是还包含空格,换行符 (n),走纸符 (f),回车符 (r),垂直制表符 (v),水平制表符 (t): #include #include int main(void) if(isblank( ) / 空格 printf(“Its True“); / 显示 Its True if(isblank(t) / Tab printf(“Its True“); / 显示 Its True if(isblank(n) / 换行 printf(“Its True“); /

6、显示 if(isblank(r) / 回车 printf(“Its True“); / 显示 if(isspace( ) / 空格 printf(“Its True“); / 显示 Its True if(isspace(t) / Tab printf(“Its True“); / 显示 Its True if(isspace(n) / 换行 printf(“Its True“); / 显示 Its True if(isspace(r) / 回车 printf(“Its True“); / 显示 Its True return 0; iscntrl()函数的作用是判断一个控制符( ascii

7、码 0-31 之间的字符): #include #include int main(void) if(isblank( ) / 空格 printf(“Its True“); / 显示 if(isblank(t) / Tab printf(“Its True“); / 显示 Its True if(isblank(n) / 换行 printf(“Its True“); / 显示 Its True if(isblank(r) / 回车 printf(“Its True“); / 显示 Its True return 0; isdigit()函数的作用是判断一个字符是否是字符类的数字: #inclu

8、de #include int main(void) if(isdigit(4) printf(“Its True“); / 显示 Its True if(isdigit(4) printf(“Its True“); / 显示 if(isdigit(a) printf(“Its True“); / 显示 return 0; isgraph()函数的作用是判断一个字符是否是可打印字符( ascii 码 33-126 之间的字符), isprint()函数功能和 isgraph()函数类似,区别是 isprint()函数包含空格在内( ascii 码 32-126 之间的字符): #include

9、 #include int main(void) 3 if(isgraph(a) printf(“Its True“); / 显示 Its True if(isgraph(.) printf(“Its True“); / 显示 Its True if(isgraph( ) / 空格 printf(“Its True“); / 显示 if(isprint(a) printf(“Its True“); / 显示 Its True if(isprint(.) printf(“Its True“); / 显示 Its True if(isprint( ) / 空格 printf(“Its True“)

10、; / 显示 Its True return 0; islower()函数的作用是判断一个字符是否是小写字母, isupper()函数的作用是判断一个字符是否是大写字母: #include #include int main(void) if(islower(a) printf(“Its True“); / 显示 Its True if(islower(A) printf(“Its True“); / 显示 if(isupper(a) printf(“Its True“); / 显示 if(isupper(A) printf(“Its True“); / 显示 Its True return

11、0; ispunct()函数的作用是判断一个字符是否是除空格,字母,数字外的标点符号: #include #include int main(void) if(ispunct(a) printf(“Its True“); / 显示 if(ispunct(.) printf(“Its True“); / 显示 Its True if(ispunct( #include int main(void) if(isxdigit(4) printf(“Its True“); / 显示 Its True if(isxdigit(xE) printf(“Its True“); / 显示 Its True i

12、f(isxdigit(xF) printf(“Its True“); / 显示 return 0; tolower()函数的作用是将大些字符转换为小写, toupper()函数的作用是将小写字符转换为大写: #include #include int main(void) 4 char n,m,i,j; n = tolower(A); m = tolower(a); i = toupper(a); j = toupper(.); printf(“%c %c %c %c“, n, m, i, j); / 显示 a a A . return 0; #include /定义错误码 #include

13、/浮点数处理 #include /文件输入输出 #include /参数化输入输出 #include /数据流输入输出 #include /定义各种数据类型最值常量 #include /定义本地化函数 #include /定义数学函数 #include /定义输入输出函数 下面的类型,宏,函数都是分类的 其他: size_t sizeof 返回的值 NULL 空指针 文件: FILE 文件的类型 fpos_t 文件中指针的位置 EOF 文件末尾 0 FOPEN_MAX 同时打开文件的最大值 8 SEEK_SET 文件头 SEEK_CUR 文件当前位置 SEEK_END 文件末尾 打开文件 FI

14、LE *fopen(const char *filename,const char *mode); 更改当前流相关的文件 FILE *freopen(const char *filename,const char *mode,FILE *stream); 关闭文件 int fclose(FILE *stream); 清除流中的错误标志或文件末尾标志 void clearerr(FILE *stream); 测试流上的文件末尾标志 int feof(FILE *stream); 测试流上的错误标志 int ferror(FILE *stream); 将一个字符放回到流中 int ungetc(i

15、nt c, FILE *stream); 从流中读一个字符 int fgetc(FILE *stream); int getc(FILE *stream);/* 与 fgetc 相同但是可以用宏实现该函数 */ 写一个字符到一个流 int fputc(int c, FILE *stream); int putc(int c, FILE *stream); 从流中获取一个字符串 char *fgets(char *s, int n, FILE *stream); 写一个字符串到一个流 int fputs(const char *s, FILE *stream); 打印一个格式化数据到一个流 in

16、t fprintf(FILE *stream,const char *format, .); 使用一个参量列表指针格式化到流的数据 int vfprintf(FILE *stream,const char *format, va_list ap); 从一个流中读取格式化数据 int fscanf(FILE *stream, const char *format, .); 从一个流中读数据 5 size_t fread(char *buffer,size_t size,size_t count,FILE *stream); 写数据到一个流 int fwrite(const char *buffe

17、r, size_t size, size_t count, FILE *stream); 获取流的文件位置指示符 int fgetpos(FILE *stream, fpos_t *pos); 设置流位置指示符 int fsetpos(FILE *stream, const fpos_t *pos); 移动文件指针到一个指定的位置 int fseek(FILE *stream, long offset, int origin); 获得文件指针相对于文件头的偏移量 long ftell(FILE *stream); 重新定位一个文件指针到文件开头 void rewind(FILE *steam)

18、; 删除一个文件 int remove(const char *path); 更改一个文件或目录 int rename(const char *oldname, const char *newname); 缓冲区 : _IOFBF _IOLBF _IONBF 缓冲区类型 BUFSIZE 缓冲区尺寸 =256 刷新一个流并清空与流相关的缓冲区的内容 int fflush(FILE *stream); 控制流的缓冲区 ,已经被 setvbuf 代替 void setbuf(FILE *stream, char *buffer); 控制流的缓冲区类型和缓冲区大小 int setvbuf(FILE *

19、stream, char *buffer, int mode, size_t size); 将一个格式化数据写入一个字符串 int sprintf(char *buffer, const char *format, .); 从字符串中读格式化数据 int sscanf(const char *buffer, const char *format, .); 从参量列表指针格式化到字符串 int vsprintf(char *buffer, const char *format, va_list ap); 临时文件 L_tmpnam 临时文件名长度 0 TMP_MAX 产生唯一文件名的最大数目 =

20、25 以二进制读写的方式建立一个临时文件 FILE *tmpfile(void); 建立一个临时文件名 char *tmpname(char *string); 标准流 : stdin 标准输入流 stdout 标准输出流 stderr 标准错误输出流 从 stdin 获得一个字符 int getchar(void); 把字符写道 stdout int putchar(int c); 从 stdin 中获取一行 char *gets(char *buffer); 写一个字符串到 stdout int puts(const char *string); 打印一个错误消息到 stderr void

21、 perror(const char *error); 打印格式化数据到 stdout int printf(const char *format, .); 从 stdin 读格式化数据 int scanf(const char *format, .); 6 从参量列表指针格式化到 stdout int vprintf(const char *format, va_list ap); #include /定义杂项函数及内存分配函数 #include /字符串处理 #include /基于数组的输入输出 #include /定义关于时间的函数 #include /宽字符处理及输入输出 #incl

22、ude /宽字符分类 标准 C+ (同上的不再注释) #include /STL 通用算法 #include /STL 位集容器 #include #include #include #include #include /复数类 #include #include #include #include #include /STL 双端队列容器 #include /异常处理类 #include #include /STL 定义运算函数(代替运算符) #include #include /STL 线性列表容器 #include /STL 映射容器 #include #include /基本输入输出支

23、持 #include /输入输出系统使用的前置声明 #include #include /基本输入流 #include /基本输出流 #include /STL 队列容器 #include /STL 集合容器 #include /基于字符串的流 #include /STL 堆栈容器 #include /标准异常类 #include /底层输入输出支持 #include /字符串类 #include /STL 通用模板类 #include /STL 动态数组容器 #include #include using namespace std; / C99 增加 #include /复数处理 #inc

24、lude /浮点环境 #include /整数格式转换 #include /布尔环境 #include /整型环境 #include /通用类型数学宏 C 头文件大全 7 分类函数 ,所在函数库为 ctype.h int isalpha(int ch) 若 ch 是字母 (A-Z,a-z)返回非 0 值 ,否则返回 0 int isalnum(int ch) 若 ch 是字母 (A-Z,a-z)或数字 (0-9) 返回非 0 值 ,否则返回 0 int isascii(int ch) 若 ch 是字符 (ASCII 码中的 0-127)返回非 0 值 ,否则返回 0 int iscntrl(i

25、nt ch) 若 ch 是作废字符 (0x7F)或普通控制字符 (0x00-0x1F) 返回非 0 值 ,否则返回 0 int isdigit(int ch) 若 ch 是数字 (0-9)返回非 0 值 ,否则返回 0 int isgraph(int ch) 若 ch 是可打印字符 (不含空格 )(0x21-0x7E)返回非 0 值 ,否则返回 0 int islower(int ch) 若 ch 是小写字母 (a-z)返回非 0 值 ,否则返回 0 int isprint(int ch) 若 ch 是可打印字符 (含空格 )(0x20-0x7E)返回非 0 值 ,否则返回 0 int isp

26、unct(int ch) 若 ch 是标点字符 (0x00-0x1F)返回非 0 值 ,否则返回 0 int isspace(int ch) 若 ch 是空格 ( ),水平制表符 (t),回车符 (r), 走纸换行 (f),垂直制表符 (v),换行符 (n) 返回非 0 值 ,否则返回 0 int isupper(int ch) 若 ch 是大写字母 (A-Z)返回非 0 值 ,否则返回 0 int isxdigit(int ch) 若 ch 是 16 进制数 (0-9,A-F,a-f)返回非 0 值 , 否则返回 0 int tolower(int ch) 若 ch 是大写字母 (A-Z)返

27、回相应的小写字母 (a-z) int toupper(int ch) 若 ch 是小写字母 (a-z)返回相应的大写字母 (A-Z) 数学函数 ,所在函数库为 math.h、 stdlib.h、 string.h、 float.h int abs(int i) 返回整型参数 i 的绝对值 double cabs(struct complex znum) 返回复数 znum 的绝对值 double fabs(double x) 返回双精度参数 x 的绝对值 long labs(long n) 返回长整型参数 n 的绝对值 double exp(double x) 返回指数函数 ex 的值 dou

28、ble frexp(double value,int *eptr) 返回 value=x*2n 中 x 的值 ,n 存贮在 eptr 中 double ldexp(double value,int exp); 返回 value*2exp 的值 double log(double x) 返回 logex 的值 double log10(double x) 返回 log10x 的值 double pow(double x,double y) 返回 xy 的值 double pow10(int p) 返回 10p 的值 double sqrt(double x) 返回 + x 的值 double a

29、cos(double x) 返回 x 的反余弦 cos-1(x)值 ,x 为弧度 double asin(double x) 返回 x 的反正弦 sin-1(x)值 ,x 为弧度 double atan(double x) 返回 x 的反正切 tan-1(x)值 ,x 为弧度 double atan2(double y,double x) 返回 y/x 的反正切 tan-1(x)值 ,y 的 x 为弧度 double cos(double x) 返回 x 的余弦 cos(x)值 ,x 为弧度 double sin(double x) 返回 x 的正弦 sin(x)值 ,x 为弧度 double

30、 tan(double x) 返回 x 的正切 tan(x)值 ,x 为弧度 double cosh(double x) 返回 x 的双曲余弦 cosh(x)值 ,x 为弧度 double sinh(double x) 返回 x 的双曲正弦 sinh(x)值 ,x 为弧度 double tanh(double x) 返回 x 的双曲正切 tanh(x)值 ,x 为弧度 double hypot(double x,double y) 返回直角三角形斜边的长度 (z), x和 y 为直角边的长度 ,z2=x2+y2 double ceil(double x) 返回不小于 x 的最小整数 doubl

31、e floor(double x) 返回不大于 x 的最大整数 void srand(unsigned seed) 初始化随机数发生器 int rand() 产生一个随机数并返回这个数 double poly(double x,int n,double c)从参数产生一个多项式 double modf(double value,double *iptr)将双精度数 value 分解成尾数和阶 double fmod(double x,double y) 返回 x/y 的余数 double frexp(double value,int *eptr) 将双精度数 value 分成尾数和阶 doub

32、le atof(char *nptr) 将字符串 nptr 转换成浮点数并返回这个浮点数 double atoi(char *nptr) 将字符串 nptr 转换成整数并返回这个整数 double atol(char *nptr) 将字符串 nptr 转换成长整数并返回这个整数 char *ecvt(double value,int ndigit,int *decpt,int *sign) 将浮点数 value 转换成字符串并返回该字符串 8 char *fcvt(double value,int ndigit,int *decpt,int *sign) 将浮点数 value 转换成字符串并返

33、回该字符串 char *gcvt(double value,int ndigit,char *buf) 将数 value 转换成字符串并存于 buf 中 ,并返回 buf 的指针 char *ultoa(unsigned long value,char *string,int radix) 将无符号整型数 value 转换成字符串并返回该字符串 ,radix 为转换时所用基数 char *ltoa(long value,char *string,int radix) 将长整型数 value 转换成字符串并返回该字符串 ,radix 为转换时所用基数 char *itoa(int value,c

34、har *string,int radix) 将整数 value 转换成字符串存入 string,radix 为转换时所用基数 double atof(char *nptr) 将字符串 nptr 转换成双精度数 ,并返回这个数 ,错误返回 0 int atoi(char *nptr) 将字符串 nptr 转换成整型数 , 并返回这个数 ,错误返回 0 long atol(char *nptr) 将字符串 nptr 转换成长整型数 ,并返回这个数 ,错误返回 0 double strtod(char *str,char *endptr)将字符串 str 转换成双精度数 ,并返回这个数 , lon

35、g strtol(char *str,char *endptr,int base)将字符串 str 转换成长整型数 , 并返回这个数 , int matherr(struct exception *e) 用户修改数学错误返回信息函数 (没有必要使用 ) double _matherr(_mexcep why,char *fun,double *arg1p, double *arg2p,double retval) 用户修改数学错误返回信息函数 (没有必要使用 ) unsigned int _clear87() 清除浮点状态字并返回原来的浮点状态 void _fpreset() 重新初使化浮点数

36、学程序包 unsigned int _status87() 返回浮点状态字 目录函数 ,所在函数库为 dir.h、 dos.h int chdir(char *path) 使指定的目录 path(如 :“C:WPS“)变成当前的工作目录 ,成 功返回 0 int findfirst(char *pathname,struct ffblk *ffblk,int attrib)查找指定的文件 ,成功 返回 0 pathname 为指定的目录名和文件名 ,如 “C:WPSTXT“ ffblk为指定的保存文件信息的一个结构 ,定义如下 : struct ffblk char ff_reserved21

37、; /*DOS 保留字 */ char ff_attrib; /*文件属性 */ int ff_ftime; /*文件时间 */ int ff_fdate; /*文件日期 */ long ff_fsize; /*文件长度 */ char ff_name13; /*文件名 */ attrib 为文件属性 ,由以下字符代表 FA_RDONLY 只读文件 FA_LABEL 卷标号 FA_HIDDEN 隐藏文件 FA_DIREC 目录 FA_SYSTEM 系统文件 FA_ARCH 档案 例 : struct ffblk ff; findfirst(“*.wps“, int findnext(struc

38、t ffblk *ffblk) 取匹配 finddirst 的文件 ,成功返回 0 void fumerge(char *path,char *drive,char *dir,char *name,char *ext) 此函数通过盘符 drive(C:、 A:等 ),路径 dir(TC、 BCLIB 等 ), 文件名 name(TC、 WPS 等 ),扩展名 ext(.EXE、 .COM 等 )组成一个文件名 存与 path 中 . int fnsplit(char *path,char *drive,char *dir,char *name,char *ext) 此函数将文件名 path 分

39、解成盘符 drive(C:、 A:等 ),路径 dir(TC、 BCLIB 等 ), 9 文件名 name(TC、 WPS 等 ),扩展名 ext(.EXE、 .COM 等 ),并分别存入相应的变量中 . int getcurdir(int drive,char *direc) 此函数返回指定驱动器的当前工作目录名称 drive 指定的驱动器 (0=当前 ,1=A,2=B,3=C 等 ) direc 保存指定驱动器当前工作路径的变量 成功返回 0 char *getcwd(char *buf,iint n) 此函数取当前工作目录并存入 buf 中 ,直到 n 个字 节长为为止 .错误返回 NU

40、LL int getdisk() 取当前正在使用的驱动器 ,返回一个整数 (0=A,1=B,2=C 等 ) int setdisk(int drive) 设置要使用的驱动器 drive(0=A,1=B,2=C 等 ), 返回可使用驱动器总数 int mkdir(char *pathname) 建立一个新的目录 pathname,成功返回 0 int rmdir(char *pathname) 删除一个目录 pathname,成功返回 0 char *mktemp(char *template) 构造一个当前目录上没有的文件名并存于 template 中 char *searchpath(cha

41、r *pathname) 利用 MSDOS 找出文件 filename 所在路径 , ,此函数使用 DOS 的 PATH 变量 ,未找到文件返回 NULL 进程函数 ,所在函数库为 stdlib.h、 process.h void abort() 此函数通过调用具有出口代码 3 的 _exit 写一个终止信息于 stderr, 并异常终止程序。无返回值 int exec装入和运行其它程序 int execl( char *pathname,char *arg0,char *arg1,char *argn,NULL) int execle( char *pathname,char *arg0,c

42、har *arg1, char *argn,NULL,char *envp) int execlp( char *pathname,char *arg0,char *arg1,NULL) int execlpe(char *pathname,char *arg0,char *arg1,NULL,char *envp) int execv( char *pathname,char *argv) int execve( char *pathname,char *argv,char *envp) int execvp( char *pathname,char *argv) int execvpe(c

43、har *pathname,char *argv,char *envp) exec函数族装入并运行程序 pathname,并将参数 arg0(arg1,arg2,argv,envp)传递给子程序 ,出错返回 -1 在 exec 函数族中 ,后缀 l、 v、 p、 e 添加到 exec 后, 所指定的函数将具有某种操作能力 有后缀 p时,函数可以利用 DOS 的 PATH 变量查找子程序文件。 l时,函数中被传递的参数个数固定。 v时,函数中被传递的参数个数不固定。 e时,函数传递指定参数 envp,允许改变子进程的环境, 无后缀 e 时,子进程使用当前程序的环境。 void _exit(int

44、 status)终止当前程序 ,但不清理现场 void exit(int status) 终止当前程序 ,关闭所有文件 ,写缓冲区的输出 (等待输出 ), 并调用任何寄存器的 “出口函数 “,无返回值 int spawn运行子程序 int spawnl( int mode,char *pathname,char *arg0,char *arg1, char *argn,NULL) int spawnle( int mode,char *pathname,char *arg0,char *arg1, char *argn,NULL,char *envp) int spawnlp( int mod

45、e,char *pathname,char *arg0,char *arg1, char *argn,NULL) int spawnlpe(int mode,char *pathname,char *arg0,char *arg1, char *argn,NULL,char *envp) int spawnv( int mode,char *pathname,char *argv) int spawnve( int mode,char *pathname,char *argv,char *envp) int spawnvp( int mode,char *pathname,char *argv) int spawnvpe(int mode,char *pathname,char *argv,char *envp) spawn函数族在 mode 模式下运行子程序 pathname,并将参数 arg0(arg1,arg2,argv,envp)传递给子程序 .出错返回 -1 mode为运行模式 mode为 P_WAIT 表示在子程序运行完后返回本程序 P_NOWAIT 表示在子程序运行时同时运行本程序 (不可用 ) 10P_OVERLAY表示在本程序退出后运行子程序 在 spawn 函数族中 ,后缀 l、 v、 p、 e 添加到 spawn 后, 所指定的函数将具有某种操作能

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报