11.字符串中常用的系统函数
字符串的头文件<string.h>
1.得到字符串的长度
size_t strlen(const char *str)//计算字符串 str 的长度,直到空结束字符,但不包括空结束字符
2.拷贝字符串
char *strcpy(char *dest, const char *src)//把 src 所指向的字符串复制到 dest
3.连接字符串
char *strcat(char *dest, const char *src)//把 src 所指向的字符串追加到 dest 所指向的字符串的结尾。
4.代码示例
#include<stdio.h> #include<string.h> void main() { char* test01 = "江南";//使用size_t strlen(const char *str)得到字符串的长度 char test02[50] ="", test03[50] = ""; printf("test01的长度为%d\n", strlen(test01)); //使用char *strcpy(char *dest, const char *src)拷贝字符串 //使用strcpy时,数组原来的内容将会被覆盖 strcpy_s(test02, strlen(test01) + 1, test01); printf("test02 = %s\n", test02); //连接字符串char *strcat(char *dest, const char *src) strcpy_s(test03, strlen(test01) + 1, test01);//test03里的内容为“江南” strcat_s(test03, strlen(test01) + strlen(test03) +1, test01); printf("test03 = %s\n", test03); //printf("test03 = %s\n", test03); }
12.时间和日期相关函数
时间的头文件为<time.h>
1.获取当前时间
char *ctime(const time_t *timer)//返回一个表示当地时间的字符串,当地时间是基于参数 timer
#include<stdio.h> #include<time.h> void main() { time_t curtime;//time_t是一个结构体类型,定义一个名称叫curtime的time_t结构体 time(&curtime);//time()初始化 char time[50]; ctime_s(time, sizeof(time), &curtime); printf("当前时间 = %s", time); }
2.编写一段代码来统计 函数 test 执行的时间
double difftime(time_t time1, time_t time2)//返回 time1 和 time2 之间相差的秒数 (time1-time2)
#include<stdio.h> #include<time.h> void test() { int a, b, sum=0; for (a = 0; a < 7777777; a++) { for (b = 0; b < 300; b++) { sum += b; } } } void main() { time_t startTime, endTime; double differentTime = 0.0; printf("程序启动!"); time(&startTime); test(); time(&endTime); differentTime = difftime(endTime, startTime); printf("程序运行时间为%.2f", differentTime); }
13.数学相关函数
1.
double exp(double x)//返回 e 的 x 次幂的值 double log(double x)//返回 x 的自然对数(基数为 e 的对数) double pow(double x, double y)//返回 x 的 y 次幂 double sqrt(double x)//返回 x 的平方根 double fabs(double x)//返回 x 的绝对值
2.代码示例
#include<stdio.h> #include<math.h> void main() { double result1 = exp(7.0); printf("e的7次方 = %f\n", result1); double result2 = log(7.0); printf("ln7 = %f\n", result2); double result3 = pow(9.0, 7.0); printf("9的7次方 = %f\n", result3); double result4 = sqrt(7.0); printf("根号7 = %f\n", result4); double result5 = fabs(-7.0); printf("-7的绝对值 = %f", result5); }
14.基本数据类型和字符串类型的转换
14.1.基本介绍
在程序开发中,我们经常需要将基本数据类型转成字符串类型(即 char 数组 ),或者将字符串类型转成基本数据类型
14.2.sprintf函数的用法
sprintf和平时我们常用的 printf函数的功能很相似。sprintf函数打印到字符串中,而 printf函数打印输出到屏幕 上。sprintf函数在我们完成其他数据类型转换成字符串类型的操作中应用广泛。该函数包含在 stdio.h 的头文件中
14.3.基本类型转字符串类型
#include<stdio.h> void main() { char str1[20], str2[20], str3[20]; int num1 = 97, num3 = 977; double num2 = 97.777777; //将num1和num3的内容存入str1中,其中按照num1→num3的顺序 sprintf_s(str1, sizeof(str1), "%d%d", num1, num3); //将num2的内容保留两个小数点存入str2 sprintf_s(str2, sizeof(str2), "%.2f", num2); //将num2的内容一共保留七位数字,其中两位小数,保存到str3中,不足的位数用空格补齐 sprintf_s(str3, sizeof(str3),"%7.2f", num2); printf("str1 = %s\nstr2 = %s\nstr3 = %s", str1, str2, str3); }
14.4符串类型转基本数据类型
通过<stdlib.h>的函数调用 atoi atof 即可
#include<stdio.h> #include<stdlib.h> void main() { char str1[77] = "12.34567"; char str2[77] = "123.4567"; char str3[77] = "abcdefg"; char str4[77] = "1234567"; int num1 = atoi(str1);//atoi(str1)将str1转换为整数 int num2 = str4; double num3 = atof(str2);//atof(str2)将str2转换为浮点数 char num4 = str3[6];//表示取出str3这个字符串的第七个字符'g' //因为str3[6]是从str[0]开始计算 0 1 2 3 4 5 6 第七个 printf("num1 = %d\nnum 2 = %d\nnum 3 = %f\nnum 4 = % c", num1, num2, num3, num4); }
14.5.注意事项
- 在将 char 数组 类型转成 基本数据类型时,要确保能够转成有效的数据。例如,可以把"777"转为数字,但是不能将"jiangnan"转为一个整数
- 如果格式不正确,会默认转成 0 或者 0.0