The C programming language (second edition,KR) exercise(CHAPTER 2)

      E x c e r c i s e 2 − 1 Excercise\quad 2-1 Excercise21:输出结果如图1和图2所示,这道练习题需要文章1和文章2的知识。

#include <stdio.h>
#include <limits.h>float getFloat(char sign, unsigned char exp, unsigned mantissa);
double getDouble(char sign, unsigned short exp, unsigned long long mantissa);int main(void)
{printf("sizeof(char)=%d\n",sizeof(char));printf("Signed char[%d to %d]\n", (unsigned char)(~(((unsigned char)~0) >> 1)), (unsigned char)(((unsigned char)~0 >> 1)));printf("Unsigned char[0 to %u]\n", ((unsigned char)~0));		printf("sizeof(short)=%d\n",sizeof(short));printf("Signed short[%d to %d]\n", (unsigned short)(~(((unsigned short)~0) >> 1)), (unsigned short)(((unsigned short)~0 >> 1)));printf("Unsigned short[0 to %u]\n", (unsigned short)((unsigned short)~0));printf("sizeof(int)=%d\n",sizeof(int));printf("Signed int[%d to %d]\n", (unsigned int)(~(((unsigned int)~0) >> 1)), (unsigned int)(((unsigned int)~0 >> 1)));printf("Unsigned int[0 to %u]\n", (unsigned int)((unsigned int)~0));printf("sizeof(long)=%d\n",sizeof(long));printf("Signed long[%ld to %ld]\n",(unsigned long)(~(((unsigned long)~0) >> 1)), (unsigned long)(((unsigned long)~0 >> 1)));printf("Unsigned long[0 to %lu]\n",(unsigned long)((unsigned long)~0));printf("sizeof(long long)=%d\n",sizeof(long long));printf("Signed long long[%lld to %lld]\n", (unsigned long long)(~(((unsigned long long)~0) >> 1)), (unsigned long long)(((unsigned long long)~0 >> 1)));printf("Unsigned long long[0 to %llu]\n", (unsigned long long)((unsigned long long)~0));printf("---------------------------------------------------------------\n");printf("Signed char[%d to %d]\n", SCHAR_MIN, SCHAR_MAX);printf("Unsigned char[0 to %u]\n", UCHAR_MAX);		printf("---------------------------------------------------------------\n");printf("Signed short[%d to %d]\n", SHRT_MIN, SHRT_MAX);printf("Unsigned short[0 to %u]\n", USHRT_MAX);printf("---------------------------------------------------------------\n");printf("Signed int[%d to %d]\n", INT_MIN, INT_MAX);printf("Unsigned int[0 to %u]\n", UINT_MAX);printf("---------------------------------------------------------------\n");printf("Signed long[%ld to %ld]\n",LONG_MIN, LONG_MAX);printf("Unsigned long[0 to %lu]\n",ULONG_MAX);printf("---------------------------------------------------------------\n");printf("Signed long long[%lld to %lld]\n", LLONG_MIN, LLONG_MAX);printf("Unsigned long long[0 to %llu]\n", ULLONG_MAX);printf("---------------------------------------------------------------\n");printf("Positive Normalized Float[%g to %g]\n", getFloat(0, 0x01, 0x00000000), getFloat(0, 0xFE, 0x007FFFFF));printf("Negative Normalized Float[%g to %g]\n", getFloat(1, 0xFE, 0x007FFFFF), getFloat(1, 0x01, 0x00000000));printf("Positive De-Normalized Float[%g to %g]\n", getFloat(0, 0x00, 0x00000001), getFloat(0, 0x00, 0x007FFFFF));printf("Negative De-Normalized Float[%g to %g]\n", getFloat(1, 0x00, 0x007FFFFF), getFloat(1, 0x00, 0x00000001));printf("Positive De-Normalized Zero Float[%g]\n", getFloat(0, 0x00, 0x00000000));printf("Negative De-Normalized Zero Float[%g]\n", getFloat(1, 0x00, 0x00000000));printf("Positive Infinity Float[%g]\n", getFloat(0, 0xFF, 0x00000000));printf("Negative Infinity Float[%g]\n", getFloat(1, 0xFF, 0x00000000));	printf("Not A Number Float[%g]\n", getFloat(0, 0xFF, 0x00000010));		printf("Not A Number Float[%g]\n", getFloat(1, 0xFF, 0x00000001));		printf("Positive Normalized Double[%g to %g]\n", getDouble(0, 0x0001, 0x0000000000000000), getDouble(0, 0x07FE, 0x000FFFFFFFFFFFFF));printf("Negative Normalized Double[%g to %g]\n", getDouble(1, 0x07FE, 0x000FFFFFFFFFFFFF), getDouble(1, 0x0001, 0x0000000000000000));printf("Positive De-Normalized Double[%g to %g]\n", getDouble(0, 0x0000, 0x0000000000000001), getDouble(0, 0x0000, 0x000FFFFFFFFFFFFF));printf("Negative De-Normalized Double[%g to %g]\n", getDouble(1, 0x0000, 0x000FFFFFFFFFFFFF), getDouble(1, 0x0000, 0x0000000000000001));printf("Positive De-Normalized Zero Double[%g]\n", getDouble(0, 0x0000, 0x0000000000000000));printf("Negative De-Normalized Zero Double[%g]\n", getDouble(1, 0x0000, 0x0000000000000000));printf("Positive Infinity Double[%g]\n", getDouble(0, 0x07FF, 0x0000000000000000));printf("Negative Infinity Double[%g]\n", getDouble(1, 0x07FF, 0x0000000000000000));	printf("Not A Number Double[%g]\n", getDouble(0, 0x07FF, 0x0000000000000010));		printf("Not A Number Double[%g]\n", getDouble(1, 0x07FF, 0x0000000000000001));		return 0;
}float getFloat(char sign, unsigned char exp, unsigned mantissa)
{unsigned f = ((unsigned)(((unsigned)(sign != 0))) << 31) | ((unsigned)(((unsigned)exp) << 23)) | ((unsigned)(mantissa & 0x007FFFFF));return *((float *)&f);
}double getDouble(char sign, unsigned short exp, unsigned long long mantissa)
{unsigned long long d = ((unsigned long long)(((unsigned long long)(sign != 0)) << 63)) | ((unsigned long long)((unsigned long long)(exp & 0x07FF) << 52)) | ((unsigned long long)(mantissa & 0x000FFFFFFFFFFFFF));return *((double *)&d);
}
图1.
图2.

      E x c e r c i s e 2 − 2 Excercise\quad 2-2 Excercise22

#include <stdio.h>#define MAXLINE  (1000)int getline(char s[], int lim);int main()
{int len;    /* current line length */char line[MAXLINE];     /* current input line */while ((len = getline(line, MAXLINE)) > 0) {printf("%s",line);}return 0;
}/* getline: read a line s, return length */
int getline(char s[], int lim)
{int c, i;for (i = 0; i < lim-1; ++i){c = getchar();	if((c == '\n')){break;}else if((c== EOF)){break;}		else{s[i] = c;			}}if (c == '\n') {s[i] = c;++i;}s[i] = '\0';return i;
}

      E x c e r c i s e 2 − 3 Excercise\quad 2-3 Excercise23

#include <stdio.h>#define MAXLINE  (1000)int getline(char s[], int lim);
int htoi(char s[]);int main()
{int result=0;int len;    /* current line length */char line[MAXLINE];     /* current input line */while ((len = getline(line, MAXLINE)) > 0) {printf("%s\n",line);result=htoi(line);printf("result=%d\n",result);		}return 0;
}int power(int ex)
{int i=0;int result=1;	while(i<ex){result=result*16;	i=i+1;		}return result;
}int htoi(char s[])
{int i=0;	int result=0;	int prefix=0;	int s_index=0;int e_index=0;while(s[i]!='\0'){if(((s[i]>='0')&&(s[i]<='9'))||((s[i]>='a')&&(s[i]<='f'))||((s[i]>='A')&&(s[i]<='F'))){}else{if(i==1){if(s[0]=='0'){if((s[1]=='x')||(s[1]=='X')){prefix=1;}else{printf("Hexadecimal string error\n");return -1;						}}else{printf("Hexadecimal string error\n");return -1;						}}else{printf("Hexadecimal string error\n");return -1;}			}i=i+1;			}	s_index=i;	if(s_index==0){printf("Hexadecimal string error\n");return -1;}if(s_index==1){if((s[i]>='0')&&(s[i]<='9')){return  ((int)(s[i]-'0'));	}		if((s[i]>='a')&&(s[i]<='f')){return  ((int)((s[i]-'a')+10));				}if((s[i]>='A')&&(s[i]<='F')){return  ((int)((s[i]-'A')+10));				}				}	if(s_index==2){		if(prefix==1){printf("Hexadecimal string error\n");return -1;	}else{for(i=0;i<s_index;i++){if((s[s_index-1-i]>='0')&&(s[s_index-1-i]<='9')){result=result +  ((int)(s[s_index-1-i]-'0')) *power(i);	}		if((s[s_index-1-i]>='a')&&(s[s_index-1-i]<='f')){result=result +  ((int)((s[s_index-1-i]-'a')+10)) *power(i);										}if((s[s_index-1-i]>='A')&&(s[s_index-1-i]<='F')){result=result +  ((int)((s[s_index-1-i]-'A')+10)) *power(i);									}					}					}					}if(s_index>2){		if(prefix==1){for(i=0;i<(s_index-2);i++){if((s[s_index-1-i]>='0')&&(s[s_index-1-i]<='9')){result=result +  ((int)(s[s_index-1-i]-'0')) *power(i);	}		if((s[s_index-1-i]>='a')&&(s[s_index-1-i]<='f')){result=result +  ((int)((s[s_index-1-i]-'a')+10)) *power(i);										}if((s[s_index-1-i]>='A')&&(s[s_index-1-i]<='F')){result=result +  ((int)((s[s_index-1-i]-'A')+10)) *power(i);									}					}					}	else{for(i=0;i<s_index;i++){if((s[s_index-1-i]>='0')&&(s[s_index-1-i]<='9')){result=result +  ((int)(s[s_index-1-i]-'0')) *power(i);	}		if((s[s_index-1-i]>='a')&&(s[s_index-1-i]<='f')){result=result +  ((int)((s[s_index-1-i]-'a')+10)) *power(i);										}if((s[s_index-1-i]>='A')&&(s[s_index-1-i]<='F')){result=result +  ((int)((s[s_index-1-i]-'A')+10)) *power(i);									}					}					}					}return result;
}/* getline: read a line s, return length */
int getline(char s[], int lim)
{int c, i;for (i = 0; i < lim-1; ++i){c = getchar();	if((c == '\n')){break;}else if((c== EOF)){break;}		else{s[i] = c;			}}if (c == '\n') {s[i] = c;++i;}s[i] = '\0';return i;
}

      E x c e r c i s e 2 − 4 Excercise\quad 2-4 Excercise24

#include <stdio.h>#define MAXLINE  (1000)int getline(char s[], int lim);
void squeeze(char s1[],char s2[]);int main()
{int result=0;int len;    /* current line length */char line[MAXLINE];     /* current input line */while ((len = getline(line, MAXLINE)) > 0) {printf("before:%s\n",line);squeeze(line,"aFEb");printf("after:%s\n",line);		}return 0;
}int is_str_ch(char s[],char c)
{int i=0;char temp_c=0;	int result=0;	while((temp_c=s[i++])!='\0'){   if(temp_c==c){result=1;break;			}	}return result;
}void squeeze(char s1[],char s2[])
{int i,j;for(i=j=0;s1[i]!='\0';i++){			if(!is_str_ch(s2,s1[i])){s1[j++]=s1[i];		}				}s1[j]='\0';		
}/* getline: read a line s, return length */
int getline(char s[], int lim)
{int c, i;for (i = 0; i < lim-1; ++i){c = getchar();	if((c == '\n')){break;}else if((c== EOF)){break;}		else{s[i] = c;			}}if (c == '\n') {s[i] = c;++i;}s[i] = '\0';return i;
}

      E x c e r c i s e 2 − 5 Excercise\quad 2-5 Excercise25

#include <stdio.h>#define MAXLINE  (1000)int getline(char s[], int lim);
int any(char s1[],char s2[]);int main()
{int position=0;int len;    /* current line length */char line[MAXLINE];     /* current input line */while ((len = getline(line, MAXLINE)) > 0) {position=any(line,"aFEb");printf("position=%d\n",position);		}return 0;
}int any(char s1[],char s2[])
{int i,j;for(i=0;s1[i]!='\0';i++){			for(j=0;s2[j]!='\0';j++){if(s1[i]==s2[j]){return i;				}				}				}return -1;		
}/* getline: read a line s, return length */
int getline(char s[], int lim)
{int c, i;for (i = 0; i < lim-1; ++i){c = getchar();	if((c == '\n')){break;}else if((c== EOF)){break;}		else{s[i] = c;			}}if (c == '\n') {s[i] = c;++i;}s[i] = '\0';return i;
}

      E x c e r c i s e 2 − 6 Excercise\quad 2-6 Excercise26

#include <stdio.h>unsigned setbits(unsigned x,int p,int n,unsigned y);int main()
{unsigned x=0xFFFFFFDF;unsigned y=0xFFFFFFF2;unsigned p=0;p=setbits(x,5,2,y);printf("p=%08x\n",p);		return 0;
}unsigned setbits(unsigned x,int p,int n,unsigned y)
{unsigned front=((~((unsigned)0))<<(p+1));unsigned back=~((~((unsigned)0))<<(p-n+1));unsigned all=front | back;	x=x & all;	y=(~((~0)<<n)) & y;y=y<<(p-n+1);x=x | y;return x;		
}

      E x c e r c i s e 2 − 7 Excercise\quad 2-7 Excercise27:

#include <stdio.h>unsigned invert(unsigned x,int p,int n);int main()
{unsigned x=0xFFFF55FF;unsigned inverted=0;inverted=invert(x,15,8);printf("inverted=%08x\n",inverted);		return 0;
}unsigned invert(unsigned x,int p,int n)
{int i;unsigned temp1_x=x & (~((~((~((unsigned)0))<<n))<<(p-n+1)));unsigned temp2_x=0;	unsigned index=1<<p;for(i=0;i<n;i++){if(!(index & x)){temp2_x|=index;			}			index=index>>1;		}return (temp1_x) | (temp2_x);		
}

      E x c e r c i s e 2 − 8 Excercise\quad 2-8 Excercise28:

#include <stdio.h>unsigned rightrot(unsigned x,int n);int main()
{unsigned x=0xFFFFFF5A;unsigned rotated=0;rotated=rightrot(x,8);printf("rotated=%08x\n",rotated);		return 0;
}unsigned rightrot(unsigned x,int n)
{unsigned temp1_x=x & (~((~((unsigned)0))<<n));temp1_x=temp1_x<<((sizeof(unsigned) * 8)-n);unsigned temp2_x=(x>>n) & ((~((unsigned)0)>>n));	return (temp1_x) | (temp2_x);		
}

      E x c e r c i s e 2 − 9 Excercise\quad 2-9 Excercise29:当 x x x为奇数的时候, x x x的二进制表示的最低位肯定为1,那么此时 x − 1 x-1 x1的值二进制表示和 x x x的二进制表示基本一样,除了二进制表示的最低位为0,那么此时操作 x & = ( x − 1 ) x\&=(x-1) x&=(x1)就会将 x x x的二进制表示的最低位清零,其它位保持不变。假设此时 x = 213 x=213 x=213,那么它的二进制表示为 11010101 11010101 11010101,此时 x − 1 x-1 x1的值二进制表示为 11010100 11010100 11010100,那么此时操作 x & = ( x − 1 ) x\&=(x-1) x&=(x1)之后 x x x的值为 11010100 11010100 11010100= x − 1 x-1 x1。当 x x x为偶数的时候, x − 1 x-1 x1会将 x x x的二进制表示的最右的比特值为1的比特位的值变成0,同时将 x x x的二进制表示的最右的比特值为1的比特位后面所有为0的比特位的值都变成1,因此总的来说操作 x & = ( x − 1 ) x\&=(x-1) x&=(x1)会将 x x x的二进制表示的最右的比特值为1的比特位清零,其它位保持不变。假设此时 x = 208 x=208 x=208,那么它的二进制表示为 11010000 11010000 11010000,此时 x − 1 x-1 x1的值二进制表示为 11001111 11001111 11001111,那么此时操作 x & = ( x − 1 ) x\&=(x-1) x&=(x1)之后 x x x的值为 11000000 11000000 11000000。因此总结来说不论 x x x为奇数还是偶数,操作 x & = ( x − 1 ) x\&=(x-1) x&=(x1)都会将 x x x的二进制表示的最右的比特值为1的比特位清零,因此可以利用这个现象来统计 x x x中比特位的值为1的比特位的个数。

#include <stdio.h>int bitcount(unsigned x);int main()
{unsigned x=0x84211234;unsigned num_of_1_bits=0;num_of_1_bits=bitcount(x);printf("num_of_1_bits=%d\n",num_of_1_bits);		return 0;
}/* bitcount:  count 1 bits in x */
int bitcount(unsigned x)
{int b;for (b = 0; x != 0; x &= (x-1))b++;return b;
}

      E x c e r c i s e 2 − 10 Excercise\quad 2-10 Excercise210:

#include <stdio.h>char lower(char c);int main()
{char c='A';printf("lower(c)=%c\n",lower(c));		return 0;
}/* lower:  convert c to lower case: ASCII only */
char lower(char c)
{return ((c>='A' && c<='Z') ? (c-'A'+'a'):c);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/600631.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

leetcode代码记录(最长递增子序列

目录 1. 题目&#xff1a;2. 我的代码&#xff1a;小结&#xff1a; 1. 题目&#xff1a; 给你一个整数数组 nums &#xff0c;找到其中最长严格递增子序列的长度。 子序列 是由数组派生而来的序列&#xff0c;删除&#xff08;或不删除&#xff09;数组中的元素而不改变其余元…

六部提问法让ChatGPT帮你写论文

教程出自b站&#xff1a;ChatGPT六步提问法&#xff1a;你若诚心发问&#xff0c;它会给你一篇惊艳论文&#xff01;_哔哩哔哩_bilibili 1、现象确认&#xff08;先把你要写的论文题目拿其中的核心论点来像它提问&#xff09; 确定你和chatgpt理解在同一个频道。 2、学术概念化…

JVM虚拟机(一)介绍、JVM组成、堆、栈、方法区/元空间、直接内存

目录 一、JVM 介绍1.1 为什么要学 JVM&#xff1f;1.2 JVM 是什么&#xff1f; 二、JVM 组成2.1 程序计数器2.2 Java堆1&#xff09;JVM 内存结构2&#xff09;Java 1.7 和 1.8 中堆的区别 2.3 Java虚拟机栈1&#xff09;虚拟机栈 和 栈帧2&#xff09;常见面试题 2.4 方法区/元…

二、【易 AI】Live2d 简介与使用

When you cry for missing the sun, you will miss the stars again. 当你为错过太阳而哭泣时,你也要再错过群星了。 ——泰戈尔 一、Live2d 简介 Live2D是一种基于2D图像的动态角色技术,它能够将静态的2D角色转化为具有丰富表情和动作的实时交互角色。通过使用Live2D,开发…

【Segment Anything Model】十三:Meta的最新工作EfficientSAM,微调到自己的数据集,代码。

&#x1f349; 博主微信 cvxiayixiao 还有其他专栏点击头像查询 &#x1f353; 【Segment Anything Model】计算机视觉检测分割任务专栏。 &#x1f351; 【公开数据集预处理】特别是医疗公开数据集的接受和预处理&#xff0c;提供代码讲解。 &#x1f348; 【opencv图像处理】…

git提交代码时报错,提不了

问题 今天在换了新电脑&#xff0c;提交代码时报错 ✖ eslint --fix found some errors. Please fix them and try committing again. ✖ 21 problems (20 errors, 1 warning) husky > pre-commit hook failed (add --no-verify to bypass) 解决 通过 --no-verify 解决&…

基于java JSP 实现的固定资产管理系统

开发语言&#xff1a;Java 框架&#xff1a;ssm 技术&#xff1a;JSP JDK版本&#xff1a;JDK1.8 服务器&#xff1a;tomcat7 数据库&#xff1a;mysql 5.7 数据库工具&#xff1a;Navicat11 开发软件&#xff1a;eclipse/myeclipse/idea 系统展示 前台首页功能模块 固…

硬件标准化之道:Linux社区与硬件厂商的协同创新

在Linux的广阔世界中&#x1f30c;&#xff0c;与各式各样的硬件设备进行互动和协作是一项不断进行的挑战&#x1f527;。硬件厂商和Linux社区的紧密合作&#xff0c;通过制定一系列标准和协议&#x1f4dc;&#xff0c;使得从键盘&#x1f3b9;和鼠标&#x1f5b1;到复杂的网络…

nodejs应用程序以守护进程daemon的方式启动,容器化部署的时候一直部署出错,导致无法成功启动程序。

一、背景 nodejs应用程序使用Egg.js 框架脚本命令&#xff0c;见package.json&#xff1a; "scripts": {"debug": "egg-bin debug","clean": "easy clean","build": "easy build prod","start&…

Java中的流程控制语句

目录 一、if条件语句 二、switch分支语句 三、for循环 四、while循环 五、do...while循环 六、break、continue关键字 1、break 2、continue 一、if条件语句 如果条件表达式true&#xff0c;就执行if的语句代码块。它有以下几种格式&#xff1a; // 第一种&#xff1…

买卖股票的最佳时机III

题目链接 买卖股票的最佳时机III 题目描述 注意点 1 < prices.length < 1000000 < prices[i] < 100000不能同时参与多笔交易&#xff08;必须在再次购买前出售掉之前的股票&#xff09;最多可以完成 两笔 交易 解答思路 本题最多可以完成两笔交易&#xff0c;…

ModelSim的使用

如何用Modelsim编译高云FPGA的库(高云IP核的联合仿真) 1.找到高云云源软件自带的IP库,在安装目录下,如下: C:\Gowin\Gowin_V1.9.7.06Beta\IDE\simlib,这个路径下包含小蜜蜂家族和晨曦家族两大类,根据自己的实际使用选 2.在Modelsim的安装路径下建一个编译云源库的存放…