c 代码存档:确定一个数是否为2的幂的完整代码(包括输入格式检查)

/********************** P o w e r O f T w o . c **********************
 * @author       :    Chunhui Zhang
 * @date         :    03/01/2009
 * @version      :    1
 * @purpose      :    For practice
 * @description  :    This c program implement a algorithm to test whether
 *                    a input number is power of two.
 *********************************************************************/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define LONGSIZE     4
#define MAXOFLONG    0x7fff
#define BUFSIZE      11

typedef enum {false=0,true=1} Boolean;

/***************** G e t N u m b e r ( ) *********************
 * Description: This function is used to get a number with type checking
 *************************************************************/
Boolean GetNumber(long *lp) //called by GetInput()
{
    int ch;
    //char *cp;
    char inputBuf[BUFSIZE];
    int i=0;
          while( (ch=getchar())!=EOF && ch!='/n' )
               {
                    if ( i==BUFSIZE-1 )//check buffer overflow
                         {
                              printf("You've overflowed the buffer whose size=%d!/n",BUFSIZE-1);
                              //flush the junk: e.g input=1234567890/n, flush 0/n when we get 9
                              while( (ch=getchar())!=EOF && ch!='/n' ){;}
                              return false;
                              //or you can choose to break
                         }
                    if (ch < '0' || ch > '9')//check characters
                         {
                              printf("You've entered characters!/n");
                              //flush the junk: e.g input=12as34/n, flush s34/n when we get a
                              while( (ch=getchar())!=EOF && ch!='/n' ){;}
                              return false;
                         }
                    inputBuf[i++]=ch;
                        
               }
          inputBuf[i]='/0';
          //if(inputBuf[0]='/0')
          //   {
          //        printf("You've entered nothing!/n");
          //        return false;
          //          //or exit(1);
          //   }
          if (sscanf(inputBuf,"%ld",lp)==1)
               {
                    return true;
               }
          else
               {
                    printf("You've entered nothing!/n");
                    return false;
                    //or you can choose to exit(1)
               }
}      

/******************* G e t I n p u t ( ) *************************
 * Description: This function is used to get input
 *****************************************************************/                    
long GetInput()
{
     long num;
     Boolean status;
    
     //Give user a chance to re-enter the input when they failed for the first time
     do {
          printf("Please enter a number(less then %ld):",MAXOFLONG);
          status = GetNumber(&num);
         
     }while(status==false);
    
     return num;
}

/******************* I s P o w e r O f T w o ( ) ***********************
 * Description: This function implement the algorithm to find out whether
 *              a number is power of two. The idea is to get the result by
 *              checking how many 1s in the binary representation of a number.
 *              As we know, a number is power of two if there is only one 1
 *              in the binary representation of it. So we can use bit operation
 *              to handle this problem instand of mathmatic calculation.
 ***********************************************************************/
void IsPowerOfTwo(long num)
{
     int i=0;
     int numOfOnes=0;
     int mask=1;
    
/*
 * This is the core part of this algorithm and I believe there are more
 * efficient algorithm. Comments are welcomed and I'd like to discuss this with you.
*/
     for (i=0,mask=1;i<LONGSIZE*8-1;++i,mask=mask<<1)
          {
               if ( (num&mask) != 0 )
                    {
                         ++numOfOnes;
                    }
          }
     if (numOfOnes==1)
          {
               printf("It is power of two/n");
          }
     else
          {
               printf("It is not power of two/n");
          }
}

/************************** m a i n ( ) *******************************/
int main ()
{
     //time_t stime,etime; 
     long testNum=0;
    
     while(1)
          {
               testNum = GetInput();

               //time(&stime); /* get   start   time */ 
               IsPowerOfTwo(testNum);
               //time(&etime); /* get   end   time */ 
               //printf(   "The running time is %ld/n"   ,   etime   -   stime   );  
 
          }
     return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值