zoukankan      html  css  js  c++  java
  • Windows下编程2----- C语言常用函数举例

    几个小函数

    1、    //MessageBoxA(0,"网络故障,重新登录","qq error",3); //弹出对话框

    2、    //ShellExecuteA(0,"open","notepad",0,0,6);    //执行指令 notepad可以指定网址

     

    3、    //malloc(100000);//吃内存,铲食

        //Sleep(100);

    4、获取当前时间并打印

    方法一:

        SYSTEMTIME tt;

        GetLocalTime(&tt);

        printf("%2d:%2d:%2d.%3d",tt.wHour,tt.wMinute,tt.wSecond,tt.wMilliseconds);

    方法二:

    time_t tt;

    //localtime(&tt);//此函数不行

    time(&tt);//获取当前系统的时间

    printf("Today's date and time: %s ",ctime(&tt));//把日期和时间转换为字符串

    方法三:

    time_t timer = time(NULL);

    struct tm *tblock = localtime(&timer); //把日期与时间转变为结构

    printf("Local time is : %s",asctime(tblock));//格式化时间

     

    5、转换日期和时间为ASCII码

        char str[80];

        struct tm t;

        t.tm_sec = 1;

        t.tm_min = 30;

        t.tm_hour = 9;

        t.tm_mday = 22;

        t.tm_mon = 11;

        t.tm_year = 56;

        t.tm_wday = 4;

        t.tm_yday = 0;

        t.tm_isdst = 0;

        strcpy(str,asctime(&t));

        printf("%s ",str);

    6、测试一个条件并可能使得程序终止

    #include <Windows.h>

    #include <stdlib.h>

    #include <stdio.h>

    #include <string.h>

    #include <time.h>

    #include <assert.h>

     

    struct ITEM

    {

        int key;

        int value;

    };

    void additem(struct ITEM *itemptr)

    {

        assert(itemptr != NULL);

    }

    int main()

    {

        //struct ITEM it;

        //it.key = 3;

        //it.value = 4;

    additem(NULL);

        return 0; //never reach here

    }

    程序执行后:(断言错误)

    7、把字符串转换为浮点数

    方法一:利用 atof函数。类似的还有:atoi atoll;

    float f = 0;

        char *str = "1234.5678";

        f = atof(str);

        printf("%f ",f);

    方法二:利用sstream头文件转换。

        std::string str = "1234.5678";

        //stringstream ss;

        //ss << str;

        stringstream ss(str);

        double dd = 0;

        ss >> dd;

        printf("%lf ",dd);

    8、分配主存储器

    char *str = NULL;

    str = (char*)calloc(10,sizeof(char));

    strcpy(str,"hello");

    puts(str);

    9、 向上向下取整

    double mm = 123.54;

    double down = floor(mm);

    printf("%lf ",down); //123.0000 向下取整

    double up = ceil(mm);

    printf("%lf ",up); //124.0000 向上取整

    10、改变文件大小

    int handle;

    char buf[11] = "0123456789";

    handle = open("DUMMY.FIL",O_CREAT);

    write(handle,buf,strlen(buf));

    chsize(handle,5);

    close(handle);

    11、确定时间

    clock_t start,end;

    start = clock();

    Sleep(1000);

    end = clock();

    printf("%d ",end - start);

     

    12、送格式化输出至屏幕

    cprintf("hello world ");

     

    13、终止程序

    exit(1);

    14、返回浮点数的绝对值

    fabs(f); == abs();

    15、检测流上的 结束

    FILE *fp;

    fp = fopen("a.txt","r");

    fgetc(fp);

    if(feof(fp))//检测流上的文件结束符

    {

         printf("we have reached end of file");

    }

    fclose(fp);

    16、清除一个流

    fflush();

    17、从流中读取字符

    fgetc()、fgetchar();

    18、传送格式化输出到一个流中

    Fprintf(); 一般作用于 字节流 而非二进制流。

    19、返回当前文件指针

    FILE *fp;

    fp = fopen("a.txt","w+");

    fprintf(fp,"this is a test");

    printf("the file ptr is at byte %ld ",ftell(fp)); //值为14

    fclose(fp);

    20、重定位流上的文件指针

    long filesize(FILE *fp)

    {

        long curpos,length;

        curpos = ftell(fp);

        fseek(fp,0L,SEEK_END);

        length = ftell(fp);

        fseek(fp,curpos,SEEK_SET);

        return length;

    }

    int main()

    {

    FILE *fp;

    fp = fopen("a.txt","w+");

    fprintf(fp,"this is a test");

    printf("filesize of a.txt is %ld bytes ",filesize(fp));

    fclose(fp);

    return 0;

    }

     

    21、常见的函数有

    gets(); 从流中取一字符串

    int number = 12345;

    char str[25];

    itoa(number,str,10); //把一个整数转换为字符串

    printf("intêo %d, stringêo %s",number,str);

     

    log(); //对数函数

     

    22、内存分配函数

    Malloc();//申请内存

    Memcpy(void* destin, void*source,unsigned n);//从源 中拷贝 n个字节到目标 中。

    23、移动一块字节

    char dest[] = "ajdlajlgjalgjlajgafdlajgflajgljaljglajglajglajglaj";

    char src[] = "******************************************";

    puts(dest);

    memmove(dest,src,sizeof(src));//src移动到dest 输出就是上面的输出

    puts(dest);

     

    24、清零函数

    memset();

    指数函数 pow(double x, double y); //x的y次方

    putch(int ch); //输出字符到控制台

    putc(int ch, file *stream);//输出字符到指定的流中

    puts(char *string);//送一字符串到流中

    25、随机数生成器

    两个函数的使用: srand(seed); rand();

    如果只用一个函数rand,则每次一开始产生的数字一样。没有随机数的意思。

    srand(time(NULL));

    printf("ten random numbers 0-99: ");

    for(int i = 0; i < 10; ++i)

    {

         printf("%d ",rand()%100);

    }

    printf(" ");

     

    26、重新分配主存

    char* str = (char*)malloc(10*sizeof(char));

    char a[] = "hello";

    memcpy(str,a,sizeof(a));

    printf("string is %s Address is %p ",str,str);

    str = (char*)realloc(str,20*sizeof(char));

    printf("string is %s Address is %p ",str,str);

    27、小函数

    睡眠函数 sleep(unsigned seconds);

    送格式化输出到字符串int sprint(char* string, char *format [,argument,….]);

    char buf[80];

    char *str = "yujinli";

    sprintf(buf,"I love %s",str);

    puts(buf);

     

    计算平方根 double sqrt(double x);

    28、关于激光字符串的处理函数

    串拷贝 strcpy();

    char str1[] = "dfaljgalg";

    char str2[20];

    strcpy(str2,str1); //这里str2不能为指针,不然会出错,最好是数组。

    puts(str2);

    串连接 strcat();

    char str1[30] = "I love ";

    char str2[] = "yujinli";

    strcat(str1,str2);// 这里连接到str1后面,把str1数组搞大点,不能为str[],这样长度就不够了。

    puts(str1);

    串比较 strcmp();

    返回三个值 -1 a<b 0 a=b 1 a>b

    char str1[] = "I love me";

    char str2[] = "I love me";

    int p = strcmp(str1,str2);

    printf("%d ",p);

     

    在串中查找指定字符串的第一次出现

    char *str1 = "Borland International", *str2 = "nation", *ptr;

    ptr = strstr(str1, str2);

    printf("The substring is: %s ", ptr); //输出: national

     

    将字符串转换为double型值 double strtod(char *str, char** endptr);

    char *input = "12345.6789",**endptr = NULL;

    double value;

    value = strtod(input,endptr);

    printf("the string is %s the number is %lf ",input,value);

    类似的还有: long strtol(char* str, char** endptr, int base); // base == 10,则转为为10进制数。 将串转为长整型。

    发送一个dos命令:int system(char* str);

    交换字节:void swab(char* from, char*to, int nbyte);

    char from[] = "dlajglg";

    char to[20];

    swab(from,to,sizeof(from));

    puts(to);

     

    将串中小写转大写 char* strupr(char* str);

     

    29、正切

    Double tan(double x);

    30、取一天的时间 time();

    31、将字符转成大小写

    char str[] = "DJALJGLAJGALGJ";

    for(int i = 0; i < strlen(str); ++i)

    {

         str[i] = tolower(str[i]);

    }

    puts(str);

    for(int i = 0; i < strlen(str); ++i)

    {

         str[i] = toupper(str[i]);

    }

    puts(str);

     

    32、将一个无符号长整型转为字符串

    char* ultoa(unsigned long value, char* string, int radix);//radix 是进制的意思

  • 相关阅读:
    (一)Kafka0.8.2官方文档中文版系列入门指南
    Hbase TTL(Time To Live)详解
    java源码学习详解Object类
    设计模式详细解读简单工厂方法模式
    (二)Kafka0.8.2官方文档中文版系列API
    Scala对象相等性判断
    scala中跳出循环的3种方法
    wpf 中借助 Grid 实现随着 Form 大小变化而按比例自动改变宽度或高度。
    static and cache
    约定编程之 Dictionary 的 String 类型的 Key
  • 原文地址:https://www.cnblogs.com/zhuxuekui/p/4196718.html
Copyright © 2011-2022 走看看