C和指针 第十六章 习题

时间:2022-09-12 14:34:02

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;
}

运行:

C和指针 第十六章 习题

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;
}

运行:

C和指针 第十六章 习题

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

  1. C和指针 第十六章 标准函数库 信号

    信号名<signal.h> 程序中大多数错误都是程序本身导致的,但是,有些程序遇到的事件却不是程序本身所引发的.比如用户终止程序,程序无法预知此类事件发生的情况,信号就是为了对此类事件做出 ...

  2. C和指针 第十六章 标准函数库

    字符串转换: long int strtol(char const *string, char **unused, int base); 将字符串转换为数值形式,遇到非法字符停止,如果stop不是NU ...

  3. C和指针 第十六章 标准函数库 本地跳转setjmp&period;h

    setjmp和longjmp提供一种类似goto语句的机制,但它的作用域不局限于同一个函数的作用域之内.这些函数可以用于深层次的嵌套函数调用链. int setjmp(jmp_buf state); ...

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

    15.8 十六进制倾印码 #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ...

  5. C和指针 第十四章 习题

    14.1 打印函数 #include <stdio.h> void print_ledger_long(){ printf("function print_ledger_long ...

  6. UNP学习笔记(第二十六章 线程)

    线程有时称为轻权进程(lightweight process) 同一进程内的所有线程共享相同的全局内存.这使得线程之间易于共享信息,然后这样也会带来同步的问题 同一进程内的所有线程处理共享全局变量外还 ...

  7. 【C&plus;&plus;】《C&plus;&plus; Primer 》第十六章

    第十六章 模板与泛型编程 面向对象编程和泛型编程都能处理在编写程序时不知道类型的情况. OOP能处理类型在程序允许之前都未知的情况. 泛型编程在编译时就可以获知类型. 一.定义模板 模板:模板是泛型编 ...

  8. 《Linux命令行与shell脚本编程大全》 第十六章 学习笔记

    第十六章:创建函数 基本的脚本函数 创建函数 1.用function关键字,后面跟函数名 function name { commands } 2.函数名后面跟空圆括号,标明正在定义一个函数 name ...

  9. Gradle 1&period;12 翻译——第十六章&period; 使用文件

    有关其它已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或訪问:http://gradledoc.qiniudn.com ...

随机推荐

  1. 关于for循环中,定义的i的作用域的问题。

    for(var i=0;i<2;i++){ console.log(i) } console.log(i) 经过测试:在IE9+,谷歌,火狐中.都出现了0,1,2三个值. 所以其作用域在整个上下 ...

  2. selected 刷新页面后selected选中的值保持不表(thinkphp 从控制器assign 传值到js)

    昨晚解决select 刷新页面以后选择的值保持不变,要想让seleted不变,有两种思路, 1,在提交表单的时候,将所选择的option的属性设为checked . 2.将option的value或者 ...

  3. 【uTenux实验】信号量

    信号量(semaphore)是一个用来指示可用的资源并将可用资源的数量以数值的形式表示出来的对象.当使用一组资源时,信号量用来实现互斥控制和同步.uTenux提供了信号量出来的API,可以很方便地使用 ...

  4. Swift开发第四篇——柯里化

    本篇分为两部分: 一.柯里化的基本使用 二.柯里化的使用场景 一.柯里化的基本使用 柯里化(Currying):也就是把接受多个参数的方法变换成接受第一个参数的方法,并且返回接受余下的参数并且返回结果 ...

  5. 02&sol;07&sol;2106 &commat; 6&colon;28am &lpar;UTC&rpar;

    <?php echo pow(2,32); 4294967296 http://www.unixtimestamp.com/index.php 4294967296 Is equivalent ...

  6. c&sol;c&plus;&plus;中的各种字符串转换

    一:CString 和 *char 的转换: 1:CString -> *char 1)CString转化为*char可以使用CString中的GetBuffer()函数,具体如下: CStri ...

  7. mysql5&period;7 timestamp错误:there can be only oneTIMESTAMP column with CURRENT&lowbar;TIMESTAMP in DEFAULT or ON UPDATE

    #1293 - Incorrect table definition; there can be only oneTIMESTAMP column with CURRENT_TIMESTAMP in ...

  8. Ubuntu16&period;04 安装Tensorflow1&period;7过程记录二:安装CUDA及Tensorflow

    参考 How to install Tensorflow 1.7.0 using official pip package 其中的CUDNN应该改为7.05for CUDA9.0 后面安装的spyde ...

  9. &lbrack;转&rsqb;Peer-to-Peer Communication Across Network Address Translators

    Peer-to-Peer Communication Across Network Address Translators Bryan Ford Massachusetts Institute of ...

  10. Android开发(二十)——Fragment中的touch事件

    问题: Fragment中没有提供监听touch事件的方法. 解决方案: Activity中能够监听touch事件. 于是在Activity中写一个接口,MyOnTouchListener,在需要监听 ...