在這里將介紹大量的函數和宏,但并不會全部給出詳細的例子。這是因為很多函數的用法都非常相似。
1、<cassert>頭文件
<cassert>頭文件是用來調試程序的。該文件中定義了assert宏,即使加入調試代碼。
使用assert宏,可以再程序調試階段加入確認代碼,assert宏斷言某個條件為真;一旦條件不滿足,assert宏就顯示這個條件,并指出程序中何處沒有通過這個條件測試,然后終止程序。
#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>頭文件中聲明的函數用于轉換字符變量并測試其是否在給定范圍之內。
int isdigit(int c); |
當c是數字(0~9)返回真 |
int isupper(int c); |
當c是大寫字母(A~Z)時返回真 |
int islower(int c); |
當c是小寫字母(a~z)時返回真 |
int isalpha(int c); |
當c是字母(a~z,A~Z)時返回真 |
int isalnum(int c); |
與isalpha(c)一樣 |
int isprint(int c); |
當c是可顯示的ASCII字符時返回真
|
int isspace(int c);
|
當c是空字符時返回真 |
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>頭文件中聲明了標準數學函數。
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的最小整數 |
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的最大整數 |
double log(double x); |
x的自然對數 |
double log10(double x); |
x的以10為底的對數 |
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>頭文件聲明了支持標準輸入輸出的函數和全局符號。支持控制臺以及文件輸入/輸出。也定義了NULL——一個表示控指針的全局符號。
5、<cstdlib>頭文件
<cstdlib>頭文件聲明了很多標準函數和宏,分為4大類:數字函數、內存管理函數、系統函數和隨機數發生器函數
1)數字函數
int abs(int i); |
i的絕對值 |
int atoi(const char *s); |
字符串代表的整形值 |
long atol(const char *s); |
字符串代表的長整形值 |
float atof( const char *s); |
字符串代表的浮點型值 |
2)內存管理函數
void *calloc(int sz,int n); |
void *malloc(int sz); |
void free(void *buf); |
malloc和calloc的區別:第一,malloc要求以字符數目給出所需內存的大小,而calloc則要求以每一項的大小和項的數目給出所需內存的大??;第二,calloc把已分配內存的內容全部初始化為0,而malloc則不進行初始化。
3)系統函數
系統函數有三個:void abort();void exit(int n);int system(const char *cmd);
abort和exit函數用于終止程序的運行。abort函數使程序異常終止。exit函數是程序正常終止,它將關閉所有打開的流文件,并將傳遞給它的參數返回給操作系統。
system函數調用操作系統來執行一個操作系統命令。
#include <cstdlib>
int main()
{
std::system("dir *.doc");
return 0;
}
4)隨機數發生器函數
相關函數有: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>頭文件聲明了用于處理以零值結尾的字符型數組的函數,其中兩個比較函數、兩個復制函數、兩個串聯函數、一個計算字符串長度函數和一個用指定字符填充內存區域的函數。
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>頭文件聲明了喝操作時間、日期相關的一些函數、一個結構和一個數據類型。
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 */
};
相關函數:
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 閱讀(1391)
評論(0) 編輯 收藏 引用 所屬分類:
C\C++