ACM常用C/C++函数

来源:互联网 发布:php java .net的区别 编辑:程序博客网 时间:2024/05/18 03:17

函数名: ceil

用 法: double ceil(double x);

功 能: 返回大于或者等于指定表达式的最小整数
头文件:math.h
说明:
float ceil ( float value )
返回不小于 value 的下一个整数,value 如果有小数部分则进一位。ceil() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大。

函数名: floor

用 法: double floor(double x);
功 能:  函数返回参数不大于传入值的最大整数(向下取整)
头文件:math.h
说明:
float ceil ( float value )

函数名:isdigit

用 法: int isdigit(char c)
功 能:检查参数是否为十进制数字字符
头文件:<ctype.h>(C)<cctype>(C++)
说明:int isdigit(char c)  用于判断字符c是否为数字,当c为数字0~9时,返回非零值,否则返回零(NULL)。 可以用一个字符数组循环判断每一项
是否为数字。
/* 找出字符串str中为阿拉伯数字0~9的字符*/#include<cctype> using namespace std; int main(){    string str = "123@#FDsP[e?";    for(int i = 0; str[i] != 0; ++i)    {        if(isdigit(str[i]))            cout << c << " is an digit character\n",str[i] );    }    return 0;}



函数名: islower

用 法: int islower(int c)
功 能:  检查参数c是否为小写英文字母
头文件:#include<ctype.h>
说明:若参数c为小写英文字母,则返回TRUE,否则返回NULL(0)。此为宏定义,非真正函数。
#include<ctype.h>main(){char str[]="123@#FDsP[e?";int i;for(i=0;str[i]!=0;i++)if(islower(str[i]))printf("%cisalower-casecharacter\n",str[i]);}执行s is a lower-case charactere is a lower-case character



函数名: isupper

原型:extern int isupper(int c);
头文件:ctype.h
功能:判断字符c是否为大写英文字母
说明:当参数c为大写英文字母(A-Z)时,返回非零值,否则返回零。
附加说明: 此为宏定义,非真正函数。
#include <ctype.h>#include <stdio.h>int main(){    char Test[]="a1B2c3D4";    char *pos;    pos=Test;    while(*pos!=0)    {        if(isupper(*pos))            printf("%c",*pos);        pos++;    }    printf("\n");    return 0;}输出:BD


函数名: strsub

头文件:string
C++中substr函数的用法#include<string>#include<iostream>using namespace std;main(){string s("12345asdf");string a=s.substr(0,5);       //获得字符串s中 从第0位开始的长度为5的字符串//默认时的长度为从开始位置到尾cout<<a<<endl;}输出结果为:12345

函数名: atof

表头文件 #include <stdlib.h>
定义函数 double atof(const char *nptr);
函数说明 atof()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。参数nptr字符串可包含正负号、小数点或E(e)来表示指数部分,如123.456或123e-2。
返回值 返回转换后的浮点型数。
#include<stdlib.h>#include<stdio.h>int main(){double d,c;char str[] = "123.456sfd";char str1[]="as245cdh" ;c=atof(str);d=atof(str1);printf("str=%s\nstr1=%s\nc=%lf\nd=%lf\n",str,str1,c,d);return 0;}执行:str=123.456sfdc=123.456000str1=as245cdhd=0.000000//可见开头只能跳过空格字符
原创粉丝点击