C程序設計語言 第二版 新版 第二章 自用
2-1
#include<stdio.h>
int main()
{
printf("char:\n");
printf("max %d\n",(char)((unsigned char)~0>>1));
printf("min %d\n",(char)(~(unsigned char)~0>>1));
printf("int:\n");
printf("max %d\n",(int)((unsigned int)~0>>1));
printf("min %d\n",(int)( ~((unsigned int)~0>>1) ) );
printf("short:\n");
printf("min %d\n",(short)(~(unsigned short)~0>>1));
printf("long:\n");
printf("max %d\n",(long)((unsigned long)~0>>1));
printf("min %d\n",(long)( ~((unsigned long)~0>>1) ) );
printf("unsighed char:\n");
printf("max %u\n",((unsigned char)~0));
printf("unsighed int:\n");
printf("max %u\n",(unsigned int)(~0));
printf("unsighed short:\n");
printf("max %u\n",((unsigned short)~0));
printf("unsighed long:\n");
printf("max %u\n",((unsigned long)~0));
return 0;
}
#include<stdio.h>
#include<limits.h>
int main()
{
printf("char min%d",SCHAR_MIN);
printf("\nint min %d",INT_MIN);
printf("int max%d",INT_MAX);
printf("\nshort min%d",SHRT_MIN);
printf("short max%d",SHRT_MAX);
printf("\n long min%ld",LONG_MIN);
printf("long max%ld",LONG_MAX);
printf("\nun___");
printf("\nunchar max%u",UCHAR_MAX);
printf("\nunint max%u",UINT_MAX);
printf("\nunshort max%u",USHRT_MAX);
printf("\nunlong max%lu",ULONG_MAX);
return 0;
}
2-2
#include<stdio.h>
/*for(i=0;i<lim-1)&&(c=getchar)!=‘\n‘&&c!=EOF*/
enum l{no,yes};
int main()
{
enum l l1=yes;
int i=0;
while(l1=yes)
{
if(i>=lim-1) l1=no;
else if((c=getchar)==‘\n‘) l1=no;
else if(c==EOF) l1=no;
else {s[i]=c;i++;}
}
return 0;
}
2-3
#include<stdio.h>
#define yes 1
#define no 0
int main()
{
return 0;
}
int htoi(char s[])
{
int hexdigit ,n, inhex,i;
i=0;
if(s[i]==‘0‘)
{
i++;
if(s[i]==‘x‘||s[i]==‘X‘)
{
i++;
}
}
n=0;
inhex=yes;
for(;inhex==yes;i++)
{
if(s[i]>=‘0‘&&s[i]<=‘9‘) hexdigit=s[i]-‘0‘;
else if(s[i]>=‘a‘&&s[i]<=‘f‘)hexdigit=s[i]-‘a‘+10;
else if(s[i]>=‘A‘&&s[i]<=‘F‘)hexdigit=s[i]-‘A‘+10;
else inhex=no;
if(inhex==yes)
n=16*n+hexdigit;
}
return n;
}
2-4
#include<stdio.h>
void squeeze(char s[],char s1[]);
int main()
{
return 0;
}
void squeeze(char s1[],char s2[])
{
int i,k,j;
for(i=0,k=0;s1[i]!=‘\0‘;i++)
{
for(j=0;s2[j]!=‘\0‘&&s2[j]!=s1[i];j++) ;
if(s2[j]==‘\0‘)
s1[k++]=s1[i];
}
s1[k]=‘\0‘;
}
2-5
#include<stdio.h>
int squeeze(char s[],char s1[]);
int main()
{
return 0;
}
int squeeze(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;
}
2-6
#include<stdio.h>
int main()
{
return 0;
}
unsigned setbits (unsigned int x,int p,int n,unsigned int y )
{
return (x&( ~(~(~0<<n)<<(p+1-n)) ))|(y&( ~(~0<<n)<<(p+1-n) ));
}
2-7
#include<stdio.h>
int main()
{
return 0;
}
unsigned setbits (unsigned int x,int p,int n )
{
return (x^( ~(~0<<n)<<(p+1-n)));
}
2-8
#include<stdio.h>
int main()
{
return 0;
}
int wordlength(void)
{
int i;
unsigned v=(unsigned) ~0;
for(i=1;(v=v>>1)>0;i++);
return i;
}
unsigned setbits (unsigned x,int n )
{
int rbit;
while(n-->0)
{
rbit=(x&1)<<(wordlength()-1);
x=x>>1;
x=x|rbit;
}
return x;
}
#include<stdio.h>
int main()
{
return 0;
}
int wordlength(void)
{
int i;
unsigned v=(unsigned) ~0;
for(i=1;(v=v>>1)>0;i++);
return i;
}
unsigned setbits (unsigned x,int n )
{
unsigned rabits;
if((n=n%wordlength())>0)
{
rabits=~(~0<<n)&x;
rabits=rabits<<(wordlength()-n);
x=x>>n;
x=x|rabits;
}
return x;
}
2-9
#include<stdio.h>
int main()
{
return 0;
}
int bitcount(unsigned x)
{
int b;
for(b;x!=0;x&=(x-1))
b++;
return b;
}
2-10
#include<stdio.h>
int main()
{
return 0;
}
int bitcount(int c)
{
return c>=‘A‘&&c<=‘Z‘? c+‘a‘-‘A‘:c;
}
C程序設計語言 第二版 新版 第二章 自用