- 博客(13)
- 资源 (3)
- 收藏
- 关注
原创 2017-7-28 2-8 编写函数rightrot(x,n),该函数返回将x循环右移(即从最右端移出的位将从最左端移入)n位后所得到的值
#include <stdio.h>#include <stdlib.h>int wordlength(void){ int t; unsigned v=(unsigned)~0;// 这里要尤其注意unsigned的使用 ,unsigned 左移 右移都补0 for(t=1;(v=v>>1)>0;t++) { ...
2018-07-28 18:14:24
938
原创 2017-7-28 2-7 编写函数invert(x,p,n),该函数返回对x执行下列操作后的结果值:将x中从第p位开始的n个(二进制)位求反,x的其余位保持不变
#include <stdio.h>#include <stdlib.h>unsigned invert(unsigned int x,int p,int n){ return x^ (~(~0<<n)<<(p+1-n)); //异或操作 1^1=0 0^0=0 1^0=1 0^1=1 //用或操作,已完成,比较麻烦 ...
2018-07-28 17:40:41
497
原创 2017-7-28 2-6 编写函数setbits(x,p,n,y),该函数返回对x执行下列操作后的结果值: 将x中从第p位开始的n个(二进制)位设置为y中最右边的n位的值,x的其余位保持不变
#include <stdio.h>#include <stdlib.h>unsigned setbits(unsigned int x,int p,int n,unsigned int y){ return ( x & (~((~(~0<<n))<<(p+1-n))) ) | ...
2018-07-28 16:54:25
736
原创 2017-7-28 2-5 编写函数any(s1,s2), 将字符串s2中任一字符在字符串s1中第一次出现的位置作为返回结果,如果s1中不包含s2中的字符,则返回-1
#include <stdio.h>#include <stdlib.h>int any( char s1[], char s2[] ){ int i=0,j=0; for(i=0; s1[i]!='\0'; i++) // ' \0 ' 字符串结束符 { for(j=0; s2[j] != '\0'&& j<...
2018-07-28 12:46:06
1167
原创 2017-7-18 2-4 编写函数squeeze(s1,s2), 将字符串s1中任何与字符串s2中字符匹配的字符都删除。
#include<stdio.h>#include<string.h>/************************************************************************************* 编写函数squeeze(s1,s2), 将字符串s1中任何与字符串s2中字符匹配的字符都删除。*********...
2018-07-20 01:03:38
777
1
原创 2017-7-18 2-3 编写函数htoi(s), 将十六进制的字符串(包含0x或0X)转换为与之等价的整形值,字符串中允许包含的数字:0~9,a~f,A~F
#define YES 1#define NO 0 int htoi(char s[]){ int hexdigit,i,inhex,n; i=0; if(s[i]=='0') { ++i; if(s[i]=='x' || s[i]=='X') { ++i; } } n=0; inhex=YES; for( ;inhex==YES; ++i) {...
2018-07-19 00:29:15
640
原创 2017-7-17 2-2 不使用&&和|| 编写一个与debug1中for循环等价的循环语句
#include<stdio.h>#define debug1 0#define debug2 1int i,lim=10;char c,s[10];enum loop {NO,YES};enum loop okloop =YES;main(){#if debug1 for(i=0;i<lim-1&&(c=getchar())!='\n'&...
2018-07-18 00:29:01
138
原创 2018-7-16 2-1 分别由signed 和unsigned 限定的 char,short,int,long类型变量的取值范围
#include<stdio.h>#include<limits.h>#define debug1 0 //使用debug1 必须包含limits.h#define debug2 1main(){ #if debug1 //signed types printf("signed char min = %d \n",SCHAR_MIN); pr...
2018-07-17 00:27:26
259
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人