继续更新这个超细的文章,关于头文件的看这篇文章:
C/C++头文件汇总
我们这次看一下常用的函数有哪些。
注意,一下函数都有自己的头文件,使用的时候不要忘记添加,不然编译错误。(或者直接万能头#include
(1):isalpha(ch) 若ch是字母返回非0值,否则返回0:
#include
using namespace std;
int main()
{
char c1 = 'a';
char c2 = '1';
cout << isalpha(c1) << endl;
cout << isalpha(c2) << endl;
return 0;
}
(2):isdigit(ch) 若ch是数字返回非0值,否则返回0:
#include
using namespace std;
int main()
{
char c1 = 'a';
char c2 = '1';
cout << isdigit(c1) << endl;
cout << isdigit(c2) << endl;
return 0;
}
(3):tolower(ch) 若ch是大写字母返回相应的小写字母:
(4):toupper(ch) 若ch是小写字母返回相应的大写字母:
#include
using namespace std;
int main()
{
char c1 = 'a';
char c2 = 'B';
printf("%c\n%c",tolower(c2), toupper(c1));
return 0;
}
(5):abs(num)返回整型的绝对值:
#include
using namespace std;
int main()
{
int a1 = 1;
int a2 = 0;
int a3 = -1;
cout << abs(a1) << endl;
cout << abs(a2) << endl;
cout << abs(a3) << endl;
return 0;
}
(6):fabs(num)返回双精度的绝对值:
#include
using namespace std;
int main()
{
double a1 = 1.5;
double a2 = 0.3;
double a3 = -1.7;
cout << fabs(a1) << endl;
cout << fabs(a2) << endl;
cout << fabs(a3) << endl;
return 0;
}
(7):exp(x)返回指数函数ex的值:
#include
using namespace std;
int main()
{
int x = 1;
cout << exp(x) << endl;
return 0;
}
(8):log(x)返回lnx的值:
#include
using namespace std;
int main()
{
cout << log(exp(1)) << endl;
return 0;
}
(9):log10(x)返回lgx的值:
#include
using namespace std;
int main()
{
cout << log10(100) << endl;
return 0;
}
(10):pow(x, y)返回xy的值:
#include
using namespace std;
int main()
{
cout << pow(2, 3) << endl;
return 0;
}
(11):sqrt(x)返回√x的值:
#include
using namespace std;
int main()
{
cout << sqrt(25) << endl;
return 0;
}
(12):acos(x)返回x的反余弦cos-1(x)值:
(13):asin(x)返回x的反正弦sin-1(x)值:
(14):atan(x)返回x的反正切tan-1(x)值:
#include
using namespace std;
int main()
{
cout << acos(-1) << endl;
return 0;
}
(15):cos(x)返回x的余弦cos(x)值:
(16):sin(x)返回x的正弦sin(x)值:
(17):tan(x)返回x的正切tan(x)值:
#include
using namespace std;
int main()
{
cout << sin(acos(-1)/2) << endl;
return 0;
}
(18):ceil(x)返回不小于x的最小整数:
(19):floor(x)返回不大于x的最大整数:
#include
using namespace std;
int main()
{
double a = 1.3;
cout << ceil(a) << endl;
cout << floor(a) << endl;
return 0;
}
(20):rand()产生一个随机数并返回这个数:
#include
using namespace std;
int main()
{
cout << rand() << endl;
return 0;
}
(21):fmod(x, y)返回x/y的余数:
#include
using namespace std;
int main()
{
cout << fmod(10, 3) << endl;
return 0;
}
(22):fgetchar()从控制台(键盘)读一个字符,显示在屏幕上:
(23):getch()从控制台(键盘)读一个字符,不显示在屏幕上:
(24):putch()向控制台(键盘)写一个字符:
(25):getchar()从控制台(键盘)读一个字符,显示在屏幕上:
(26):putchar()向控制台(键盘)写一个字符:
(27):getche()从控制台(键盘)读一个字符,显示在屏幕上:
#include
#include
using namespace std;
int main()
{
//fgetchar();
//getch();
//putch('a');
//getchar();
//putchar('A');
//getche();
return 0;
}
(28):scanf(char*format[,argument…])从控制台读入一个字符串,分别对各个参数进行赋值,使用BIOS进行输出:
(29):sscanf(char string,charformat[,argument,…])通过字符串string,分别对各个参数进行赋值:
#include
#include
using namespace std;
int main()
{
char s[100] = "12345";
char a[100];
sscanf(s, "%s", a);
cout << a;
return 0;
}
(30):puts(char*string) 发送一个字符串string给控制台(显示器),使用BIOS进行输出:
#include
#include
using namespace std;
int main()
{
puts("hello world.");
return 0;
}
(31):printf(char*format[,argument,…]) 发送格式化字符串输出给控制台(显示器)使用BIOS进行输出:
(32):sprintf(char*string,char *format[,argument,…])将字符串string的内容重新写为格式化后的字符串:
#include
#include
using namespace std;
int main()
{
char s[100];
char c[100] = "12345";
sprintf(s,"%s",c);
cout << s;
return 0;
}
(33):fclose(FILE *stream)关闭一个流,可以是文件或设备(例如LPT1):
(34):fflush(FILE *stream)关闭一个流,并对缓冲区作处理处理即对读的流,将流内内容读入缓冲区;对写的流,将缓冲区内内容写入流。成功返回0:
交互题中经常使用到fflush刷新缓冲区,不然会出现特殊的错误(懒惰错误)。
#include
#include
using namespace std;
int main()
{
char c;
scanf("%c", &c);
printf("%d\n", c);
fflush(stdin);
scanf("%c", &c);
printf("%d\n", c);
return 0;
}