posts - 29,comments - 10,trackbacks - 0
      在这里将介绍大量的函数和宏,但并不会全部给出详细的例子。这是因为很多函数的用法都非常相似。
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 阅读(1363) 评论(0)  编辑 收藏 引用 所属分类: C\C++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理