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中的类三.创建类四.构造函数 实例变量 五.方法六.方法的输出七.方法的调用八.重载九.析构函数十.继承十一. ...
随机推荐
- 在github上建立自己的网站
学了前端小半年,如今写了个自己的网页想要去应聘,却发现部署很麻烦,部署到阿里云之类,买域名啊啥的还要收费,说贵也不贵,但我就是傲娇~ google一下了解到Github有一个Github pages的 ...
- 苹果iOS强制HTTPS迫在眉睫,距离2017年只剩1天,准备好了么?
其实不久前苹果就发了通告,要求ios上的应用全部以HTTPS来进行接口调用以及数据访问,这样做是为了数据安全,一方面为了自己,另一方面也是对应用的要求更加严格,这么做很好,也加强了市场app的规范,虽 ...
- POJ2001Shortest Prefixes[Trie]
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 17683 Accepted: 768 ...
- 转: 在 Vim 中优雅地查找和替换 (写的很好,排版也是相当的赞)
http://harttle.com/2016/08/08/vim-search-in-file.html
- vuejs mvvm图解
- HttpClient学习整理
HttpClient简介HttpClient 功能介绍 1. 读取网页(HTTP/HTTPS)内容 2.使用POST方式提交数据(httpClient3) 3. 处理页面重定向 ...
- 2016-2017-2 《Java程序设计》预备作业1 总结
2016-2017-2 <Java程序设计>预备作业1 总结 预备作业01:你期望的师生关系是什么见https://edu.cnblogs.com/campus/besti/2016-20 ...
- CURL
基本语法: function curl($url){ $ch=curl_init(); //初始化 curl_setopt($ch, CURLOPT_URL, $url); //核心 curl_se ...
- spring 整合 mongo
spring 非常强大,不仅在jdbc访问提供了jdbctemplate,而且在mongo访问上提供了mongoTemplate.闲话不多说,下边开始整合mongoTemplate. ONE: 添加s ...
- 第一篇puppet
1,什么是puppet puppet是一种Linux.Unix.windows平台的集中配置管理系统,使用自有的puppet描述语言,可管理配置文件.用户.cron任务.软件包.系统服务等.puppe ...