C和指针 第十六章 习题
16.8 计算平均年龄
#include <stdlib.h>
#include <stdio.h>
#define MAX_LEN 512 int main()
{
int age;
int totalAge;
float avgAge;
int peopleNum;
FILE *file;
char info[MAX_LEN];
char *infoPtr;
file = fopen("D:/family.txt", "r"); //按行读取文件
while(fgets(info, MAX_LEN, file)){
infoPtr = info;
peopleNum = 0;
totalAge = 0;
//strtol转换字符到数字
while((age = strtol(infoPtr, &infoPtr, 10)) > 0){
totalAge += age;
peopleNum++;
}
//类型转换为float,然后计算平均年龄
avgAge = (float)totalAge / peopleNum;
printf("%savg: %5.2f\n", info, avgAge);
} return 0;
}
运行:

16.9 计算相同生日概率
#include <stdlib.h>
#include <stdio.h>
#include <time.h> //比较元素
int compare(void const *birth1, void const *birth2){
return *(int *)(birth1) - *(int*)(birth2);
} //打印数组
void print_arr(int *array, int len){
int idx = 0;
while(idx <= len){
printf("%d ", array[idx]);
idx++;
}
} //数组中是否有两个相同数
int count_same(int *array, int len){
int same = 0;
while(len > 0){
if(array[len] == array[len - 1]){
return 1;
}
len--;
}
return 0;
}
int main()
{
int times = 0;
int randBirthday[30];
int peopleCount;
int sameCount = 0;
srand((unsigned int)time(0)); while(times < 100000){
peopleCount = 29;
while(peopleCount >= 0){
randBirthday[peopleCount] = rand() % 365;
peopleCount--;
}
qsort(randBirthday, 30, sizeof(int), compare);
sameCount += count_same(randBirthday, 29);
times++;
}
printf("%f", (float)(sameCount) / 100000);
return 0;
}
运行:

16.10 插入排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //插入排序
void insert_sort(void *array, unsigned int length, unsigned int size, int (*handle)(void const *num1, void const *num2));
//整型移动函数 //整型比较函数
int int_compare(void const *num1, void const *num2)
{
return (*(int *)num1 - *(int *)(num2));
} //打印数组
void print_arr(int *arr , int len){
for(int idx = 0 ; idx < len; idx++){
printf("%d ", arr[idx]);
}
} int main()
{
int array[10] = {4, 1, 17, 2, 8 , 9, 22, 12, 7, 5};
insert_sort(array, 10, sizeof(int), int_compare);
print_arr(array, 10);
return 0;
} void insert_sort(void *array, size_t n_elements, size_t n_size, int (*handle)(void const *num1, void const *num2))
{
//存放临时需要位移的元素
char *temp = malloc(n_size);
//已排序的元素下标,从0开始,拿第二个元素和第一个对比
unsigned int sortIdx = 1;
//元素游标和已排序元素游标
unsigned int idx, idy;
//位移元素下面
unsigned int mov;
//从第二位开始,依次拿出元素和之前的已排序元素比较
for(idx = 1; idx < n_elements; idx++){
//开始比较
for(idy = 0; idy < sortIdx; idy++){
if(handle(array + idy * n_size, array + idx * n_size) > 0){
//将元素和已排序元素依次比较,当遇到已排序元素,比当前元素大时,当前元素应该插入到该已排序元素位置,已排序元素应该后移一位
//位移会覆盖后面的值,所以需要先保存需要插入的值
memcpy(temp, array + idx * n_size, n_size);
for(mov = sortIdx; mov > idy; mov--){
memmove(array + mov * n_size, array + (mov - 1) * n_size, n_size);
}
//元素插入
memcpy(array + idy * n_size , temp, n_size);
}
}
//如果需要插入的值,正好比已排序的值中最大的还要大,那么不需要移动,只要增加已排序的值得下标即可
sortIdx++;
}
}
运行:

C和指针 第十六章 习题的更多相关文章
- C和指针 第十六章 标准函数库 信号
信号名<signal.h> 程序中大多数错误都是程序本身导致的,但是,有些程序遇到的事件却不是程序本身所引发的.比如用户终止程序,程序无法预知此类事件发生的情况,信号就是为了对此类事件做出 ...
- C和指针 第十六章 标准函数库
字符串转换: long int strtol(char const *string, char **unused, int base); 将字符串转换为数值形式,遇到非法字符停止,如果stop不是NU ...
- C和指针 第十六章 标准函数库 本地跳转setjmp.h
setjmp和longjmp提供一种类似goto语句的机制,但它的作用域不局限于同一个函数的作用域之内.这些函数可以用于深层次的嵌套函数调用链. int setjmp(jmp_buf state); ...
- C和指针 第十五章 习题
15.8 十六进制倾印码 #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ...
- C和指针 第十四章 习题
14.1 打印函数 #include <stdio.h> void print_ledger_long(){ printf("function print_ledger_long ...
- UNP学习笔记(第二十六章 线程)
线程有时称为轻权进程(lightweight process) 同一进程内的所有线程共享相同的全局内存.这使得线程之间易于共享信息,然后这样也会带来同步的问题 同一进程内的所有线程处理共享全局变量外还 ...
- 【C++】《C++ Primer 》第十六章
第十六章 模板与泛型编程 面向对象编程和泛型编程都能处理在编写程序时不知道类型的情况. OOP能处理类型在程序允许之前都未知的情况. 泛型编程在编译时就可以获知类型. 一.定义模板 模板:模板是泛型编 ...
- 《Linux命令行与shell脚本编程大全》 第十六章 学习笔记
第十六章:创建函数 基本的脚本函数 创建函数 1.用function关键字,后面跟函数名 function name { commands } 2.函数名后面跟空圆括号,标明正在定义一个函数 name ...
- Gradle 1.12 翻译——第十六章. 使用文件
有关其它已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或訪问:http://gradledoc.qiniudn.com ...
随机推荐
- [html/css]清除浮动的相关技巧
以前只了解得很浅显,转载了一篇不错的文,学习参考 浮动会使当前标签产生向上浮的效果,同时会影响到前后标签.父级标签的位置及 width height 属性.而且同样的代码,在各种浏览器中显示效果也有可 ...
- [LeetCode] 4Sum II 四数之和之二
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- [LeetCode] Implement strStr() 实现strStr()函数
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- .NET程序员细数Oracle与众不同的那些奇葩点
扯淡 距上次接触 Oracle 数据库已经是 N 年前的事了,Oracle 的工作方式以及某些点很特别,那会就感觉,这货就是一个奇葩!最近重拾记忆,一直在折腾 Oracle,因为 Oracle 与众不 ...
- javaWeb实现登录功能
1.三要素 (1) 入口 就是我们所在的页面 入口到处理的数据请求会出现乱码,用request.SetCharacterEncoding("UTF-8");来解决,仅仅是用用于Po ...
- RabbitMQ总结概念
AMQP:一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计 http://www.diggerplus.org/archives/3110 AMQP ...
- 自建Ceph存储与 AWS、阿里云、腾讯云的成本对比
本文单从存储成本角度对比了自建Ceph存储和业界公有云存储的硬件成本,不包括IDC带宽成本. 统计Ceph集群的用到的主要设备为: OSD.MON.RGW服务器 .TOR交换机. 机架. 下表解释: ...
- asp.net使用Get请求webservice
先在Web.config中的System.Web节点下添加如下代码,使其支持Get请求: <webServices> <protocols> <add name=&quo ...
- jQuery 邮箱下拉列表自动补全
综述 我想大家一定见到过,在某个网站填写邮箱的时候,还没有填写完,就会出现一系列下拉列表,帮你自动补全邮箱的功能.现在我们就用jQuery来实现一下. 博主原创代码,如有代码写的不完善的地方还望大家多 ...
- odoo种种
[精]Odoo 8.0深入浅出开发教程-模块开发基础 http://blog.csdn.net/sunansheng/article/details/50864527 搭建odoo开发调试环境 htt ...