《C和指针》章节后编程练习解答参考——第5章
5.1
题目:
略
解答代码:
#include <stdio.h> int main(void)
{
char ch; while (((ch = getchar()) != EOF) && (ch != 'z'))
{
if (ch >= 'A' && ch <= 'Z')
{
ch += 'a' - 'A';
}
putchar(ch);
} getchar();
return ;
}
5.2
题目:
略
解答代码:
#include <stdio.h> int main(void)
{
char ch; while (((ch = getchar()) != EOF) && (ch != 'z')) //输入z时退出
{
if (((ch >= 'A') && (ch <= 'Z')) || ((ch >= 'a') && (ch <= 'z'))) //加密
{
if ((ch <= 'M') || (ch <= 'm'))
{
ch += 'n' - 'a';
}
else if ((ch >= 'N') || (ch >= 'n'))
{
ch -= 'n' - 'a';
}
}
putchar(ch);
} getchar();
return ;
}
5.3
题目:
编写函数,函数返回值把参数的二进制位模式从左到右变换一下后的值。
函数原型:
unsigned int reverse_bits(unsigned int value);
解答代码:
#include <stdio.h> #define NUM 0xA5 unsigned int reverse_bits(unsigned int value)
{
unsigned int i;
unsigned int valuebak = ;
char list[] = { }; for (i=; i!=; i<<=)
{
valuebak <<= ;
valuebak |= (value & 0x01);
value >>= ;
} return valuebak;
} int main(void)
{
unsigned int rev;
rev = reverse_bits(NUM);
printf("0x%X\n", NUM);
printf("0x%X\t\t%d\n", rev, rev); getchar();
return ;
}
5.4
5.5
题目:
编写函数,把一个给定的值存储到一个整数中指定的几个位。假定整数中的位是从右向左进行编号,起始位的位置不会小于结束位的位置。
函数原型:
int store_bit_field(int original_value, int value_to_store, unsigned starting_bit, unsigned ending_bit);
提示:
创建一个掩码
用掩码的反码对原值AND操作
将新值左移,使与待存储值对齐
把移位后的值与掩码进行AND操作
把结果值与原值进行位OR操作
解答代码:
#include <stdio.h> #define ORI_VALUE 0xFFFF
#define VALUE_STORE 0x123
#define START_BIT 13
#define END_BIT 9 int store_bit_field(int original_value, int value_to_store,
unsigned starting_bit, unsigned ending_bit)
{
unsigned int int_bit = sizeof(unsigned) * ; if ((starting_bit <= int_bit) && (ending_bit <= int_bit) && (ending_bit < starting_bit))
{
unsigned int maskt = ~;
unsigned int mask = ;
unsigned int i; for (i=starting_bit; i>=ending_bit; i--) //生成掩码
{
mask |= (<<i);
}
original_value &= ~mask;
value_to_store <<= ending_bit;
value_to_store &= mask; //待存储数据与掩码匹配
original_value |= value_to_store;
}
else
printf("Data format error!\n"); return original_value;
} int main (void)
{
int value;
value = store_bit_field(ORI_VALUE, VALUE_STORE, START_BIT, END_BIT);
printf("0x%X\n", value); getchar();
return ;
}
《C和指针》章节后编程练习解答参考——第5章的更多相关文章
- 《C和指针》章节后编程练习解答参考——第10章
10.1 #include <stdio.h> typedef struct { unsigned ]; unsigned ]; unsigned ]; }TelphoneNumber; ...
- 《C和指针》章节后编程练习解答参考——第9章
9.1 #include <stdio.h> #include <ctype.h> #include <string.h> #define N 100 int ma ...
- 《C和指针》章节后编程练习解答参考——第8章
8.1 #include <stdio.h> int main (void) { int a, b, c, d; // 不使用嵌套花括号初始化 unsigned ][][][] = { , ...
- 《C和指针》章节后编程练习解答参考——6.2
<C和指针>——6.2 题目: 编写一个函数,删除源字符串中含有的子字符串部分. 函数原型: int del_substr(char *str, char const *substr); ...
- 《C和指针》章节后编程练习解答参考——6.3
<C和指针>——6.3 题目: 编写一个函数,把参数字符串中的字符反向排列. 函数原型: void reverse_string(char *string); 要求: 使用指针而不是数组下 ...
- 《C和指针》章节后编程练习解答参考——6.6
<C和指针>——6.6 题目: 在指定的下限.上限之间使用数组方法查找质数,并将质数提取出来. 要求: 略 解答代码: #include <stdio.h> #define U ...
- 《C和指针》章节后编程练习解答参考——6.4
<C和指针>——6.4 题目: 质数是只能被1和本身整除的整数. 在1到1000之间的质数,在数组中剔除不是质数的数. 解答代码: #include <stdio.h> #de ...
- 《C和指针》章节后编程练习解答参考——6.1
<C和指针>——6.1 6.1 题目: 编写一个函数,在一个字符串中进行搜索,查找另一子字符串中出现的字符. 函数原型如下: char *find_char(char const *sou ...
- DSAPI多功能组件编程应用-参考-Win32API常数
DSAPI多功能组件编程应用-参考-Win32API常数 在编程过程中,常常需要使用Win32API来实现一些特定功能,而Win32API又往往需要使用一些API常数,百度搜索常数值,查手册,也就成了 ...
随机推荐
- android-配置虚拟机Virtual device
Android的应用程序是基于virtual device运行的,在运行一个android的应用程序之前先要配置要virtual device
- poj 3140 Contestants Division(树形dp? dfs计数+枚举)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- CentOS7添加第三方源
CentOS由于很追求稳定性,所以官方源中自带的软件不多,因而需要一些第三方源,比如EPEL.ATrpms.ELRepo.Nux Dextop.RepoForge等. EPEL EPEL即Extra ...
- centos7 安装php5.6.0 、nginx1.7.4、phpssdbadmin
1 创建用户.网站目录和下载相关的安装包 groupadd www #添加www组 创建目录/data/www/ chown www:www /data/www/ -R #设置目录所有者 chmod ...
- [DEncrypt] Encrypt--加密/解密/MD5加密 (转载)
点击下载 Encrypt.zip 这个类是关于加密,解密的操作,文件的一些高级操作1.Encrypt加密2.Encrypt解密3.Encrypt MD5加密看下面代码吧 /// <summar ...
- 多线程、Socket
多线程 线程.进程和应用程序域 进程:进程是一个操作系统上的概念,用来实现多任务并发执行,是资源分配的最小单元,各个进程是相互独立的,可以理解为执行当中的程序,在操作系统中一般用一个称为PCB的结 ...
- Windows环境下使用Cmake ndk编译fdk-aac
一.废话 最近学习,第一步就是编译.我们需要编译FFmpag,x264,fdk_aac,下面是x264,网上说的很多都是几百年前的,我亲测完美可用 还是那句话 我能力有限,但是我希望我写的东西能够让 ...
- 查看当前使用的shell
1.实时查看当前进程中使用的shell种类:推荐 ps | grep $$ | awk '{print $4}' (注:$$表示shell的进程号) 2.最常用的查看shell的命令,但不能实时反映当 ...
- ria service 单元测试
https://blogs.msdn.microsoft.com/kylemc/2011/08/18/unit-testing-a-wcf-ria-domainservice-part-1-the-i ...
- CSS Text文本格式
Text Color 颜色属性被用来设置文字的颜色. 颜色是通过CSS最经常的指定: 十六进制值 - 如"#FF0000" 一个RGB值 - "RGB(255,0,0)& ...