第7章 Linux常用C函数

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Linux操作系统精讲大连理工大学软件学院邱铁综合楼413,Tel:0411-87571632E_mail:qiutie@dlut.edu.cn参考教材:‡《Linux应用与开发典型实例精讲》邱铁、于玉龙、徐子川编著.清华大学出版社.2010.5第7章Linux常用C函数学习本章要达到的目标:†1.熟练应用Linux中C语言函数库中的字符操作函数;†2.熟练应用掌握Linux中C语言函数库中的字符串操作函数;†3.熟悉Linux中C语言函数库中的数学计算操作函数的应用;†4.熟悉Linux中C语言函数库中的数据结构和算法的函数的应用;†5.了解Linux中C语言函数库中的日期时间函数的使用;†6.掌据Linux常用C函数混合编程的方法。7.1使用函数库†人们把大量的基础性操作编写成一个个独立的函数,这些函数具有低耦合、高内聚的特点,这些函数集合成一个已经编译好的不完整的二进制代码文件,就是函数库†需要解决的问题„如何在源代码中调用这些函数库„如何在编译的时候让编译器正确的将函数库库中的内容加载到我们的应用程序中†根据函数库中函数逻辑加载的时间不同,可分为静态库、载入连接库、动态连接库。„静态库是在连接的时候被加入到应用程序的可执行文件中的„载入连接库在编译时并不将库的内容添加到应用程序的可执行文件中„动态链接库是在程序运行过程根据需要动态加载到程序中的7.2字符操作†测试参数所对应的字符是否为拉丁字母„#includectype.h„intisalpha(intc);†测试参数所对应的字符是否为大写字母(isupper)或小写字母(islower)„#includectype.h„intisupper(intc);„intislower(intc);†测试参数所对应的字符是否为阿拉伯数字(isdigit)或十六进制数字(isxdigit)„#includectype.h„intisdigit(intc);„intisxdigit(intc);†测试参数所对应的字符是否为拉丁字母或阿拉伯数字„#includectype.h„intisalnum(intc);†测试参数所对应的字符是否为空白字符„#includectype.h„intisspace(intc);†测试参数所对应的字符是否为标点符号或特殊符号„#includectype.h„intispunct(intc);†测试参数所对应的字符是否为可打印字符。„#includectype.h„intisgraph(intc);„intisprint(intc);†测试参数所对应的字符是否为ASCII码字符„#includectype.h„intisascii(intc);†将参数转化为ASCII码字符„#includectype.h„inttoascii(intc);†将参数转化为小写字母或大写字母„#includectype.h„inttolower(intc);„inttoupper(intc);7.3字符串操作†字符串是编程中一种重要的数据类型,因为用户通过键盘的输入都是采用字符串的形式的7.3.1数据类型转换†将字符串转化成整型„#includestdlib.h„intatoi(constchar*nptr);†将字符串转化成长整型„#includestdlib.h„longatol(constchar*nptr);†将字符串转化成双精度浮点型„#includestdlib.h„doubleatof(constchar*nptr);†将所提供的数字转化成字符串„#includestdlib.h„char*gcvt(doublenumber,size_tndigits,char*buf);†将字符串转化成长整型„#includestdlib.h„longstrtol(constchar*nptr,char**endptr,intbase);††将字符串转化成无符号长整型„#includestdlib.h„unsignedlongstrtoul(constchar*nptr,char**endptr,intbase);†将字符串转化成双精度浮点型„#includestdlib.h„dobulestrtod(constchar*nptr,char**endptr,intbase);7.3.2字符串数据处理†找到字符串第一次出现某一字符(或字符代码)的位置„#includestring.h„char*index(constchar*s,intc);„char*strchr(constchar*s,intc);†找到字符串昀后一次出现某一字符(或字符代码)的位置„#includestring.h„char*rindex(constchar*s,intc);„char*strrchr(constchar*s,intc);†计算字符串的长度„#includestring.h„intstrlen(constchar*s);†字符串拷贝„#includestring.h„char*strcpy(char*dest,constchar*src);„char*strncpy(char*dest,constchar*src,size_tn);†拷贝字符串,目标字符串地址在堆空间申请„#includestring.h„char*index(constchar*src);†将两个字符串连接成一个字符串„#includestring.h„char*strcat(char*dest,constchar*src);„char*strncat(char*dest,constchar*src,size_tn);†不区分大小写按照ASCII码的顺序,比较两个字符串„#includestring.h„intstrcasecmp(constchar*s1,constchar*s2);„intstrncasecmp(constchar*s1,constchar*s2,size_tn);†查找字符串„#includestring.h„intstrstr(constchar*haystack,constchar*needle);†分割字符串„#includestring.h„char*strtok(constchar*s,constchar*delim);7.4数学计算操作†取绝对值„#includestdlib.h„intabs(intj);„#includemath.h„doublefabs(doublex);†取整操作„#includemath.h„doubleceil(doublex);„doublefloor(doublex);†计算乘方„#includemath.h„doublepow(doublex,doubley);†计算开方„#includemath.h„doublesqrt(doublex);†计算e的x次幂„#includemath.h„doubleexp(doublex);†计算以e(log)或10(log10)为底x的对数„#includemath.h„doublelog(doublex);„doublelog10(doublex);†计算三角函数正弦(sin)、余弦(cos)或正切(tan)„#includemath.h„doublesin(doublex);„doublecos(doublex);„doubletan(doublex);†计算反三角函数反正弦(asin)、反余弦(acos)或反正切(atan)„#includemath.h„doubleasin(doublex);„doubleacos(doublex);„doubleatan(doublex);7.5数据结构与算法操作†按均匀分布,取出一个随机整数„#includestdlib.h„intrand();†设置随机数种子„#includestdlib.h„voidsrand(unsignedintseed);†快速排序„#includestdlib.h„voidqsort(void*base,size_tnmemb,size_tsize,int(*compar)(constvoid*,constvoid*));†线性搜索„#includestdlib.h„void*lfind(constvoid*key,constvoid*voidbase,size_tnmemb,size_tsize,int(*compar)(constvoid*,constvoid*));„void*lsearch(constvoid*key,constvoid*voidbase,size_tnmemb,size_tsize,int(*compar)(constvoid*,constvoid*));†采用二元搜索的方式,搜索已经排序的数组„#includestdlib.h„void*bsearch(constvoid*key,constvoid*voidbase,size_tnmemb,size_tsize,int(*compar)(constvoid*,constvoid*));7.6日期时间操作†获取系统当前时间„#includetime.h„time_ttime(time_t*t);†将Linux纪元时间转化为现实世界的时间„#includetime.h„structtm*localtime(consttime_t*timep);„structtm*gmtime(consttime_t*timep);†将现实世界时间转化为Linux纪元时间„#includetime.h„time_t*mktime(structtm*timeptr);†将tm结构体转化成字符串形式„#includetime.h„char*ctime(structtm*timeptr);„char*asctime(structtm*timeptr);7.7实例训练†需要完成如下功能:„接收用户输入的字符串„摘除字符串中不能组成数字的字符,例如“1a2b.3cEee2”摘除后的结果应该为“12.3E2”。„判断新的字符串所表示的数,是一个整数还是浮点数。„输出新的字符串所表示的数的值。„分别对取出的整数和浮点数分别进行加和,在程序退出的时候输出两个加和的值。7.7.1任务分析7.7.2编写程序†用vi编写程序代码,完成程序设计的状态机7.7.3编译、运行†gcc-oexp7exp7.c思考与练习†1.思考:从软件工程的角度分析,为什么需要建立函数库?†2.编写一个程序,模拟扑克牌洗牌。并将洗牌后,扑克牌的顺序输出到屏幕上,花色可以用字母代替。†3.选作题:编写一个计算器,用户从键盘输入算式,程序将结果返回。计算器支持加、减、乘、除、乘方、括号。例如,用户输入“(1+2)*3-6/2”,程序输出“6”。

1 / 32
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功