在這里將介紹大量的函數(shù)和宏,但并不會(huì)全部給出詳細(xì)的例子。這是因?yàn)楹芏嗪瘮?shù)的用法都非常相似。
1、<cassert>頭文件
<cassert>頭文件是用來調(diào)試程序的。該文件中定義了assert宏,即使加入調(diào)試代碼。
使用assert宏,可以再程序調(diào)試階段加入確認(rèn)代碼,assert宏斷言某個(gè)條件為真;一旦條件不滿足,assert宏就顯示這個(gè)條件,并指出程序中何處沒有通過這個(gè)條件測試,然后終止程序。
#include <iostream>
#include <cassert>
void DisplayMsg(char* msg);
int main()
{
char* cp = 0;
DisplayMsg(cp);
return 0;
}
void DisplayMsg(char *msg)
{
assert(msg != 0);
std::cout << msg;
}
2、<cctype>頭文件
<cctype>頭文件中聲明的函數(shù)用于轉(zhuǎn)換字符變量并測試其是否在給定范圍之內(nèi)。
int isdigit(int c); |
當(dāng)c是數(shù)字(0~9)返回真 |
int isupper(int c); |
當(dāng)c是大寫字母(A~Z)時(shí)返回真 |
int islower(int c); |
當(dāng)c是小寫字母(a~z)時(shí)返回真 |
int isalpha(int c); |
當(dāng)c是字母(a~z,A~Z)時(shí)返回真 |
int isalnum(int c); |
與isalpha(c)一樣 |
int isprint(int c); |
當(dāng)c是可顯示的ASCII字符時(shí)返回真
|
int isspace(int c);
|
當(dāng)c是空字符時(shí)返回真 |
int toupper(int c);
|
c的大寫形式 |
int to lower(int c); |
c的小寫形式 |
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
char a='a',A='A',num='2',s=' ';
char a1,A1;
if (isdigit(num))
cout<<"num is a digit"<<endl;
if (isupper(A))
cout<<"A is a upper"<<endl;
if (islower(a))
cout<<"a is a lower"<<endl;
if (isalpha(A)&&isalpha(a))
cout<<"A and a are alpha"<<endl;
if (isalnum(A)&&isalpha(a))
cout<<"A and a are alnum"<<endl;
if (isprint(a))
cout<<"a is a print"<<endl;
if (isspace(s))
cout<<"s is a space"<<endl;
a1=tolower(A);
cout<<"the lower of A is "<<a1<<endl;
A1=toupper(a);
cout<<"the upper of a is "<<A1<<endl;
return 0;
}
3、<cmath>頭文件double(double x);撒地方
<cmath>頭文件中聲明了標(biāo)準(zhǔn)數(shù)學(xué)函數(shù)。
double acos(double x); |
x的反余弦
|
double asin(double x); |
x的反正弦 |
double atan(double x); |
x的反正切 |
double atan2(double y,double x); |
y/x的反正切 |
double ceil(double x); |
不小于x的最小整數(shù) |
double cos(double x); |
x的余弦 |
double cosh(double x); |
x的雙曲余弦 |
double exp(double x); |
e的x次方 |
double fabs(double x); |
x的絕對值 |
double floor(double x); |
不大于x的最大整數(shù) |
double log(double x); |
x的自然對數(shù) |
double log10(double x); |
x的以10為底的對數(shù) |
double pow(double x,double y); |
x的y次方 |
double sin(double x); |
x的正弦 |
double sinh(double x); |
x的雙曲正弦 |
double sqrt(double x); |
x的平方根 |
double tan(double x); |
x的正切 |
double tanh(double x); |
x的雙曲正切 |
#include <iostream>
using namespace std;
#include <cmath>
int main()
{
double x=0,y=1,
acos1=acos(x),
asin1=asin(x),
atan1=atan(x),
atan21=atan2(x,y),
cos1=cos(x),
sin1=sin(x),
tan1=tan(x),
cosh1=cosh(x),
sinh1=sinh(x),
tanh1=tanh(x),
log1=log(y),
log101=log10(y),
exp1=exp(x),
pow1=pow(y,x);
double num=-3.25,
ceil1=ceil(num),
fabs1=fabs(num),
floor1=floor(num),
sqrt1=sqrt(num);
cout<<acos1<<endl;
cout<<asin1<<endl;
cout<<atan1<<endl;
cout<<atan21<<endl;
cout<<cos1<<endl;
cout<<sin1<<endl;
cout<<tan1<<endl;
cout<<cosh1<<endl;
cout<<sinh1<<endl;
cout<<tanh1<<endl;
cout<<log1<<endl;
cout<<log101<<endl;
cout<<exp1<<endl;
cout<<pow1<<endl<<endl;
cout<<ceil1<<endl;
cout<<fabs1<<endl;
cout<<floor1<<endl;
cout<<sqrt1<<endl;
return 0;
}
4、<cstdio>頭文件
<cstdio>頭文件聲明了支持標(biāo)準(zhǔn)輸入輸出的函數(shù)和全局符號(hào)。支持控制臺(tái)以及文件輸入/輸出。也定義了NULL——一個(gè)表示控指針的全局符號(hào)。
5、<cstdlib>頭文件
<cstdlib>頭文件聲明了很多標(biāo)準(zhǔn)函數(shù)和宏,分為4大類:數(shù)字函數(shù)、內(nèi)存管理函數(shù)、系統(tǒng)函數(shù)和隨機(jī)數(shù)發(fā)生器函數(shù)
1)數(shù)字函數(shù)
int abs(int i); |
i的絕對值 |
int atoi(const char *s); |
字符串代表的整形值 |
long atol(const char *s); |
字符串代表的長整形值 |
float atof( const char *s); |
字符串代表的浮點(diǎn)型值 |
2)內(nèi)存管理函數(shù)
void *calloc(int sz,int n); |
void *malloc(int sz); |
void free(void *buf); |
malloc和calloc的區(qū)別:第一,malloc要求以字符數(shù)目給出所需內(nèi)存的大小,而calloc則要求以每一項(xiàng)的大小和項(xiàng)的數(shù)目給出所需內(nèi)存的大小;第二,calloc把已分配內(nèi)存的內(nèi)容全部初始化為0,而malloc則不進(jìn)行初始化。
3)系統(tǒng)函數(shù)
系統(tǒng)函數(shù)有三個(gè):void abort();void exit(int n);int system(const char *cmd);
abort和exit函數(shù)用于終止程序的運(yùn)行。abort函數(shù)使程序異常終止。exit函數(shù)是程序正常終止,它將關(guān)閉所有打開的流文件,并將傳遞給它的參數(shù)返回給操作系統(tǒng)。
system函數(shù)調(diào)用操作系統(tǒng)來執(zhí)行一個(gè)操作系統(tǒng)命令。
#include <cstdlib>
int main()
{
std::system("dir *.doc");
return 0;
}
4)隨機(jī)數(shù)發(fā)生器函數(shù)
相關(guān)函數(shù)有:int rand(); void srand(unsigned int seed);
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
srand(time(0));
char ans;
do
{
int fav = rand() % 32;
int num;
do
{
std::cout << "Guess my secret number (0 - 32) ";
std::cin >> num;
std::cout << (num < fav ? "Too low" :
num > fav ? "Too high" :"Right") << std::endl;
}
while (num != fav);
std::cout << "Go again? (y/n) ";
std::cin >> ans;
}
while (ans == 'y');
return 0;
}
6、<cstring>頭文件
<cstring>頭文件聲明了用于處理以零值結(jié)尾的字符型數(shù)組的函數(shù),其中兩個(gè)比較函數(shù)、兩個(gè)復(fù)制函數(shù)、兩個(gè)串聯(lián)函數(shù)、一個(gè)計(jì)算字符串長度函數(shù)和一個(gè)用指定字符填充內(nèi)存區(qū)域的函數(shù)。
int strcmp(const char *s1,const char *s2);
int strncmp(const char *s1,const char *s2,int n);
char *strcpy(char *s1,const char *s2);
char *strncpy(char *s1,const char *s2,int n);
int strlen(const char *s);
char *strcat(char *s1,const char *s2);
char *strncat(char *s1,const char *s2,int n);
char *memset(void *s,int c,int n);
#include <iostream>
#include <cstring>
int main()
{
int len;
char msg[] = "Wrong.";
std::cout << "Password? ";
char pwd[40];
std::cin >> pwd;
len = strlen(pwd);
if (strcmp(pwd, "boobah") == 0)
strcpy(msg, "OK.");
std::cout << msg << " You typed " << len << " characters";
return 0;
}
7、<ctime>文件頭
<ctime>頭文件聲明了喝操作時(shí)間、日期相關(guān)的一些函數(shù)、一個(gè)結(jié)構(gòu)和一個(gè)數(shù)據(jù)類型。
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
相關(guān)函數(shù):
char *asctime(const struct tm *time);
char *ctime(const time_t *t);
double difftime(time_t t1,time_t t2);
struct tm *gmtime(const time_t *t);
struct tm *localtime(const time_t *t);
time_t mktime(struct tm * time);
time_t time(time_t *t);
#include <iostream>
#include <ctime>
int main()
{
time_t now = time(0);
std::cout << asctime(gmtime(&now));
return 0;
}
posted on 2009-06-21 19:51
The_Moment 閱讀(1395)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
C\C++