025. C语言基础

//  241.c
#include
void main()
{
   int a=32768;
   a=a<<1;//左移的时候,右边用0来填充。
   printf("%d",a);
   getch();
}
//  242.c
//编程求一个10进制所对应的二进制当中
//有多少个1
#include
int fun(int n)
{
   int sum=0,flag=1;
   if(n<0){n=~n;flag=0;}
   while(n)
   {
      if(n&0x0001==1)sum++;
      n=n>>1;
   }
   if(flag==1)return sum;
   else return 16-sum;
}
void main()
{
   int n;
   scanf("%d",&n);
   printf("%d",fun(n));
   getch();
}
//   243.c
#include
//求一个十进制对应的二进制数中1的数量
void main()
{
   int a,n=0;
   scanf("%d",&a);
   while(a)
   {
      a=a&(a-1);
      n++;
   }
   printf("%d",n);
   getch();
}
244.c
#include
void wait()
{
   int i,j;
   for(i=0;i<30000;i++)
     for(j=0;j<30000;j++);
}
struct bit
{
   unsigned a1:1;
   unsigned a2:1;
   unsigned a3:1;
   unsigned a4:1;
   unsigned a5:1;
   unsigned a6:1;
   unsigned a7:1;
   unsigned a8:1;
   unsigned a9:1;
   unsigned a10:1;
   unsigned a11:1;
   unsigned a12:1;
   unsigned a13:1;
   unsigned a14:1;
   unsigned a15:1;
   unsigned a16:1;
};
void put(int x)
{
   struct bit *p;
   p=(struct bit *)&x;
   printf("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d",p->a16,p->a15,p->a14,p->a13,p->a12,p->a11,p->a10,p->a9,p->a8,p->a7,p->a6,p->a5,p->a4,p->a3,p->a2,p->a1);
}
int fun(int a,int n)
{
   int b,c;
   while(1)
   {
      b=a<<15;
      c=a>>1;
      c=0x7FFF&c; //将算术右移变成逻辑右移
      a=b|c;
      printf("\r");
      wait();
      put(a);
      n--;
   }
  return a;
}

void main()
{
   int a=10;
   a=fun(a,3);
   printf("%d",a);
   getch();
}
//   245.c
#include
//将一个十进制数化成二进制并输出
int fun(int n)
{
   if(n==0||n==1)return n;
   return fun(n/2)*10+n%2;
}
void main()
{
   printf("%d",fun(10));
   getch();
}
//   246.c
#include
//将一个十进制数化成4进制并输出
struct bit
{
   unsigned a1:2;
   unsigned a2:2;
   unsigned a3:2;
   unsigned a4:2;
};
int fun(int n)
{
   struct bit *p;
   p=(struct bit *)&n;
   printf("%d%d",
           p->a2,
           p->a1);
}
void main()
{
   int n;
   scanf("%d",&n);
   fun(n);
   getch();
}
//  247.c
#include
//将一个十进制数化成4进制并输出
union node
{
    struct bit
    {
      unsigned a1:2;
      unsigned a2:2;
      unsigned a3:2;
      unsigned a4:2;
    }a;
    int b;
};
int fun(int n)
{
   union node x;
   x.b=n;
   printf("%d%d%d%d",x.a.a4,x.a.a3,x.a.a2,x.a.a1);
}
void main()
{
   int n;
   scanf("%d",&n);
   fun(n);
   getch();
}
//   248.c
#include
void main()
{
  FILE *fp;
  char a[1000]="",*p,t;
  fp=fopen("abc.txt","r");
  fscanf(fp,"%s",a);
  for(p=a;*p;p+=2)
  {
     t=*p;*p=*(p+1);*(p+1)=t;
  }
  fclose(fp);
  fp=fopen("cba.txt","w");
  fprintf(fp,"%s",a);
  getch();
}

//   249.c
#include
void main()
{
  FILE *fp;
  char a[1000],*p,t;
  gets(a);
  for(p=a;*p;p+=2)
  {
     t=*p;*p=*(p+1);*(p+1)=t;
  }
  fp=fopen("abc.txt","w");
  fprintf(fp,"%s",a);
  getch();
}
//   250.c
#include
void main()
{
  FILE *fp;
  char x;
  if((fp=fopen("abc.txt","w"))==NULL)
     {printf("error");exit(1);}
  while((x=getchar())!='\n')
  {
     fputc(x,fp);
  }
  getch();
}

你可能感兴趣的:(025. C语言基础)