C和指针 第六章 习题
6。1编写一个函数,它在一个字符串中进行搜索,查找所有在一个给定字符集中出现的字符,返回第一个找到的字符位置指针,未找到返回NULL
#include <stdio.h> char * find_char(char const *source, char const *chars)
{
char const *sptr = source;
char const *cptr = chars; if (sptr == NULL || cptr == NULL) {
return NULL;
} while (*sptr != '\0') {
cptr = chars;
while (*cptr != '\0') {
if (*cptr == *sptr) {
//找到打印source地址
printf("chars:0x%p\n", chars);
//返回类型为 char *,此处类型转换一下把char const *转换回来
return (char *)cptr;
}
cptr++;
}
sptr++;
} return NULL;
}
#include <stdio.h>
#include "function.h" int main()
{
char *source = "ABCDEF";
char *str1 = "XYZ";
char *str2 = "XRCQEF";
char *chars = str1;
char *ptr = NULL;
//没有对应的字符
ptr = find_char(source, chars);
printf("0x%p\n", ptr);
//对应的字符C,第三个
chars = str2;
ptr = find_char(source, chars);
printf("0x%p\n", ptr); while (1)
;
return 0;
}
执行结果:

6.2删除字符串中子串部分,将剩下部分前移。
int del_substr(char *str, char const *substr)
{
if (str == NULL || substr == NULL) {
return 0;
}
//将数组首位赋值给指针数组
char *source = str;
char *sub = substr;
char *tmp = NULL; while (*source != '\0') {
//将指针重置指向子串首
sub = substr;
//使用临时变量进行对比,保持source位置信息不变
tmp = source;
//当遇到相同的字符,开始比较之后是否相同
while (*tmp++ == *sub++) {
//循环中已经sub++了,到达末尾,证明找到子串,开始前移
if (*sub == '\0') {
//未到达字符串末尾,继续前移
while (*(tmp + 1) != '\0') {
*source = *tmp;
}
return 1;
}
}
source++;
}
return 0;
}
int main()
{
char *source = "ABCDEF";
char *str1 = "CGE";
char *str2 = "CDE";
int isDel;
//无子串
isDel = del_substr(source, str1);
printf("del_substr: %d\n", isDel);
//有子串
isDel = del_substr(source, str2);
printf("del_substr: %d\n", isDel); while (1)
;
return 0;
}
执行结果:

6.3 编写函数reverse_string,翻转字符串。
void reverse_string(char *string)
{
//先定义两个指针,一个指向首一个指向末尾
char *head = string;
//string本身指向第一位,加上字符串长度后是指向\0后的,所以需要前移,指向最后一个字符
char *tail = string + strlen(string) - 1;
char tmp; //同一数组内可以进行指针位置对比
while (head < tail) {
tmp = *head;
*head = *tail;
*tail = tmp;
head++;
tail--;
}
} int main()
{
char source[] = "ABCDEF";
printf("source: %s\n", source); reverse_string(source);
printf("result: %s\n", source); return 0;
}
执行结果:

6.4 Eratosthenes法找质数,第一步写下2至某个上线之间的所有的数,第二步开始剔除不是质数的整数,找到列表第一个不被剔除的数(就是2)然后将表后面所有逢双的数都剔除,因为都可以被2整除,所以不是质数,然后回到表头,此时表头尚未被剔除的是三,然后每逢三位剔除,反复进行最后都是质数。
void find_primer(int *numbers, int length)
{
//0 1 不为质数
numbers[0] = FALSE;
numbers[1] = FALSE; int tmp;
int loc;
int index = 2; while (index < length) {
tmp = index;
//当前头部找到的质数,和后面的数相乘的结果对应的位置全部不是质数。
while ( (tmp += index) < length) {
*(numbers + tmp) = FALSE;
}
index++;
} }
int main()
{
int numbers[10000];
for (int index = 0; index < 10000; index++) {
numbers[index] = TRUE;
} find_primer(numbers, 10000); for (int index = 0; index < 10000; index++) {
if (numbers[index]) {
printf("%-08d", index);
}
} return 0;
}
运行结果:

6.5利用第五章的位数组求质数
位数组:
//字符偏移
unsigned int char_offset(unsigned bit_number)
{
return bit_number / CHAR_BIT;
} //bit位偏移
unsigned int bit_offset(unsigned bit_number)
{
return bit_number % CHAR_BIT;
} void set_bit(char bit_array[], unsigned bit_number)
{
bit_array[char_offset(bit_number)] |= 1 << bit_offset(bit_number);
} void clear_bit(char bit_array[], unsigned bit_number)
{
bit_array[char_offset(bit_number)] &= ~(1 << bit_offset(bit_number));
} void assign_bit(char bit_array[], unsigned bit_number, int value)
{
if (value != 0) {
set_bit(bit_array, bit_number);
}
else {
clear_bit(bit_array, bit_number);
}
} int test_bit(char bit_array[], unsigned bit_number)
{
//对该bit位进行与操作,如果是1则结果还是 1<< (bit_number % CHAR_BIT)
return (bit_array[char_offset(bit_number)] & (1 << bit_offset(bit_number))) != 0;
}
位数组求质数:
void find_primer_bit(char bit_array[], unsigned long int length)
{
clear_bit(bit_array, 0);
clear_bit(bit_array, 1); unsigned int tmp;
unsigned int loc;
unsigned int index = 2; while (index < length) {
tmp = index;
//没逢index位置0
while ( (tmp += index) < length) {
clear_bit(bit_array, tmp);
}
index++;
}
}
#define MAX_LEN 1000000
#define MAX_ARR_SIZE (MAX_LEN / 8) int main()
{
char bit_array[MAX_ARR_SIZE];
unsigned int count = 0;
unsigned int index = 0;
unsigned int total = 0;
while (index < MAX_ARR_SIZE) {
bit_array[index++] = 0xff;
} find_primer_bit(bit_array, MAX_LEN); index = 1;
while (index < MAX_LEN) {
if (test_bit(bit_array, index)) {
total++;
printf("%-8d", index);
}
index++;
} printf("\n共计: %d 个质数 \n", total); return 0;
}
执行结果

检测一下是否正确:

1000000万内有78498个质数
6.6计算每隔1000位质数个数:
统计一下每隔100000的质数
#define MAX_LEN 1000000
#define MAX_ARR_SIZE (MAX_LEN / 8)
int main()
{
char bit_array[MAX_ARR_SIZE];
unsigned int count = 0;
unsigned int index = 0;
unsigned int total = 0;
unsigned int limit = 100000;
while (index < MAX_ARR_SIZE) {
bit_array[index++] = 0xff;
}
find_primer_bit(bit_array, MAX_LEN); index = 0;
while (index < MAX_LEN) {
if (index == limit) {
printf("%-6d %-6d avg: %5.2f\n", index - 100000, index, (float)count / 100);
count = 0;
limit += 100000;
}
if (test_bit(bit_array, index)) {
count++;
total++;
}
index++;
} printf("\n共计: %d 个质数 \n", total);
while (1)
;
return 0;
}

C和指针 第六章 习题的更多相关文章
- Perl语言入门:第六章习题:处理用户所指定的名字并汇报相应的姓。
37 print "\n----------------------------------_exercise_6_1--------------------------\n"; ...
- C和指针 第十六章 习题
16.8 计算平均年龄 #include <stdlib.h> #include <stdio.h> #define MAX_LEN 512 int main() { int ...
- C和指针 第十七章 习题
17.8 为数组形式的树编写模块,用于从树中删除一个值,如果没有找到,程序节点 ArrayBinaryTree.c // // Created by mao on 16-9-18. // #inclu ...
- C和指针 第十三章 习题
1,1标准输入读入字符,统计各类字符所占百分比 #include <stdio.h> #include <ctype.h> //不可打印字符 int isunprint(int ...
- 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和指针 第六章 数组名与指针
指针的算术运算符是指针和数组之间的一种关联,但不是唯一关联: 可以使用数组名作为指向数组第一个元素的指针,但是不可以给数组名赋新的值. //如下声明a int a[10]; //用a作为指向数组第一个 ...
- C和指针 第六章 指针6.2 6.3字符串中查找的两个版本
int find_char(char **strings, char ch) { char *string; while ((string = *strings++) != NULL) { while ...
- C和指针 第五章 习题
下列输出的值: #include <stdio.h> int func(){ static int count = 1; return ++count; } int main() { in ...
随机推荐
- CODEVS1643 线段覆盖3[贪心]
1643 线段覆盖 3 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 在一个数轴上有n条线段,现要选 ...
- etl结合java的例子
1.新建Java测试类,导出Jar包,放在kettle目录中的libext文件中 package test; public class Test{ public static final String ...
- FILE不是C语言关键字
FILE不是C语言关键字,只是标准C中的标准输入输出中定义的一个新的数据类型 stdio.htypedef struct _iobuf{ char* _ptr; int _cnt; char* _ba ...
- 激活windows7 企业版小记
问题:状态不可用 1)控制面板->管理工具->服务,启动SPP Notification Service和Software Protection两项服务后 重启计算机,仍然提示状态不可用. ...
- BZOJ1951[SDOI2010]古代猪文
Description "在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心--" --选自猪王国民歌 很久 ...
- PHP开发工具+电子书+视频教程等资料下载汇总
本汇总帖包括如下内容: PHP开发工具.PHP IDE PHP学习资源 基础.进阶类 PHP学习资源 高级及应用类 经典PHP视频教程系列 1. PHP开发工具.PHP IDE: PHP开发工具:Ze ...
- jquery获取父元素及祖先元素
parent是找当前元素的第一个父节点,parents是找当前元素的所有父节点 先说一下parent和parents的区别 从字面上不难看出 parent是指取得一个包含着所有匹配元素的唯一父元素的元 ...
- 如何重新划分linux分区大小
1.下载脚本文件,将脚本文件内容复制 chmod +x resize.sh sudo ./resize.sh 输入上面命令后会看到下面的结果 root@odroid:~# sudo ./resize. ...
- Oracle 实验四-七
shutdown immediateORA-01097: 无法在事务处理过程中关闭 - 请首先提交或回退 解决:先 "commit" 实验四 SQL Production :: C ...
- ModernUI教程:独立显示器DPI感知
独立显示器DPI感知,是在Windows 8.1中新增的特性,这个特性针对拥有多个显示器同时各个显示器的DPI设定又不同的人.对这个新特性做了优化支持的软件能够在一个高DPI的显示器 ...