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 ...
随机推荐
- java基础学习02(简单的java程序)
简单的java程序 一.完成的目标 1. 理解java程序的基本组成 2. 如何对程序代码进行注释 3. java标识符的命名规则 4. 了解java中的关键字 5. 使用java定义变量或声明变量 ...
- Struts2 验证码图片实例
本文转载于DongLiYang的博客http://www.cnblogs.com/dongliyang/archive/2012/08/24/2654431.html 其中修改过一部分,针对使用注解而 ...
- [LeetCode] Encode and Decode Strings 加码解码字符串
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
- Kinect for Windows SDK开发入门(十九):Kinect Fusion
Kinect for Windows SDK1.7中引入了Kinect Fusion功能.在1.8的SDK中对该功能进行了改进和强化,Kinect Fusion能够使得我们使用Kinect f ...
- phpstorm 无法格式化代码
phpstorm 默认的格式化代码的快捷键是 Ctrl + Alt + L,但是按了没有反应. 原因是当时开着网易云音乐,占用了这个快捷键,关了就好了.另外其他的程序比如QQ也有可能占用这个快捷键.
- JavaScript检测对象的类属性
function classof(o) { if(o === null) { return "Null"; } if(o === undefined) { return " ...
- 讲座:Modeling User Engagement for Ad and Search
讲座:http://bdai.ruc.edu.cn/?p=118 Modeling User Engagement for Ad and Search ppt 链接: Dr. Ke(Adam) Zho ...
- AngularJs的$http发送POST请求,php无法接收Post的数据解决方案
最近在使用AngularJs+Php开发中遇到php后台无法接收到来自AngularJs的数据,在网上也有许多解决方法,却都点到即止.多番摸索后记录下解决方法:tips:当前使用的AngularJ ...
- NPOI的操作
public async Task<MemoryStream> ExportExcel(IList<fuquestionbank> _list, string pId, str ...
- BZOJ 1876: [SDOI2009]SuperGCD
1876: [SDOI2009]SuperGCD Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 3060 Solved: 1036[Submit][St ...