首页 技术 正文
技术 2022年11月18日
0 收藏 540 点赞 4,279 浏览 4242 个字

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

  执行结果:

C和指针 第六章 习题

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

执行结果:

C和指针 第六章 习题

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

  执行结果:

C和指针 第六章 习题

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

  运行结果:

C和指针 第六章 习题

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

  

执行结果

C和指针 第六章 习题

检测一下是否正确:

C和指针 第六章 习题

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和指针 第六章 习题

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294