C和指针 第十三章 习题
1,1标准输入读入字符,统计各类字符所占百分比
#include <stdio.h>
#include <ctype.h> //不可打印字符
int isunprint(int ch){
return !isprint(ch);
} //转换表,储存各个判断函数指针
int (*tables[])(int) = {iscntrl, isspace, isdigit, islower, isupper, ispunct, isunprint}; int main()
{
int count[7] = {0};
int ch;
int idx; while((ch = getchar()) != EOF){
//转换表中的函数进行测试,如果符合对应的数组项+1
for(idx = 0; idx < 7; idx++){
if(tables[idx](ch)){
count[idx]++;
}
}
} for(idx = 0; idx < 7; idx++){
printf("%d\n", count[idx]);
} return 0;
}
运行结果:

1.4 编写sort函数,对任何类型数组进行排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h> void sort(void *array, unsigned length, unsigned int size, int(*compare)(void const *value1, void const *value2))
{
//循环变量
int idx, idy;
void *temp = malloc(size); for (idx = 0; idx < length - 1; idx++) {
for (idy = idx + 1; idy < length; idy++) {
//通过字节复制,交换位置,由于array是void类型的,所以需要根据size来进行偏移,找到对应的元素地址
if (compare(array + (idx * size), array + idy * size) == 1) {
//array是指向数组的指针,根据元素大小,得到元素地址
memcpy(temp, array + idx * size, size);
memcpy(array + idx * size, array + idy * size, size);
memcpy(array + idy * size, temp, size);
}
}
}
} int int_compare(void const *value1, void const *value2)
{
if (*(int *)value1 == *(int *)value2) {
return 0;
}
else if (*(int *)value1 <= *(int *)value2) {
return -1;
}
else {
return 1;
}
} int main()
{
int array[] = { 1, 4, 5, 2, 3, 8, 6, -10};
for (int idx = 0; idx < 8; idx++) {
printf("%d\t", array[idx]);
}
printf("\n");
sort(array, 8, 4, int_compare); for (int idx = 0; idx < 8; idx++) {
printf("%d\t", array[idx]);
} return 0;
}
运行:

C和指针 第十三章 习题的更多相关文章
- C和指针 第六章 习题
6.1编写一个函数,它在一个字符串中进行搜索,查找所有在一个给定字符集中出现的字符,返回第一个找到的字符位置指针,未找到返回NULL #include <stdio.h> char * f ...
- C和指针 第十七章 习题
17.8 为数组形式的树编写模块,用于从树中删除一个值,如果没有找到,程序节点 ArrayBinaryTree.c // // Created by mao on 16-9-18. // #inclu ...
- C和指针 第十三章 高级指针话题
高级声明: int (*f)(); 这里声明有两个括号,第二个括号是函数调用,第一个括号是聚组作用.(*f)是一个函数,所以f是指向返回整型的函数的指针.程序中的每个函数都位于,内存中某个位置,所以存 ...
- C和指针 第十一章 习题
1编写calloc,内部使用malloc函数获取内存 #include <stdio.h> #include <stdlib.h> void *myAlloc(unsigned ...
- C和指针 第七章 习题
7.1 hermite递归函数 int hermite(int n, int x) { if (n <= 0) { return 1; } if (n == 1) { return 2 * x; ...
- C和指针 第五章 习题
下列输出的值: #include <stdio.h> int func(){ static int count = 1; return ++count; } int main() { in ...
- C和指针 第四章 习题
4.1正数的n的平方根可以通过: ai+1= (ai + n / ai ) / 2 得到,第一个a1是1,结果会越来越精确. #include <stdio.h> int main() { ...
- C和指针 第三章 习题
在一个源文件中,有两个函数x和y,定义一个链接属性external储存类型static的变量a,且y可以访问,x不可以访问,该如何定义呢? #include <stdio.h> void ...
- perl5 第十三章 Perl的面向对象编程
第十三章 Perl的面向对象编程 by flamephoenix 一.模块简介二.Perl中的类三.创建类四.构造函数 实例变量 五.方法六.方法的输出七.方法的调用八.重载九.析构函数十.继承十一. ...
随机推荐
- usb驱动开发之大结局
从usb总线的那个match函数usb_device_match()开始到现在,遇到了设备,遇到了设备驱动,遇到了接口,也遇到了接口驱动,期间还多次遇到usb_device_match(),又多次与它 ...
- 14-前端开发之HTML
1. 什么是HTML? HTML (Hyper Text Mark-up Language) 即超文本标记语言,是用来描述网页的一种语言. 它不是一种编程语言,而是一种标记语言 (markup lan ...
- phabricator在mac上的搭建
环境:OS X Yosemite 10.10.5 前提:phabricator主要是由php写的,而且是以website方式运行的,所以mac上要先安装好 php + nginx(或apache) + ...
- python 列表 总结
在python里创建列表和字典非常简单,这里总结一下它们的常用方法 1.创建列表 myArry = ["one", "two", "three&quo ...
- C#反射在ADO中的巧用
在C#原生开发网站的时候,经常会碰到一个问题,后台用原生查出来的数据一般是DataReader或者是DataTable,这样就把数据从数据库中拿到了,然后就是把数据绑定到前台页面进行输出,这是最原生态 ...
- React Native 接入微博、微信、QQ 登录功能
在 App 开发中我们经常需要在用户登录模块接入 SNS 登录组件,这样会大大提高用户的注册体验.特别当一个不是刚性需求 App 推广的时候,这样会很大的降低用户体验的成本,没有人愿意忍受输入邮箱.手 ...
- 为什么 "auto a = 1;" 在C语言中可以编译通过?
参照:这里 这让我想起之前看的一部书, int i; 其实是等价与 auto int i; 表示为局部变量 这应该与static是相对的吧?
- 安裝 14.04.1 Ubuntu 到 Lenovo thinkpad t460p
在 Lenovo Thinkpad T460p 安裝 ubuntu, BIOS 需要做一些設定, 沒設定的現象:不斷地停在 usb disk 設定 可以 使用 usb disk install 了!
- 从基层容器类看万变不离其宗的JAVA继承体系
以容器类为例子,可以观一叶而知秋,看看以前的前辈们是如何处理各种面向对象思想下的继承体系的.读的源代码越多,就越要总结这个继承关系否则读的多也忘得快. 首先摆上一张图片: 看到这张图很多人就慌了,难道 ...
- mysql关联表的复制
1. 复制被参照的表: CREATE TABLE clone_product_1 LIKE product_1; INSERT INTO clone_product_1 SELECT * FROM p ...