C语言C库(十六)


数学库 

#include <math.h>  
    
     double sq = sqrt(4);  //4开平方 = 2
    
    //返回弧度值
    double at = atan2(10, 40);




通用工具库

#include <stdlib.h>//通用工具库

#include <stdlib.h> //通用工具库
#include <string.h>
#define NDEBUG      //禁用assert
#include <assert.h>
#include <stdarg.h> //可变参数
struct name {
    int i;
}a;
void mathTest(void *);
void singOff();
void tooBad();


void qsortTest();
#define NUM 40
void fillarray (double ar[], int n);
void showarray (const double ar[],int n);
int mycomp (const void *p1, const void *p2);//数组排序
int comp (const void *p1, const void *p2);  //比较姓名
struct names {
    char first[40];
    char last[40];
};

void assertt();   //异常断言

void memmovee();  //数组复制

void stdargg();   //可变参数
double sum(int, ...);

int main(int argc, const char * argv[]) {

    struct name b;
    b.i = 10;
    
    
    //mathTest(&b);
    qsortTest();
    assertt();
    memmovee();
    stdargg();
    return 0;
}


#pragma mark -
void assertt()
{
    assert(1==1);
}





#pragma mark - 排序
void qsortTest()
{
    //qsort() 比较元素大小排序
    double vals[NUM];
    fillarray(vals, NUM);
    puts("Random list: ");
    showarray(vals, NUM);
    qsort(vals, NUM, sizeof(double), mycomp);
    puts("\nSorted list: ");
    showarray(vals, NUM);
    
    
    //比较姓名
    
}

//数组赋值
void fillarray (double ar[], int n)
{
    int index;
    for (index = 0; index<n; index++) {
        ar[index] = (double) rand()/ ((double) rand()+0.1);
    }
}
//读取数组
void showarray (const double ar[],int n)
{
    int index;
    for (index = 0; index<n; index++) {

        printf("%9.4f ",ar[index]);
    
        if (index % 6 == 5)
        {
            putchar('\n');
        }
    }
    
    if (index % 6 != 0)
    {
        putchar('\n');
    }
}

int mycomp (const void *p1, const void *p2)
{
    /* 需要使用指向double 的指针访问值 */
    const double *a1 = (const double *)p1;
    const double *a2 = (const double *)p2;
    
    if (*a1 < *a2)
    {
        return -1;
    }
    else if (*a1 == *a2)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int comp (const void *p1, const void *p2)
{
    /* 需要使用指向double 的指针访问值 */
    const struct names *a1 = (const struct names*)p1;
    const struct names *a2 = (const struct names *)p2;
    int res;
    res = strcmp(a1->last,a2->last);
    
    if (res != 0)
    {
        return res;
    }
    else
    {
        return strcmp(a1->first, a2->first);
    }
}



#pragma mark - 数学函数
void mathTest(void * test)
{
    
    //平方根函数
    double sq = sqrt(4);  //4开平方 = 2
    
    //返回弧度值
    double at = atan2(10, 40);
    
    
    //通用工具库  exit() atexit()
    //atexit() 函数 在显式或隐式调用 exit()退出时调用预先注册的 函数指针,按先进后出原则
    atexit(singOff);
    
    int a;
    if (scanf("%d",&a) != 1)
    {
        atexit(tooBad);
        
        exit(EXIT_FAILURE);
    }
    
    
}

void singOff()
{
    puts(__FUNCTION__);
}


void tooBad()
{
    puts(__FUNCTION__);
}



void memmovee()
{
    char*s="GoldenGlobalView";
    char d[20];
    //memcpy 假定两个内存区域没有重叠  如果存在重叠会发生未知情况
    memcpy(d,s+5,4);//从第5个字符(n)开始复制,连续复制4个字符(View)
    //memcpy(d,s+14*sizeof(char),4*sizeof(char));也可
    d[4]='\0';
    printf("memcpy %s",d);
    getchar();
    
    //memmove 不假定重叠
    memmove(d+2, s, 5*sizeof(char)); //从数组 d的第2个下标元素开始,将s的前5个元素复制过去
    d[7]='\0';
    printf("memmove %s",d);
    
    
}


//可变参数 stdarg.h
void stdargg()
{
    double a,b;
    int a_num = 3;
    int b_num = 5;
    a = sum(a_num,1.1,2.5,11.1); // 第一个3代表参数个数
    b = sum(b_num,233.11,22.1,1123.2,1.11123,88.88); //第一个5代表参数个数
    
    printf("\na = %.2lf\n",a);
    printf("b = %.2lf\n",b);
}


double sum(int lim, ...)
{
    
    va_list ap;    //声明用于存放参数的变量

    double tot = 0.0f;
    
    va_start(ap, lim); //把ap初始化为参数列表
    
    for (int i = 0; i<lim; i++) {
        
        tot += va_arg(ap, double);   //访问列表

    }
    
    
    va_end(ap); //清理工作
    
    return tot;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值