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

时间:2021-09-19 19:27:14

/********************** 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;
}