C/C++常用函数及样例

  • Post author:
  • Post category:其他




C标准库常用函数



1



<

c

s

t

r

i

n

g

>

<cstring>






<








c


s


t


r


i


n


g




>





1.strlen()字符串长度。

#include<iostream>
#include<string.h>
using namespace std;

int main()
{
    char a[101];
    fgets(a, 101, stdin);
    
    int c = strlen(a);
    cout << c << endl;
    return 0;
}

2.strcmp()字符串比较

strcmp(str1, str2)

该函数返回值如下:

如果返回值小于 0,则表示 str1 小于 str2。

如果返回值大于 0,则表示 str1 大于 str2。

如果返回值等于 0,则表示 str1 等于 str2。

#include <cstdio>
#include <string.h>

using namespace std;

const int N = 15;
int main()
{
    char str1[N] = "abcdef";
    char str2[N] = "ABCDEF";
    int ret;

    ret = strcmp(str1, str2);
    if(ret < 0)
    {
        printf("str1 小于 str2");
    }
    else if(ret > 0) 
    {
        printf("str1 大于 str2");
    }
    else 
    {
        printf("str1 等于 str2");
    }

    return 0;
}

3.strcpy()字符串拷贝

#include <stdio.h>
#include <string.h>
 
int main ()
{
  char str1[]="Sample string";
  char str2[40];
  char str3[40];
  strcpy (str2,str1);
  strcpy (str3,"copy successful");
  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
  return 0;
}

4.memset()暴力清空

#include <cstdio>
#include <cstring>
#include <iostream>
int main ()
{
   char str[50];
 
   strcpy(str,"This is string.h library function");
   puts(str);
 
   memset(str,0,strlen(str));
   puts(str);
   
   return(0);
}

5.memcpy()暴力拷贝

#include<cstdio>
#include<cstring>
 
int main(void)
{
  char src[] = "***";
  char dest[] = "abcdefg";
  printf("使用 memcpy 前: %s\n", dest);
  memcpy(dest, src, strlen(src));
  printf("使用 memcpy 后: %s\n", dest);
  return 0;
}



2



<

c

m

a

t

h

>

<cmath>






<








c


m


a


t


h




>





1.pow()次方函数

#include <cstdio>
#include <cmath>

int main ()
{
   printf("值 8.0 ^ 3 = %lf\n", pow(8.0, 3));

   printf("值 3.05 ^ 1.98 = %lf", pow(3.05, 1.98));
   
   return(0);
}

2.sin()cos()等三角函数asin()反函数

#include <cstdio>
#include <cmath>

#define PI 3.14159265

int main ()
{
   double x, ret, val;

   x = 45.0;
   val = PI / 180;
   ret = sin(x*val);
   printf("%lf 的正弦是 %lf 度", x, ret);
   
   return(0);
}

3.sqrt()平方根

#include <cstdio>
#include <cmath>

int main ()
{

   printf("%lf 的平方根是 %lf\n", 4.0, sqrt(4.0) );
   printf("%lf 的平方根是 %lf\n", 5.0, sqrt(5.0) );
   
   return(0);
}

4.fabs()绝对值(浮点数)

#include <stdio.h>
#include <math.h>

int main ()
{
   int a, b;
   a = 1234;
   b = -344;
  
   printf("%d 的绝对值是 %lf\n", a, fabs(a));
   printf("%d 的绝对值是 %lf\n", b, fabs(b));
   
   return(0);
}

5.double ceil(double x) 返回大于或等于 x 的最小的整数值

#include <stdio.h>
#include <math.h>

int main ()
{
   float val1, val2, val3, val4;

   val1 = 1.6;
   val2 = 1.2;
   val3 = 2.8;
   val4 = 2.3;

   printf ("value1 = %.1lf\n", ceil(val1));
   printf ("value2 = %.1lf\n", ceil(val2));
   printf ("value3 = %.1lf\n", ceil(val3));
   printf ("value4 = %.1lf\n", ceil(val4));
   
   return(0);
}
  1. double floor(double x) 返回小于或等于 x 的最大的整数值
#include <cstdio>
#include <cmath>

int main ()
{
   float val1, val2, val3, val4;

   val1 = 1.6;
   val2 = 1.2;
   val3 = 2.8;
   val4 = 2.3;

   printf("Value1 = %.1lf\n", floor(val1));
   printf("Value2 = %.1lf\n", floor(val2));
   printf("Value3 = %.1lf\n", floor(val3));
   printf("Value4 = %.1lf\n", floor(val4));
   
   return(0);
}



3



<

c

s

t

d

l

i

b

>

<cstdlib>






<








c


s


t


d


l


i


b




>





1.qsort()快排

#include <cstdio>
#include <cstdlib>

int values[] = { 88, 56, 100, 2, 25 };

int cmpfunc (const void * a, const void * b)
{
   return (  *(int*)b - *(int*)a );
}

int main()
{
   int n;

   printf("排序之前的列表:\n");
   for( n = 0 ; n < 5; n++ ) {
      printf("%d ", values[n]);
   }

   qsort(values, 5, sizeof(int), cmpfunc);

   printf("\n排序之后的列表:\n");
   for( n = 0 ; n < 5; n++ ) {
      printf("%d ", values[n]);
   }
 
  return(0);
}

2.malloc()free()C语言动态内存分配

#include <cstdio>
#include <cstring>
#include <stdlib>
 
int main()
{
   char *str;
 
   /* 最初的内存分配 */
   str = (char *) malloc(15);
   strcpy(str, "runoob");
   printf("String = %s,  Address = %u\n", str, str);
 
   /* 重新分配内存 */
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %u\n", str, str);
 
   free(str);
 
   return(0);
}

3.rand()随机数

#include <cstdio>
#include <cstdlib>
#include <ctime>
 
int main()
{
   int i, n;
   time_t t;
   
   n = 5;
   
   srand((unsigned) time(&t));
 
   for( i = 0 ; i < n ; i++ ) 
   {
      printf("%d\n", rand() % 50);
   }
   
  return(0);
}



4



<

c

t

i

m

e

>

<ctime>






<








c


t


i


m


e




>





1.clock()程序从启动到现在的毫秒数

#include <ctime>
#include <cstdio>
 
int main()
{
   clock_t start_t, end_t;
   double total_t;
   int i;
 
   start_t = clock();
   printf("程序启动,start_t = %ld\n", start_t);
    
   printf("开始一个大循环,start_t = %ld\n", start_t);
   for(i=0; i< 10000000; i++)
   {
   }
   end_t = clock();
   printf("大循环结束,end_t = %ld\n", end_t);
   
   total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
   printf("CPU 占用的总时间:%f\n", total_t  );
   printf("程序退出...\n");
 
   return(0);
}

2.time()

#include <stdio.h>
#include <time.h>
 
int main ()
{
  time_t seconds;
 
  seconds = time(NULL);
  printf("自 1970-01-01 起的小时数 = %ld\n", seconds/3600);
  
  return(0);
}



5



<

c

c

t

y

p

e

>

<cctype>






<








c


c


t


y


p


e




>





1.int isdigit(int c) 检查所传的字符是否是十进制数字字符

#include <cstdio>
#include <cctype>

int main()
{
   int var1 = 'h';
   int var2 = '2';
    
   if( isdigit(var1) )
   {
      printf("var1 = |%c| 是一个数字\n", var1 );
   }
   else
   {
      printf("var1 = |%c| 不是一个数字\n", var1 );
   }
   if( isdigit(var2) )
   {
      printf("var2 = |%c| 是一个数字\n", var2 );
   }
   else
   {
      printf("var2 = |%c| 不是一个数字\n", var2 );
   }
  
   return(0);
}

2.void isalpha(int c) 检查所传的字符是否是字母

#include <cstdio>
#include <cctype>

int main()
{
   int var1 = 'd';
   int var2 = '2';
   int var3 = '\t';
   int var4 = ' ';
    
   if( isalpha(var1) )
   {
      printf("var1 = |%c| 是一个字母\n", var1 );
   }
   else
   {
      printf("var1 = |%c| 不是一个字母\n", var1 );
   }
   if( isalpha(var2) )
   {
      printf("var2 = |%c| 是一个字母\n", var2 );
   }
   else
   {
      printf("var2 = |%c| 不是一个字母\n", var2 );
   }
   if( isalpha(var3) )
   {
      printf("var3 = |%c| 是一个字母\n", var3 );
   }
   else
   {
      printf("var3 = |%c| 不是一个字母\n", var3 );
   }
   if( isalpha(var4) )
   {
      printf("var4 = |%c| 是一个字母\n", var4 );
   }
   else
   {
      printf("var4 = |%c| 不是一个字母\n", var4 );
   }
   
   return(0);
}

3.int isupper(int c) 检查所传的字符是否是大写字母;int islower(int c) 检查所传的字符是否是小写字母。是的话返回非零值,否则返回0。



版权声明:本文为weixin_61823031原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。