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和指针 第十三章 习题的更多相关文章

  1. C和指针 第六章 习题

    6.1编写一个函数,它在一个字符串中进行搜索,查找所有在一个给定字符集中出现的字符,返回第一个找到的字符位置指针,未找到返回NULL #include <stdio.h> char * f ...

  2. C和指针 第十七章 习题

    17.8 为数组形式的树编写模块,用于从树中删除一个值,如果没有找到,程序节点 ArrayBinaryTree.c // // Created by mao on 16-9-18. // #inclu ...

  3. C和指针 第十三章 高级指针话题

    高级声明: int (*f)(); 这里声明有两个括号,第二个括号是函数调用,第一个括号是聚组作用.(*f)是一个函数,所以f是指向返回整型的函数的指针.程序中的每个函数都位于,内存中某个位置,所以存 ...

  4. C和指针 第十一章 习题

    1编写calloc,内部使用malloc函数获取内存 #include <stdio.h> #include <stdlib.h> void *myAlloc(unsigned ...

  5. C和指针 第七章 习题

    7.1 hermite递归函数 int hermite(int n, int x) { if (n <= 0) { return 1; } if (n == 1) { return 2 * x; ...

  6. C和指针 第五章 习题

    下列输出的值: #include <stdio.h> int func(){ static int count = 1; return ++count; } int main() { in ...

  7. C和指针 第四章 习题

    4.1正数的n的平方根可以通过: ai+1= (ai + n / ai ) / 2 得到,第一个a1是1,结果会越来越精确. #include <stdio.h> int main() { ...

  8. C和指针 第三章 习题

    在一个源文件中,有两个函数x和y,定义一个链接属性external储存类型static的变量a,且y可以访问,x不可以访问,该如何定义呢? #include <stdio.h> void ...

  9. perl5 第十三章 Perl的面向对象编程

    第十三章 Perl的面向对象编程 by flamephoenix 一.模块简介二.Perl中的类三.创建类四.构造函数 实例变量 五.方法六.方法的输出七.方法的调用八.重载九.析构函数十.继承十一. ...

随机推荐

  1. google的云盘与公司网盘

    很多人都很期待Google推出的云存储服务,也就是公司网盘.因为多数人相信,没有比google更适合做云存储的公司了,作为一个标准的Web公司或者说互联网公司,云端理所应当的优秀.但比起几年前听传言时 ...

  2. CH Round #72 奇数码问题[逆序对 观察]

    描述 你一定玩过八数码游戏,它实际上是在一个3*3的网格中进行的,1个空格和1~8这8个数字恰好不重不漏地分布在这3*3的网格中. 例如:5 2 81 3 _4 6 7 在游戏过程中,可以把空格与其上 ...

  3. Struts2 自定义MVC框架

    一.Model1与Model2: Model1:就是一种纯jsp开发技术,将业务逻辑代码和视图渲染代码杂糅在一起. Model2:Model2是在Model1的基础上,将业务逻辑的代码分离开来,单独形 ...

  4. spring的@Transactional

    在service类前加上@Transactional,声明这个service所有方法需要事务管理.每一个业务方法开始时都会打开一个事务.Spring默认情况下会对运行期例外(RunTimeExcept ...

  5. 【C#】【Thread】BackgroundWorker的使用

    BackgroundWorker 可以用于启动后台线程. 主要的事件及参数: 1.DoWork --当执行BackgroundWorker.RunWorkerAsync方法时会触发该事件,并且传递Do ...

  6. centos7.0 安装字体库

    最近在centos7.0下用itextpdf将word文档转成pdf时出现字体丢失的情况.网上找了很多资料,各式各样的原因和解决方法.后来经过一番测试发现是centos7.0 minimal没有安装相 ...

  7. C#如何防止程序多次运行的技巧

    一.使用互斥量Mutex弄懂了主要的实现思路之后,下面看代码实现就完全不是问题了,使用互斥量的实现就是第四点的思路的体现,我们用为该程序进程创建一个互斥量Mutex对象变量,当运行该程序时,该程序进程 ...

  8. 解决:error: Cannot fetch repo (TypeError: expected string or buffer)

    同步源码,问题重现: Fetching project platform/external/libopus Fetching project repo error: Cannot fetch repo ...

  9. js判断浏览器类型以及浏览器版本

    判断浏览器类型:   if navigator.userAgent.indexOf(”MSIE”)>0) {} //判断是否IE浏览器 if(isFirefox=navigator.userAg ...

  10. Leetcode 285. Inorder Successor in BST

    Given a binary search tree and a node in it, find the in-order successor of that node in the BST. 本题 ...