首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

\xxx是当成 3 octal digits吗`解决方法

2012-03-02 
\xxx是当成 3 octal digits吗`?顺便帮我看下下面程序,感觉这个程序有问题,如果\xxx真的是当成3octaldigits

\xxx是当成 3 octal digits吗`?
顺便帮我看下下面程序,感觉这个程序有问题,如果\xxx真的是当成   3   octal   digits,那么转成int后会再调用digit,等下这个int会因为> base(等于8)而出错  


#include   <limits>
#include   <stdexcept>
#include   <string>
#include   <stdio.h>

#if   0
using   std::domain_error;
using   std::range_error;
using   std::string;
#endif

namespace   {
      inline   int   digit(char   c,   int   base)   {
            int   value;
            switch   (c)   {
                  case   '0 ':   value   =   0;   break;
                  case   '1 ':   value   =   1;   break;
                  case   '2 ':   value   =   2;   break;
                  case   '3 ':   value   =   3;   break;
                  case   '4 ':   value   =   4;   break;
                  case   '5 ':   value   =   5;   break;
                  case   '6 ':   value   =   6;   break;
                  case   '7 ':   value   =   7;   break;
                  case   '8 ':   value   =   8;   break;
                  case   '9 ':   value   =   9;   break;
                  case   'a ':   case   'A ':   value   =   10;   break;
                  case   'b ':   case   'B ':   value   =   11;   break;
                  case   'c ':   case   'C ':   value   =   12;   break;
                  case   'd ':   case   'D ':   value   =   13;   break;
                  case   'e ':   case   'E ':   value   =   14;   break;
                  case   'f ':   case   'F ':   value   =   15;   break;
                  default:
                        throw   domain_error(string( "invalid   digit "));
            }
            if   (value> =base)
                  throw   domain_error(string( "invalid   digit "));
            return   value;
      }

      inline   char   next_char(char   const   *&p)   {


            if   (*p!= '\\ ')   //   \   is   special;   hence   '\\ '
                  return   *p++;
            else   {   //   3   octal   digits:       //@@@@\xxx是不是表示是三个八进制要一起读`
                  int   char_value   =   digit(p[1],   8)*64
                                                  +digit(p[2],   8)*8
                                                  +digit(p[3],   8);
                  if   (char_value> std::numeric_limits <char> ::max()
                    or   char_value <std::numeric_limits <char> ::min())
                        throw   domain_error(string( "not   a   char "));
                  p   +=   4;   //   backslash   and   3   octal   digits
                  return   char_value;
            }
      }

      void   load_first_digit(char   const   *&s,   int   &value,
                                                  bool   &is_negative,   int   &base)   {
            char   c1   =   next_char(s);
            is_negative   =   c1== '- ';
            if   (c1== '- '   ||   c1== '+ ')
                  c1   =   next_char(s);
            if   (c1== '\0 ')   {   //   " ",   "- "   and   "+ "   are   illegal
                  throw   domain_error(string( "invalid   input "));
            }   else   if   (c1!= '0 ')   {
                  base   =   10;
            }   else   {
                  char   const   *p   =   s;
                  char   c2   =   next_char(p);
                  if   (c2== 'x '   ||   c2== 'X ')   {   //   "0x... "?
                        base   =   16;
                        s   =   p;
                        c1   =   next_char(s);


                  }   else   {   //   c2!= 'x '   and   c2!= 'X '
                          base   =   8;   //   I.e.,   even   "0 "   is   treated   as   octal
//@@@@@@@@@@@我觉得应该加上c1=c2;
                  }
            }
            value   =   digit(c1,   base);
      }
}   //   End   unnamed   namespace   (helper   functions)

int   atoi(char   const   *s)   {
      int   value,   base;
      bool   is_negative;
      load_first_digit(s,   value,   is_negative,   base);  
      while   (char   c   =   next_char(s))   {
            if   (value> std::numeric_limits <int> ::max()/base)
                  throw   range_error(string( "out-of-range "));
            value   *=   base;
            int   d   =   digit(c,   base);
            if   (value> std::numeric_limits <int> ::max()-d)
                  throw   range_error(string( "out-of-range "));
            value   +=   d;
      }
      return   is_negative?   -value:   value;
}

int   main()   {
      printf( "atoi(\ "123\ ")   =   %d\n ",   atoi( "123 "));
      printf( "atoi(\ "0123\ ")   =   %d\n ",   atoi( "0123 "));
      printf( "atoi(\ "0x123\ ")   =   %d\n ",   atoi( "0x123 "));
}


[解决办法]
自己先编译运行了试一下。

热点排行