博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《The C Programming Language》答案(第二章)
阅读量:4169 次
发布时间:2019-05-26

本文共 4515 字,大约阅读时间需要 15 分钟。

《The C Programming Language》答案(第二章)

我的新站

P1

#include 
#include
//肖战,nmslint main(){
printf("Domain of signed char is [%d, %d].\n",SCHAR_MIN,SCHAR_MAX); printf("Domain of unsigned char is [%u, %u].\n",0,UCHAR_MAX); printf("Domain of char is [%d, %d].\n",CHAR_MIN,CHAR_MAX); printf("Domain of signed short int is [%hd, %hd].\n",SHRT_MIN,SHRT_MAX); printf("Domain of unsigned short int is [%hu, %hu].\n",0,USHRT_MAX); printf("Domain of signed int is [%d, %d].\n",INT_MIN,INT_MAX); printf("Domain of unsigned int is [%u, %u].\n",0,UINT_MAX); printf("Domain of signed long int is [%ld, %ld].\n",LONG_MIN,LONG_MAX); printf("Domain of unsigned long int is [%lu, %lu].\n",0,ULLONG_MAX); return 0;}

P2

#include 
#define MAXLINE 1024int main(){
char line[MAXLINE]; int c,i; i=0; while(i

P3

#include 
#define MAXLINE 1024int get_line(char s[],int lim){
int c,i,l; for(i=0,l=0;(c=getchar())!=EOF&&c!='\n';++i) if(i
='0'&&c<='9') n+=c-'0'; else if(c>='a'&&c<='f') n+=c-'a'; else if(c>='A'&&c<='F') n+=c-'A'; } return n;}int main(){
int len; char line[MAXLINE]; while((len=get_line(line,MAXLINE))>0) printf("%lu\n",htoi(line)); return 0;}

P4

#include 
#define MAXLINE 1024int get_line(char line[], int maxline);void squeeze(char s1[], char s2[]);int main(void){
int len; char s1[MAXLINE]; char s2[MAXLINE]; printf("Input string s1:\n"); while ((len = get_line(s1, MAXLINE)) == 0) ; printf("Input string s2:\n"); while ((len = get_line(s2, MAXLINE)) == 0) ; squeeze(s1, s2); printf("Result is %s\n", s1); return 0;}int get_line(char s[], int lim){
int c, i, l; for (i = 0, l = 0; (c = getchar()) != EOF && c != '\n'; ++i) if (i < lim - 1) s[l++] = c; s[l] = '\0'; return l;}void squeeze(char s1[], char s2[]){
int i=0;//s1 ptr char tmp[MAXLINE]; int k=0;//tmp ptr while(s1[i]!='\0'){
int find_flag = 0; int j=0;//s2 ptr while(s2[j]!='\0'){
if(s1[i]==s2[j]){
//match! find_flag=1; break; } j++; } if(find_flag==0){
tmp[k]=s1[i]; k++; } i++; } tmp[k]='\0'; for(int ii=0;ii

P5

#include 
#define MAXLINE 1024int get_line(char line[], int maxline);void squeeze(char s1[], char s2[]);int main(void){
int len; char s1[MAXLINE]; char s2[MAXLINE]; printf("Input string s1:\n"); while ((len = get_line(s1, MAXLINE)) == 0) ; printf("Input string s2:\n"); while ((len = get_line(s2, MAXLINE)) == 0) ; squeeze(s1, s2); printf("Result is %s\n", s1); return 0;}int get_line(char s[], int lim){
int c, i, l; for (i = 0, l = 0; (c = getchar()) != EOF && c != '\n'; ++i) if (i < lim - 1) s[l++] = c; s[l] = '\0'; return l;}int any(char s1[], char s2[]){
int i, j; i = 0; while (s2[i] != '\0') {
j = 0; while (s1[j] != '\0') {
if (s1[j] == s2[i]) return j; ++j; } ++i; } return -1;}

P6

#include 
unsigned setbits(unsigned x,int p,int n,unsigned y){
unsigned maskA = x&~(~(~0U<
<<(p+1-n)); unsigned maskB = (y&~(~0U<
<<(p+1-n); return maskA|maskB;}int main(){
printf("%u\n",setbits(114514,1,9,19810)); return 0;}

P7

#include 
unsigned invert(unsigned x,int p,int n){
unsigned mask = ~(~0U<
<<(p+1-n); return x^mask;}int main(){
printf("%u\n",invert(1145,19,14)); return 0;}

P8

#include 
unsigned rightrot(unsigned x,int n){
while(n-- >0) if(x&1) x = (x>>1)|~(~0U>>1); else x = x>>1; return x;}int main(){
printf("%u\n",rightrot(1145,1)); return 0;}

P9

原理:x-1在二进制上相当于把最右边为1的位置零,然后把这一位之后的所有位置1

#include 
int bitcount(unsigned x){
int bits; for(bits=0;x!=0;x&=(x-1)) bits++; return bits;}int main(){
printf("%d\n",bitcount(114514)); return 0;}

P10

#include 
int lower(int c){
return (c>='A'&&c<='Z')?c+'a'-'A':c;}int main(){
char jj[] = "YESHOUXIANBEI"; int i = 0; while(jj[i]!='\0'){
printf("%c--->%c\n",jj[i],lower(jj[i])); i++; } return 0;}

第二章 完

转载地址:http://nfwai.baihongyu.com/

你可能感兴趣的文章
python 字典常见方法
查看>>
python字典复制(浅拷贝and深拷贝)
查看>>
python set集合的特点,功能and常见方法
查看>>
python set集合运算(交集,并集,差集,对称差集)
查看>>
python字符串replace()方法
查看>>
python替换文件中的指定内容
查看>>
linux系统下python tab键补全(2步搞定)
查看>>
linux locate 命令使用示例
查看>>
eclipse PyDev 字符集编码设置的3种方法
查看>>
eclipse字体大小设置
查看>>
python __init__.py __name__ __doc__ __file__ argv[0] 浅析
查看>>
Python 命名空间和LEGB规则
查看>>
python 函数的嵌套定义 and 函数的返回值是函数
查看>>
Python 内置函数 locals() 和globals()
查看>>
Python repr() 函数和str() 函数
查看>>
cmd命令里的路径包含空格 的解决方法
查看>>
linux命令执行后的 返回值与错误代码
查看>>
python os.system(command)函数的返回值 与 linux命令返回值的关系
查看>>
python os.system()和os.popen()
查看>>
python sys.argv[]用法
查看>>